One time job schedule.
“at” command is used to schedule a job for a particular time or interval. But we can’t use at command for any recurring tasks. For recurring tasks use Linux crontab.
“at” command can be useful for taking one time backup, sending email as reminder at specified time, executing a script at specified time etc.
Schedule job using at command
# at time Task Ctrl+d to save it.
Schedule “sh load.sh” command to be executed on next 6:00 AM once.
# at 6:00 AM at> sh load.sh at> < EOT > job 1 at 2016-05-10 06:00
Schedule “sh load.sh” command to be executed at 6:00 AM till 7 days from now.
# at 6:00 AM + 7days at> sh load.sh at> < EOT > job 3 at 2016-05-17 06:00
To check list of at jobs.
# at -l 1 2016-05-10 06:00 a root 3 2016-05-17 06:00 a root
# atq 1 2016-05-10 06:00 a root 3 2016-05-17 06:00 a root
To check what is scheduled
# at -c job id # at -c 3
To remove a job
# atrm job id # atq 1 2016-05-10 06:00 a root 3 2016-05-17 06:00 a root # atrm 3 # atq 1 2016-05-10 06:00 a root
Schedule task at coming 09:00 AM.
# at 09:00 AM
Schedule task at 01:00 AM on coming Sunday.
# at 01:00 AM Sun
Schedule task at 06:00 AM on coming 5th July.
# at 06:00 AM July 5
Schedule task to execute at mid night.
# at midnight
Schedule task at 09:00 AM on coming 12th Dec 2016.
# at 09:00 AM 12/12/2016 # at 09:00 AM 12.12.2016
Schedule task at 12:00 PM on same date at next month.
# at 12:00 PM next month
Schedule task at 07:00 AM tomorrow.
# at 07:00 AM tomorrow
Schedule task to execute just after 1 hour.
# at now + 1 hour
Schedule task to execute just after 30 minutes.
# at now + 30 minutes
Schedule task to execute just after 1 and 2 weeks.
# at now + 1 week # at now + 2 weeks
Schedule task to execute just after 1 and 2 years.
# at now + 1 year # at now + 2 years
Recurring Job schedule
In Linux Operating system, it is possible to create a schedule for commands or script files to be executed automatically within a specified period of time, on a specified date.
Linux comes with the automated task utility task i.e cron. The cron daemon on Linux runs jobs in the background at specific times.
The jobs that you want to run with cron can be scheduled in various ways. The most common way is to edit a file which is known as your crontab.
Normally, each user has his/her own and is able to schedule jobs by editing it.
Users can have their own individual crontab files and often there is a system-wide crontab file (usually in /etc or a subdirectory of /etc) that only system administrators can edit.
Each line of a crontab file represents a job, and looks like this
$ cd /etc/ $ cat crontab SHELL=/bin/bash PATH=/sbin:/bin:/usr/sbin:/usr/bin MAILTO=root # For details see man 4 crontabs # Example of job definition: # .------------ minute (0 - 59) # | .---------- hour (0 - 23) # | | .-------- day of month (1 - 31) # | | | .------ month (1-12) OR jan,feb,mar,apr .. # | | | | .---- day of week (0 - 6) (Sunday=0 or 7) # | | | | | OR sun,mon,tue,wed,thu,fri,sat # * * * * * user-name command to be executed
Nonstandard predefined scheduling definitions
Some cron implementations support the following non-standard macros
||Run once a year at midnight of 1 January||
||Run once a month at midnight of the first day of the month||
||Run once a week at midnight on Sunday morning||
||Run once a day at midnight||
||Run once an hour at the beginning of the hour||
||Run at startup||N/A|
Below two files play an important role
- /etc/cron.allow – If this file exists, it must contain your username for you to use cron jobs.
- /etc/cron.deny – If the cron.allow file does not exist but the /etc/cron.deny file does exist then, to use cron jobs, you must not be listed in the /etc/cron.deny file.
To display the current crontab
$ crontab -l
To edit the crontab, use below command
$ crontab -e
To delete crontab, use below command
$ crontab -r
Use cases :
Run backup.sh script at 01:30 AM daily.
30 01 * * * /bin/bash /home/ops/scripts/backup.sh
Run load.sh script daily every hour from 02 AM to 08 AM
00 02-08 * * * /bin/bash /home/ops/load.sh
Run monday.sh every Monday, at 9 A.M. and 6 P.M
00 09-18 * * Mon /bin/bash /home/ops/monday.sh