Difference between revisions of "Linux: cron jobs"

From RHS Wiki
Jump to navigation Jump to search
m (Protected "Linux: cron jobs" ([Edit=Allow only administrators] (indefinite) [Move=Allow only administrators] (indefinite)))
Tag: visualeditor
 
(7 intermediate revisions by the same user not shown)
Line 1: Line 1:
=== Linux cron tab format===
+
Cron files are located at:<br />
 +
Debian: sudo cat /var/spool/cron/crontabs/*
 +
===Linux cron tab format===
 
MIN HOUR DOM MON DOW CMD<br />
 
MIN HOUR DOM MON DOW CMD<br />
  
 
{| class="wikitable"
 
{| class="wikitable"
 
|-
 
|-
! Field !! Description !! Allowed Value
+
!Field!!Description!!Allowed Value
 
|-
 
|-
| MIN || Minute || 0-59
+
|MIN||Minute||0-59
 
|-
 
|-
| HOUR || Hour || 0-23
+
|HOUR||Hour||0-23
 
|-
 
|-
| DOM || Day of Month || 1-31
+
|DOM||Day of Month||1-31
 
|-
 
|-
| MON || Month || 1-12
+
|MON||Month||1-12
 
|-
 
|-
| DOW || Day of the week || 0-6
+
|DOW||Day of the week||0-6
 
|-
 
|-
| CMD || Command || Any command to execute
+
|CMD||Command||Any command to execute
 
|}
 
|}
=== view/edit crontab ===
+
 
 +
===view/edit crontab===
 
  $ crontab -l → To view crontab
 
  $ crontab -l → To view crontab
 
  $ crontab -e → Edit crontab
 
  $ crontab -e → Edit crontab
 
  $ crontab -u otherusername -e → Edit other user crontab
 
  $ crontab -u otherusername -e → Edit other user crontab
  
=== Schedule a job for a specific day ===
+
===Schedule a job every X minutes===
 +
*/5 * * * * /home/ramesh/backup.sh  # Every 5 minutes
 +
*/10 * * * * /home/ramesh/backup.sh  # Every 10 minutes
 +
 
 +
===Schedule a job for a specific day===
 
example: 10th June 08:30 AM.
 
example: 10th June 08:30 AM.
 
  30 08 10 06 * /home/ramesh/full-backup
 
  30 08 10 06 * /home/ramesh/full-backup
 +
 
*30 – 30th Minute
 
*30 – 30th Minute
 
*08 – 08 AM
 
*08 – 08 AM
 
*10 – 10th Day
 
*10 – 10th Day
 
*06 – 6th Month (June)
 
*06 – 6th Month (June)
* * – Every day of the week</pre>
+
*<nowiki>* – Every day of the week</nowiki>
  
=== Schedule a job twice a day ===
+
===Schedule a job twice a day===
 
  00 11,16 * * * /home/ramesh/bin/incremental-backup
 
  00 11,16 * * * /home/ramesh/bin/incremental-backup
 +
 
*00 – 0th Minute (Top of the hour)
 
*00 – 0th Minute (Top of the hour)
 
*11,16 – 11 AM and 4 PM
 
*11,16 – 11 AM and 4 PM
* * – Every day
+
*<nowiki>* – Every day</nowiki>
* * – Every month
+
*<nowiki>* – Every month</nowiki>
* * – Every day of the week
+
*<nowiki>* – Every day of the week</nowiki>
  
=== Schedule a job during working hours ===
+
===Schedule a job during working hours===
 
  00 09-18 * * * /home/ramesh/bin/check-db-status
 
  00 09-18 * * * /home/ramesh/bin/check-db-status
 +
 
*00 – 0th Minute (Top of the hour)
 
*00 – 0th Minute (Top of the hour)
 
*09-18 – 9 am, 10 am,11 am, 12 am, 1 pm, 2 pm, 3 pm, 4 pm, 5 pm, 6 pm
 
*09-18 – 9 am, 10 am,11 am, 12 am, 1 pm, 2 pm, 3 pm, 4 pm, 5 pm, 6 pm
* * – Every day
+
*<nowiki>* – Every day</nowiki>
* * – Every month
+
*<nowiki>* – Every month</nowiki>
* * – Every day of the week
+
*<nowiki>* – Every day of the week</nowiki>
  
