linux_wiki:schedule_tasks_using_at_and_cron

Schedule Tasks Using At And Cron

General Information

Using system utilities to schedule one-off or reoccurring jobs.


at is good for one-off jobs that don't need to reoccur regularly.


Install, Enable, Start

yum install at
systemctl enable atd
systemctl start atd


Example Times (teatime is 4pm)

at now +5 minutes
at now +5 hours
at 12:00am
at teatime
<enter command> <ctrl+d>


Example job

at now +5 minutes
at>logger "System uptime is:$(uptime)"
at> <ctrl+d>


View jobs

atq


Remove job (cancel job)

atrm <job#>

cron is for scheduling reoccurring jobs/scripts.


Edit current user's cron

crontab -e


System wide

/etc/crontab
 
# .---------------- 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


More preferred method: Place scripts inside one of the cron.* directories.

  • /etc/cron.d/ = system executed jobs (format ⇒ /etc/crontab)
  • /etc/cron.daily/ = daily executed scripts (format ⇒ bash script)
  • /etc/cron.hourly/ = hourly executed scripts (format ⇒ bash script)
  • /etc/cron.monthly/ = monthly executed scripts (format ⇒ bash script)
  • /etc/cron.weekly/ = weekly executed scripts (format ⇒ bash script)


Example custom script in /etc/cron.d

*/5 * * * * root /home/user/uptimelog
  • Executes /home/user/uptimelog as root, every 5 mins

Anacron runs all scripts in cron.* directories. This allows jobs to be run if the scheduled time was while a system was powered off.


Main config and contents: /etc/anacrontab

#period in days   delay in minutes   job-identifier   command
1	5	cron.daily		nice run-parts /etc/cron.daily
7	25	cron.weekly		nice run-parts /etc/cron.weekly
@monthly 45	cron.monthly		nice run-parts /etc/cron.monthly


Run all anacron jobs(in /etc/anacrontab), ignore timestamps (note that the “delay in minutes” setting is still in effect)

anacron -f


View timestamps for anacron jobs (cron.daily in this example)

cat /var/spool/anacron/cron.daily


Run all jobs now, ignore delay specifications in /etc/anacrontab

anacron -n

  • linux_wiki/schedule_tasks_using_at_and_cron.txt
  • Last modified: 2019/05/25 23:50
  • (external edit)