Most of the Linux users are aware of how commands are run, processes are manipulated and scripts are executed in terminal. But, if you are a Linux system administrator, you might want them to start and execute automatically in the background. As an example, you might consider running a backup job every day, at a specific time, automatically. Or you might consider an example of collecting inventory data of the systems deployed across your network, by running a script automatically on monthly basis. But, how to schedule these jobs and execute them automatically in Linux?
There is an utility in Linux known as CRON with which you can start your jobs automatically at a desired time and schedule them to get executed periodically.
Cron utility consists of two parts: The cron daemon and the cron configuration files. Cron daemon is just like any service that is started automatically whenever your system boots. Cron configuration files hold the information of what to do and when to do. The main job of cron daemon is to inspect the configuration regularly (every minute to be more precise) and check if there is any job to be completed.
In the /etc directory, you will find some sub-directories namely cron.hourly
, cron.daily
, cron.weekly
and cron.monthly
. You can put your scripts in these directories, and as their names suggest, they will be automatically executed after certain period of time. For example, if you wish to run a job or service regularly after every week, simple put the script in /etc/cron.weekly
directory.
In case you need to execute a script after every fortnight, keeping the script in any of the above mentioned directories will not help. But, every problem has a solution. You can launch crontab (CRON TABle) editor which provides an interface that is very much similar to vi editor using the command crontab –e
. This will start the crontab editor to create cron job for the root user. If you are root and you wish to create cron job for other user, you can use command like crontab -u [USER] -e
.
Read More at YourOwnLinux.