=== Schedule a job during working hours excluding Saturday and Sunday ===
+
===Schedule a job during working hours excluding Saturday and Sunday===
 
  00 09-18 * * 1-5 /home/ramesh/bin/check-db-status
 
  00 09-18 * * 1-5 /home/ramesh/bin/check-db-status
 +
 
*00 – 0th Minute (Top of the hour)
 
*00 – 0th Minute (Top of the hour)
 
*09-18 – 9 am, 10 am,11 am, 12 am, 1 pm, 2 pm, 3 pm, 4 pm, 5 pm, 6 pm
 
*09-18 – 9 am, 10 am,11 am, 12 am, 1 pm, 2 pm, 3 pm, 4 pm, 5 pm, 6 pm
* * – Every day
+
*<nowiki>* – Every day</nowiki>
* * – Every month
+
*<nowiki>* – Every month</nowiki>
 
*1-5 -Mon, Tue, Wed, Thu and Fri (Every Weekday)
 
*1-5 -Mon, Tue, Wed, Thu and Fri (Every Weekday)
  
=== Cron jobs special codes ===
+
===Cron jobs special codes===
 
{| class="wikitable"
 
{| class="wikitable"
 
|-
 
|-
! Keword !! Equivalent
+
!Keword!!Equivalent
 
|-
 
|-
| @yearly || 0 0 1 1 *
+
|@yearly||0 0 1 1 *
 
|-
 
|-
| @daily || 0 0 * * *
+
|@daily||0 0 * * *
 
|-
 
|-
| @hourly || 0 * * * *
+
|@hourly||0 * * * *
 
|-
 
|-
| @reboot || Run at startup.
+
|@reboot||Run at startup.
 
|}
 
|}
 
  @yearly /home/ramesh/red-hat/bin/annual-maintenance
 
  @yearly /home/ramesh/red-hat/bin/annual-maintenance
Line 74: Line 85:
 
  @reboot CMD
 
  @reboot CMD
  
=== Adding paths to cron jobs ===
+
===Adding paths to cron jobs===
 
Add the following line to crontab to specify the location of the scriptss (/home/user/scripts) <br />
 
Add the following line to crontab to specify the location of the scriptss (/home/user/scripts) <br />
 
This will allow to avoid having to type the full path to the script
 
This will allow to avoid having to type the full path to the script
 
  PATH=/bin:/sbin:/usr/bin:/usr/sbin:/home/user/scripts
 
  PATH=/bin:/sbin:/usr/bin:/usr/sbin:/home/user/scripts
 +
 +
===Enable logging to /var/log/cron.log===
 +
Add to /etc/rsyslog.d/50-default.conf the line:
 +
cron.*                        /var/log/cron.log
 +
 +
Save and restart rsyslog service
 +
sudo systemctl restart rsyslog
 +
sudo systemctl restart cron
 +
 +
==Examples==
 +
 +
===WF0006D3===
 +
<syntaxhighlight lang="bash">
 +
0 9-17 * * * if [ -d "/media/rafa/GIT/" ]; then /home/rafa/.virtualenvs/git_mirror/bin/python /home/rafa/PycharmProjects/git_mirror/mirror.py work;else echo "$(date) GIT not connected" >> /home/rafa/PycharmProjects/git_mirror/logs/errors.log;fi && /home/rafa/PycharmProjects/Misc/docker_mirror/mirror.sh;fi
 +
0 9-17 * * * if [ -d "/media/rafa/GIT/" ]; then rsync -avp /home/rafa/Downloads/Backups/ /media/rafa/GIT/Wikis/;fi
 +
30 16 * * * scp wiki-tc11.s01.rra.lan:/tmp/wikibackup-$(date +"%Y-%m-%d").tar.gz /home/rafa/Downloads/Backups/attack_wiki/
 +
30 16 * * * scp wiki.rra.lan:/tmp/wikibackup-$(date +"%Y-%m-%d").tar.gz /home/rafa/Downloads/Backups/wiki/
 +
 +
