Under Linux, you can easily automate tasks with the init command, to start a task at startup, or with cron to run a task at a time.
There are several software managing cron, here we will get a good preview of on of these software: crontab.
Create a new task
First of all, we must create a new file that will execute the code that you want.
To do this, simply open an editor (as root), and save the file in the folder you want. Make sure to include the interpreter used at the beginning of the file.
For example (using bash):
It is possible to use another shell, simply by changing #!/bin/bash chosen by the interpreter: #!/bin/python, #!/bin/perl, etc..
Then we must make the file executable:
# chmod 755 /directory/myfile
Automate a task at startup: init
init is the first process executed by the kernel.
At startup, it runs various scripts in /etc/init.d/ or /etc/rc*.d/.
In the /etc/init.d you must save the file to launch at startup.
You must then add the file to the list of processes running at startup:
# update-rc.d my-fil3 defaults
For more information
Type in a terminal:
ls -l /etc/init.d/: to see scripts running at startup
Start a task at a specific time: crontab
cron can run a task periodically: daily, weekly, every hour ...
There is a cron file for user
Adding a task
There are several folders containing tasks performed by cron:
/etc/cron.hourly : run scripts each hour.
/etc/cron.daily : scripts executed each day. .
/etc/cron.weekly : run scripts each week.
/etc/cron.monthly: run scripts each month.
Simply create a file in the folder.
However, it is possible to try to initiate tasks at more accurate times.
For this, we must create a file with correct syntax for crontab in /etc/cron.d, which contains a command or execute another file.
It is also possible to create tasks for a particular user. You must edit the crontab of the user.
Syntax for crontab
There are already many tutorials on the web about it.