Linux Job Scheduling Using Crontab
Cron Jobs are used for scheduling tasks to run on the server. The task can be either command or a script. The background process(daemon) behind the cronjob is the crond. In the /etc folder, there is a file called crontab where we can write the command or name of the script that is to be executed at as per our customized time.
Format of the Crontab file
1 2 3 4 5 6 1- minute (0-59) 2- hour (0-23) 3- day of month (1-31) 4- month (1-12, or name such as jan, feb, etc) 5- day of week ( 0-7(0,7 = Sunday) or name such as mon, tue,etc) 6- command to run
The command will print the current system date to the terminal ‘/bin/date‘ . if we want save the out put to a file
‘ /bin/date >> /root/date.txt ‘ . what will happen if we run this command in every minutes.
Q1. Run the command ‘/bin/date >> /root/data.txt ‘ in every minutes?
Crontab is the command to manage the Crontab file “-e” Switch indicates we want to edit the file “*” in the field means every
a star in the minutes field meand every minutes, a “*” in the hour field means every hour …..
[[email protected]]# crontab -e * * * * * /bin/date >> /tmp/data.txt
Q2. Make the ‘/bin/date >> /tmp/data.txt’ into a script called timenow1.sh and put it in the /bin directory. Then execute the timenow1.sh in every 5 min?
[[email protected]]# vim /bin/timenow1.sh /bin/date >> /tmp/data.txt [[email protected]]# chmod +x /bin/timenow1.sh
[[email protected]]# crontab -e */5 * * * * /bin/timenow1.sh
Q3. Run the script ‘timenow1.sh’ after 2 PM there after every 5min. (14:05, 14:10, 14:15, 14:20, 14:25 ………)
*/5 14 * * * /bin/timenow1.sh
Q4. Run the script every 5 minutes in the first 10 minute after 2PM.
0-10/5 14 * * * /bin/timenow1.sh
Q5. Run the script every 5 minutes in the first 10 minute after 2PM- to -5PM
0-10/5 14-16 * * * /bin/timenow1.sh
Q6. Run the Script every weekday (i.e excluding Sat and Sun) during the working hours 9 a.m – 6 p.m.
00 09-18 * * 1-5 /bin/timenow1.sh 00 0th Minutes (Top of the Hour). 09-18 9am,10am,11am,12am,1pm,2pm,3pm,4pm,5pm,6pm. * Every day. * Every month. 1-5 Mon,Tue,Wed,Thu,Fri.
Q7. Run the Script every second hour in the first, seventh, 25th, and 47th minute.
1,7,25,47 */2 * * * /bin/timenow1.sh
Q8. Run the Script at 2:15pm on the first of every month, enter:
15 14 1 * * /bin/timenow1.sh
Q9. Make a script called the timenow2.sh /bin/timenow2.sh with the contents /bin/date >> /tmp/timenow2.txt and set a cronjob
for the user clado that will run the script timenow2.sh every 1 .
[[email protected]]# vim /bin/timenow2.sh /bin/date >> /tmp/timenow2.txt [[email protected]]# chmod +x /bin/timeout2.sh [[email protected]]# crontab -u clado -e * * * * * /bin/timenow2.sh
Q10. List all the Cronjob of the user server.
[[email protected]]# crontab -u server -l
Q11. To remove or erase all crontab jobs use the following command.
[[email protected]]# crontab -r crontab -r -u server
Controlling Access to Cron
The /etc/cron.allow and /etc/cron.deny files are used to restrict access to cron. The format of both access control files is one username on each line. Whitespace is not permitted in either file. The cron daemon (crond) does not have to be restarted if the access control files are modified. The access control files are checked each time a user tries to add or delete a cron job.
The root user can always use cron, regardless of the usernames listed in the access control files.If the file cron.allow exists, only users listed in it are allowed to use cron, and the cron.deny file is ignored.If cron.allow does not exist, users listed in cron.deny are not allowed to use cron.
Access can also be controlled through Pluggable Authentication Modules (PAM). These settings are stored in /etc/security/access.conf. For example, adding the following line in this file forbids creating crontabs for all users except the root user:
-:ALL EXCEPT root :cron
The forbidden jobs are logged in an appropriate log file or, when using “crontab -e”, returned to the standard output. For more information, refer to access.conf.5 (i.e. man 5 access.conf).
Q2. Block the user ‘student’ and ‘visitor’ from installing a crontab
[[email protected]]# vim /etc/cron.deny student visitor
Managing the cron service.
# service crond status
# service crond stop
# service crond restart
# chkconfig –list crond
# chkconfig crond on
Prepared by Fujin Komalan