</syntaxhighlight>

Latest revision as of 08:51, 12 April 2019

Cron files are located at:
Debian: sudo cat /var/spool/cron/crontabs/*

Linux cron tab format

MIN HOUR DOM MON DOW CMD

Field Description Allowed Value
MIN Minute 0-59
HOUR Hour 0-23
DOM Day of Month 1-31
MON Month 1-12
DOW Day of the week 0-6
CMD Command Any command to execute

view/edit crontab

$ crontab -l → To view crontab
$ crontab -e → Edit crontab
$ crontab -u otherusername -e → Edit other user crontab

Schedule a job every X minutes

*/5 * * * * /home/ramesh/backup.sh   # Every 5 minutes
*/10 * * * * /home/ramesh/backup.sh  # Every 10 minutes

Schedule a job for a specific day

example: 10th June 08:30 AM.

30 08 10 06 * /home/ramesh/full-backup
  • 30 – 30th Minute
  • 08 – 08 AM
  • 10 – 10th Day
  • 06 – 6th Month (June)
  • * – Every day of the week

Schedule a job twice a day

00 11,16 * * * /home/ramesh/bin/incremental-backup
  • 00 – 0th Minute (Top of the hour)
  • 11,16 – 11 AM and 4 PM
  • * – Every day
  • * – Every month
  • * – Every day of the week

Schedule a job during working hours

00 09-18 * * * /home/ramesh/bin/check-db-status
  • 00 – 0th Minute (Top of the hour)
  • 09-18 – 9 am, 10 am,11 am, 12 am, 1 pm, 2 pm, 3 pm, 4 pm, 5 pm, 6 pm
  • * – Every day
  • * – Every month
  • * – Every day of the week

Schedule a job during working hours excluding Saturday and Sunday

00 09-18 * * 1-5 /home/ramesh/bin/check-db-status
  • 00 – 0th Minute (Top of the hour)
  • 09-18 – 9 am, 10 am,11 am, 12 am, 1 pm, 2 pm, 3 pm, 4 pm, 5 pm, 6 pm
  • * – Every day
  • * – Every month
  • 1-5 -Mon, Tue, Wed, Thu and Fri (Every Weekday)

Cron jobs special codes

Keword Equivalent
@yearly 0 0 1 1 *
@daily 0 0 * * *
@hourly 0 * * * *
@reboot Run at startup.
@yearly /home/ramesh/red-hat/bin/annual-maintenance
@monthly /home/ramesh/suse/bin/tape-backup
@daily /home/ramesh/arch-linux/bin/cleanup-logs "day started"
@reboot CMD

Adding paths to cron jobs

Add the following line to crontab to specify the location of the scriptss (/home/user/scripts)
This will allow to avoid having to type the full path to the script

PATH=/bin:/sbin:/usr/bin:/usr/sbin:/home/user/scripts

Enable logging to /var/log/cron.log

Add to /etc/rsyslog.d/50-default.conf the line:

cron.*                         /var/log/cron.log

Save and restart rsyslog service

sudo systemctl restart rsyslog
sudo systemctl restart cron

Examples

WF0006D3

0 9-17 * * * if [ -d "/media/rafa/GIT/" ]; then /home/rafa/.virtualenvs/git_mirror/bin/python /home/rafa/PycharmProjects/git_mirror/mirror.py work;else echo "$(date) GIT not connected" >> /home/rafa/PycharmProjects/git_mirror/logs/errors.log;fi && /home/rafa/PycharmProjects/Misc/docker_mirror/mirror.sh;fi
0 9-17 * * * if [ -d "/media/rafa/GIT/" ]; then rsync -avp /home/rafa/Downloads/Backups/ /media/rafa/GIT/Wikis/;fi
30 16 * * * scp wiki-tc11.s01.rra.lan:/tmp/wikibackup-$(date +"%Y-%m-%d").tar.gz /home/rafa/Downloads/Backups/attack_wiki/
30 16 * * * scp wiki.rra.lan:/tmp/wikibackup-$(date +"%Y-%m-%d").tar.gz /home/rafa/Downloads/Backups/wiki/