🖥️crontab

➡️This is a command-line reference manual for commands and command combinations that you don’t use often enough to remember it. This cheatsheet explains the crontab command with important options and switches using examples.

▁ ▂ ▃ ▄ ꧁ 🔴☠ COMMANDLINE-KUNGFU WITH CHEATSHEETS ☠🔴꧂▅ ▃ ▂ ▁

#                 ██████╗██████╗  ██████╗ ███╗   ██╗████████╗ █████╗ ██████╗ 
#                ██╔════╝██╔══██╗██╔═══██╗████╗  ██║╚══██╔══╝██╔══██╗██╔══██╗
#                ██║     ██████╔╝██║   ██║██╔██╗ ██║   ██║   ███████║██████╔╝
#                ██║     ██╔══██╗██║   ██║██║╚██╗██║   ██║   ██╔══██║██╔══██╗
#                ╚██████╗██║  ██║╚██████╔╝██║ ╚████║   ██║   ██║  ██║██████╔╝
#                 ╚═════╝╚═╝  ╚═╝ ╚═════╝ ╚═╝  ╚═══╝   ╚═╝   ╚═╝  ╚═╝╚═════╝ 
                                                                            
                                                                           
# set a shell
SHELL=/bin/bash

# crontab format
* * * * *  command_to_execute
- - - - -
| | | | |
| | | | +- day of week (0 - 7) (where sunday is 0 and 7)
| | | +--- month (1 - 12)
| | +----- day (1 - 31)
| +------- hour (0 - 23)
+--------- minute (0 - 59)

# example entries
# every 15 min
*/15 * * * * /home/user/command.sh

# every midnight
* 0 * * * /home/user/command.sh

# every Saturday at 8:05 AM
5 8 * * 6 /home/user/command.sh

# Allowed special character (*, -, /, ?, #)
#-------------------------------------------
     Asterik(*) – Match all values in the field or any possible value.
     Hyphen(-) – To define range.
     Slash (/) – 1st field /10 meaning every ten minute or increment of range.
     Comma (,) – To separate items.

# System Wide Cron Schedule
#===========================
# System administrator can use predefine cron directory as shown below.

     /etc/cron.d
     /etc/cron.daily
     /etc/cron.hourly
     /etc/cron.monthly
     /etc/cron.weekly

# Special Strings for Common Schedule
#--------------------------------------
Strings   Meanings
@reboot   Command will run when the system reboot.
@daily    Once per day or may use @midnight.
@weekly   Once per week.
@yearly   Once per year. we can use @annually keyword also.

# Multiple Commands with Double amper-sand(&&)
#---------------------------------------------
In below example command1 and command2 run daily.

# crontab -e
@daily <command1> && <command2>

# Disable Email Notification.
#------------------------------
# By default cron send mail to user account executing cronjob. If you want to disable it add your cron job similar to below example. Using >/dev/null 2>&1 option at the end of the file will redirect all the output of the cron results under /dev/null.

crontab -e
* * * * * >/dev/null 2>&1

crontab -l | tee crontab-backup.txt | sed 's/old/new/' | crontab –

# Send mail to root when crontab command fails to run
#Got a script that gets executed by crontab as the Apache user. Had a problem with my deployment script where it didn't give the Apache user permissions to execute the script after deploying it. Crontab would call the script and it would silently fail because the Apache user didn't have permissions to run it.
# I wanted to get notified when this happened outside of the normal notifications the script itself does, in case script fails to run altogether.

# Adding || mail -s "Error $? running $_" root to my script achieved this. What it does is emails root with the exit code and the command name in the subject line, e.g.: Subject: Error 126 running drush.cron. Bash exit code 126 means command not executable, e.g. permission denied. 

# Example of how it looks in /etc/crontab:
*/15 *  * * *   www-data        drush.cron || mail -s "Error $? running $_" root

# Another example with script's normal output (stdout) being discared (e.g. mail sent only if script exits with an error or script execution fails in the first place):
*/15 *  * * *   www-data        drush.cron 1>/dev/null || mail -s "Error $? running $_" root

# Monitoring of Crons
"&&" is used to chain commands together, such that the next command is run if and only if the preceding command exited without errors (or, more accurately, exits with a return code of 0). - Source We ran our Symfony cron command with ping to OpsGenie like below:

bin/console our-cron:command && curl -X GET 'https://api.opsgenie.com/v2/heartbeats/our-cron/ping' --header 'Authorization: GenieKey '"$OPSGENIE_API_KEY"''
# We did a bit different variation to the above command and created a bash script where the command and name of the heartbeat were sent as parameters.

# Redirect stdout to /dev/null (only print output sent to stderr):
    ./test 1>/dev/null

# Redirect stderr to /dev/null (only print output sent to stdout):
    ./test 2>/dev/null

# Redirect all output to /dev/null (print nothing):
    ./test &>/dev/null

#==============================##==============================#
# CMD crontab						       #
#==============================##==============================#
░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░

  █║▌│║█║▌★ KALI ★ PARROT ★ DEBIAN 🔴 PENTESTING ★ HACKING ★ █║▌│║█║▌

              ██╗ ██╗ ██████╗  ██████╗ ██╗  ██╗███████╗██████╗
             ████████╗██╔══██╗██╔═══██╗╚██╗██╔╝██╔════╝██╔══██╗
             ╚██╔═██╔╝██║  ██║██║   ██║ ╚███╔╝ █████╗  ██║  ██║
             ████████╗██║  ██║██║   ██║ ██╔██╗ ██╔══╝  ██║  ██║
             ╚██╔═██╔╝██████╔╝╚██████╔╝██╔╝ ██╗███████╗██████╔╝
              ╚═╝ ╚═╝ ╚═════╝  ╚═════╝ ╚═╝  ╚═╝╚══════╝╚═════╝

               █║▌│║█║▌ WITH COMMANDLINE-KUNGFU POWER █║▌│║█║▌

░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░