Scheduling processes Part 1
Contents
Introduction
We often, need to schedule some process to be run at periodic intervals. These can be maintenance tasks, backups, etc. Linux provides the "cron" utility for this purpose.
Crontab
The "crontab" is the program that is used to configure the cron daemon. The name of the daemon is called "crond". We can confirm that it is up and running on the hills server using the following command:
[amittal@hills mm]$ ps -aef | grep "crond" root 1641 1 0 Oct19 ? 00:00:15 /usr/sbin/crond -n amittal 2709483 2609417 0 16:50 pts/2 00:00:00 grep --color=auto crond [amittal@hills mm]$Each user can have their own crontab file. The files are kept in "/var/spool/cron/crontabs" on the hills server. We do not have access to edit the files directly. The schedule entries are kept in text files that although editable are manipulated using the crontab utility. The format of an entry follows as shown below.
* * * * * Command_to_execute | | | | | | | | | Day of the Week ( 0 - 6 ) ( Sunday = 0 ) | | | | | | | Month ( 1 - 12 ) | | | | | Day of Month ( 1 - 31 ) | | | Hour ( 0 - 23 ) | Min ( 0 - 59 )
The value of "*" means all values. Specific values for the fields are: minute 0-59 hour 0-23 day of month 1-31 month 1-12 First three character strings are also allowed. For example: jan, feb, mar, apr, may, jun, jul, aug, sep, oct, nov, or dec. day of week 0-7 with 7 being Sunday or use strings. Example:mon, tue, wed, thu, fri, sat, or sun. Strings can be mixed ( upper and lowercase) or just uppercase and lowercase. The comma can be used to separate multiple values: "1,5,10" . The dash can be used to specify a range: "1-5" means "1,2,3,4,5" . We can combine ranges and lists: "1-5, 7-10" . The "/" is used to specify an interval. For minute field "*/5" means every 5 minutes.
Crontab options
To install or update job in crontab, use -e option: $ crontab -e To List Crontab entries, use -l option: $ crontab -l To Deinstall job from crontab, use -r option: $ crontab -r To Confirm Deinstall of job from crontab, use -i option: $ crontab -i -r To add SELINUX security to crontab file, use -s option: $ crontab -s To edit other user crontab, user -u option and specify username: $ crontab -u username -e To List other user crontab entries: $ crontab -u username -l We normally do not have permissions to use the "-u" option. [amittal@hills mm]$ crontab -u amittal -l must be privileged to use -u Cron abbreviations @reboot : Run once after reboot. @yearly : Run once a year, ie. "0 0 1 1 *". @annually : Run once a year, ie. "0 0 1 1 *". @monthly : Run once a month, ie. "0 0 1 * *". @weekly : Run once a week, ie. "0 0 * * 0". @daily : Run once a day, ie. "0 0 * * *". @hourly : Run once an hour, ie. "0 * * * *".We are going to set up our own cron job on the hills server. It is very important to delete the job once we are done else it will keep running even after we log out.
We first create a directory. I have created mine called "crontab". [amittal@hills crontab]$ pwd /users/amittal/cs260a/crontab We create a file in it called "sample.sh" whose contents are: date >> /users/amittal/cs260a/crontab/1.txt The command appends the current date and time to the end of "1.txt" . Make sure "sample.sh" is executable by using the "chmod" command. [amittal@hills crontab]$ chmod 777 sample.sh [amittal@hills crontab]$ ls -l sample.sh -rwxrwxrwx 1 amittal csdept 44 Dec 31 17:38 sample.sh The path will be different depending on where you created your folder and of course the name of your home folder. Now we create an entry by typing: crontab -e This will open up an editor and type: */1 * * * * /users/amittal/cs260a/crontab/sample.sh Again the path will be different for you. This command tells cron to run our sample.sh every minute. Come out of the editor and wait a few minutes. Check the output of the "1.txt" file. [amittal@hills crontab]$ crontab -l no crontab for amittal [amittal@hills crontab]$ crontab -e no crontab for amittal - using an empty one crontab: installing new crontab [amittal@hills crontab]$ crontab -l */1 * * * * /users/amittal/cs260a/crontab/sample.sh [amittal@hills crontab]$ cat 1.txt Sun Jan 1 11:12:01 PST 2023 Sun Jan 1 11:13:01 PST 2023 Sun Jan 1 11:14:01 PST 2023 Once we are done with the cron entry we can delete it in 2 ways. One way is to use the "crontab -e" . This will open up the "vi" editor. Delete the line and save the file. The other is to run the "crontab -r" command. Not this will delete all the cron jobs. Rin the "crontab -l" command to make sure the job has been removed. TO DO Repeat the above but put the interval as 5 minutes.Examples of cron entries:
Run at 12:59 pm every day 59 12 * * * /usr/bin/sample.sh > /dev/null 2>&1 The notation after "sample.sh" is a common scheme used in Linux to redirect output to the "/dev/null" which eats up the output text. Run on Tues-Sat at 2 am 0 2 * * 2-6 /usr/bin/sample.sh 1>/dev/null 2>&1 Run at 7:30 am , 9:30 am , 1:30 pm and 3:30 pm 30 07,09,13,15 * * * /usr/bin/sample.sh 1>/dev/null 2>&1 Run twice at 5 am and 5 pm. 0 5,17 * * * /usr/bin/sample.sh 1>/dev/null 2>&1 Run every minute.. * * * * * /usr/bin/sample.sh 1>/dev/null 2>&1 Run every 10 minutes */10 * * * * /usr/bin/sample.sh 1>/dev/null 2>&1 To run in jan may aug. This will run the program every minute. * * * jan,may,aug * /usr/bin/sample.sh 1>/dev/null 2>&1 Run at 5 pm on Sunday and Friday 0 17 * * sun,fri To run every 4 hours 0 */4 * * * /usr/bin/sample.sh 1>/dev/null 2>&1 Run at 4 am and 5 pm on Sunday and Monday 0 4,17 * * sun,mon sample1.sh; sample2.sh Run multiple jobs with a single cron job entry * * * * * sample1.sh; sample2.sh To run a job on the 30th second. * * * * * sleep 30; sample.sh Example output: [amittal@hills crontab]$ cat 1.txt Sun Jan 1 19:48:31 PST 2023 Sun Jan 1 19:49:31 PST 2023 Sun Jan 1 19:50:31 PST 2023 Sun Jan 1 19:51:31 PST 2023 Sun Jan 1 19:52:31 PST 2023 Sun Jan 1 19:53:31 PST 2023 Run the program on an hourly basis. If we enter the cron entry say at 7:30 pm then it will run it on the hour at 8:00 pm @hourly sample.sh
TO DO #Write a cron entry to run every 5 minutes on a Mon, Tues, Wed #Use strings and then numbers for the day Every day at midnight Every weekday at midnight Midnight on 1st and 15th day of the month 6.32 PM on the 17th, 21st and 29th of November plus each Monday and Wednesday in November each yearThe cron daemon checks whether we created an entry using the "-e" option regularly. We do not have to do anything extra like informing the daemon that an entry has been created.There are 2 types of crontabs: 1 is the system wide crontab and the other is user based. One needs root privileges in order to execute the system crontab. The cron entry file is usually kept in "/etc/crontab" whereas the user cron entry file is kept in "/var/spool/cron/crontabs/". For the systen crontab we have to manually edit the entry in "/etc/crontab". The entry can include an optional parameter "runas" which can specify the user to run under.
Example:
01 3 * * 3 kevin sample.sh
Environment
Modify "sample.sh" as below: date >> /users/amittal/cs260a/crontab/1.txt echo $PATH >> /users/amittal/cs260a/crontab/1.txt >> /users/amittal/cs260a/crontab/1.txt echo $HOME >> /users/amittal/cs260a/crontab/1.txt >> /users/amittal/cs260a/crontab/1.txt If we add the cronentry again then the output of "1.txt" looks like: Mon Jan 2 07:32:01 PST 2023 /usr/bin:/bin /users/amittalThe "PATH" is set by cron and is not the same path as when we log in as a user. The "HOME" is our home folder. It is advisable to use absolute paths in the cron entry and in the programs/jobs .
What does the below mean ? Does it mean that the script will be executed if the day is 1-15 and if the day is Sunday or Saturday or does it mean that even if the day is higher than 15 and the day is Saturday or Sunday ? */1 * 1-15 * 0,6 /users/amittal/cs260a/crontab/sample.sh The above is an or condition. If the day is "1-15" then it does not matter what day of the week it is.
Linux Extensions
We have seen how default environment values are given to our crontab entry. We can customize this by placing these before our entry .On the hills server navigate to "/etc/cron.d" and open the file "0hourly" . This file contains the system ( as opposed to user ) cron entries. # Run the hourly jobs SHELL=/bin/bash PATH=/sbin:/bin:/usr/sbin:/usr/bin MAILTO=root 01 * * * * root run-parts /etc/cron.hourly The above shows the customization of some of the variables before the scheduled entry. If there is standard output or error then it is emailed to the root user. TO DO Read more about crontab in "man crontab" .
Cron Access
Recall that the user entries are kept in files in the "/var/spool/cron/crontabs/" folder. However we do not have access to this folder directly. We can only use the "crontab" utility to manipulate cron jobs. There are 2 files called "cron.allow" and "cron.deny" that can be used to restrict access. They are usually kept in the "/etc" folder. On our hills server we have a single empty file "cron.deny" in the "/etc" folder. What does this mean ? The rules are:1) If "cron.allow" does not exist and "cron.deny" exists then all users are allowed access except the ones listed on "cron.deny" . On the hills server the "cron.deny" is empty which means all users are allowed to use crontab. 2)If "cron.allow" exists then only the users listed in this file are allowed to use crontab. If the file "cron.allow" is empty then only the root user is allowed access. 3)If both files do not exist then only the root is allowed to use crontab.