This is the 15th post in the Linux Series and this will about Scheduling Processes in a Linux System. Anyone learning Linux must know how to schedule processes in the Linux as it can be very useful once you start getting better at it and want to schedule processes like backing-up certain files and running a certain process at a certain time etc. There are two ways that I know to schedule processes in Linux, one is at
and the other is cron
.
Scheduling Process To Run Only Once
The at
command is used to run some process at a certain time in the future for only once. at
command is a daemon which a background process used to schedule a process.
The at
command accepts an argument as a time stamp and then asks for a file or processs to run at that time.
root@User:~$ at 7:00am
at>/home/User/script-to-run
The at
command accepts following time formats to schedule tasks.
Format | Meaning |
---|---|
at noon | Runs at noon on the current day |
at tomorrow | Runs tomorrow from current day |
at now + n minutes | Runs after n minutes from the current time |
at now + n hours | Runs after n hours from the current time |
at now + n days | Runs after n day from the current time |
at now + n weeks | Runs after n weeks from the current time |
at 10:00am | Runs at 10:00 AM of the current day |
at 10:00pm | Runs at 10:00 PM of the current day |
at 10:00pm May 15 | Runs at 10 PM on May 25 |
at 10:00pm 05/15/2021 | Runs at 10 PM on May 25 |
Scheduling Process To Run Regularly
The cron
daemon (Daemon: a process that runs in the background) is used to schedule task to run on regular basis on a specified time. The cron
and crontab
are used to manage these tasks.
To shcedule a task using cron
you need to edit the crontab
(cron table) file with adding or removing the task you want to schedule to run after the time that you specify in the file. The crontab
accepts the following syntax.
M H DOM MON DOW USER COMMAND
* * * * * User /lib/command
The five stars are for Minute, Hour, DayOfMonth, MONth, DayOfWeek respectively. You specify Minutes, Hours, DayOfMonth, MONth, DayOfWeek in the place of the stars (asterisks) after which the task should run. The Units to use in place of asterisks are following.
Unit | Limit |
---|---|
Minute (M) | After 0 to 59 Minutes |
Hour (H) | After 0 to 23 Hours |
Day Of the Month (DOM) | After 1 to 31 Days |
Month (MON) | After 1 to 12 |
Day Of the Week | After 0 to 7 Days |
Note: 0 and 7 for DOW both means sunday.
For Example: I want to backup a file after everyday at 10:00AM from monday to friday as a root user I will edit the crontab
file like following.
root@User:~$ crontab -e
# After selecting the editor to edit the crontab file
--- FILE STARTING ---
# m h dom mon dow user command
0 10 * * 1-5 root /home/user/backup-script
The 0 and 10 means at 10:00AM, two askterisks next means run them no matter what day of the month or what month it is, 1-5 means first five days of the week from monday to firday as a root user run a script with path /home/user/backup-script.
You can add as many task to schedule in the crontab
as you want by adding one line after another in the crontab
for one task.