cron
Updated: September 28, 2024
System task scheduler. This is where scripts can be automatically run at set intervals.
TASK SCHEDULING USING CRON TABLE
/usr/lib/cron/tabs Directory for personal crontab files.
NOTE: Edit the current crontab using the editor specified by the VISUAL or EDITOR environment variables. The specified editor must edit the file in place; any editor that unlinks the file and recreates it cannot be used. After you exit from the editor, the modified crontab will be installed automatically.
MAIN CRONTAB COMMANDS
crontab -l # lists all scheduled cron in a cron table
crontab -e # edit file where cronjobs are made
crontab -u user2 -e # edit another users cron file
sudo crontab -e # edit the roots cron file
crontab -r # deletes cron file
CRON SNIPPET
MAILTO=username@domain.com # place at top | emails on every cron in this crontab.
59 */6 * * * script.sh | mail -s "Subject of Mail" someother@address.com # email for just this cron.
59 */6 * * * script.sh >/dev/null 2>&1 # omits this cron in emails.
# ┌───────────── minute (0 - 59)
# │ ┌───────────── hour (0 - 23)
# │ │ ┌───────────── day of month (1 - 31)
# │ │ │ ┌───────────── month (1 - 12)
# │ │ │ │ ┌───────────── day of week (0 - 6) (Sunday to Saturday;
# │ │ │ │ │ 7 is also Sunday on some systems)
# │ │ │ │ │
# │ │ │ │ │
# * * * * * command_to_execute
0 5 1,15 * * command_to_execute # additional values use , | this example =1st&15th every month.
*/10 # will do intervals, this example is every 10minutes if using first slot
0 0-5 * * * command_to_execute # run every hours from 12-5am
*/30 9-17 * * 1-5 # every 30m 9am-5pm M-F
# at 4:44 a.m every day:
44 4 * * * certbot renew -n --rsa-key-size 4096 --renew-hook 'service nginx renew'
###### Sample crontab ######
# Empty temp folder every Friday at 5pm
0 5 * * 5 rm -rf /tmp/*
0 3 15 */2 * /bin/certbot renew >> /var/log/letsencrypt/le-renew.log
# Backup images to Google Drive every night at midnight
0 0 * * * rsync -za ~/Pictures/ ~/Google\ Drive/Pictures/