You are on page 1of 15

Automating tasks with cron jobs

Dr. Indrajeet Kumar


 By using

 At
 Crontab
 anacron
At command

# at 11:45 pm
at>mkdir /root/Desktop/New1
at><EOT>

#atq
Crontab

 Crontab stands for “cron table”.

 It is a set of commands that are used for


running regular scheduling tasks.

 It allows to use job scheduler, which is


known as cron to execute tasks.
 Crontab is also the name of the program,
which is used to edit that schedule.

 It is driven by a crontab file, a config file


that indicates shell commands to run
periodically for the specific schedule.
Linux Crontab format

 Crontab of Linux has six fields.

 The first five fields define the time and


date of execution, and the 6’th field is
used for command execution.
Cat /etc/crontab
Commands

 sudo apt install crontab


 crontab –e
 crontab –l
 crontab -r
Crontab Examples
 Schedule a cron to execute at 2am daily.
 0 2 * * * /bin/sh backup.sh
 Command will execute at 5 AM and 5 PM daily.
 0 5,17 * * * /Desktop/test.sh
 Schedule a cron to execute on every Sunday at 5 PM.
 0 17 * * sun /scripts/script.sh
 Schedule a cron to execute on every 10 minutes.
 */10 * * * * /scripts/monitor.sh
 Schedule a cron to execute on selected months
 * * * jan,may,aug * /script/script.sh
 Schedule a cron to execute on selected days.
 0 17 * * sun,fri /script/script.sh
 Schedule a cron to execute on every four hours.

 0 */4 * * * /scripts/script.sh

 Schedule a cron to execute on every 30 Seconds.

 * * * * * /scripts/script.sh

 * * * * * sleep 30; /scripts/script.sh

 Schedule a multiple tasks in single cron.

 * * * * * /scripts/script.sh; /scripts/scrit2.sh
@yearly: @yearly /scripts/script.sh
@monthly: @monthly /scripts/script.sh
@weekly: @weekly /bin/script.sh
@daily: @daily /scripts/script.sh
@hourly: @hourly /scripts/script.sh
@reboot : @reboot /scripts/script.sh
Redirect Cron Results to plain text file

Backup cron to text file


# crontab -l > cron-backup.txt
# cat cron-backup.txt
MAIL=inder
0 2 * * * /script/backup.sh
Removing current scheduled cron:

# crontab -r
# crontab -l
no crontab for root
Restore crons from text file
# crontab cron-backup.txt
# crontab -l MAIL=rahul
0 2 * * * /script/backup.sh
 To check the log files
 Tail –f /var/log/syslog
Thank you

You might also like