Skip to content

Instantly share code, notes, and snippets.

@mttjohnson
Created February 26, 2019 22:34
Show Gist options
  • Save mttjohnson/708cdf3dfe83c23bfccb924be38b3513 to your computer and use it in GitHub Desktop.
Save mttjohnson/708cdf3dfe83c23bfccb924be38b3513 to your computer and use it in GitHub Desktop.
Bash Disable/Enable/Append to crontab
# Example for CRONTAB_ENTRIES
# This is useful for testing scenarios
set +H # disable history expansion
CRONTAB_ENTRIES=$(cat <<CONTENTS_HEREDOC
# Edit this file to introduce tasks to be run by cron.
#
# Each task to run has to be defined through a single line
# indicating with different fields when the task will be run
# and what command to run for the task
#
# To define the time you can provide concrete values for
# minute (m), hour (h), day of month (dom), month (mon),
# and day of week (dow) or use '*' in these fields (for 'any').#
# Notice that tasks will be started based on the cron's system
# daemon's notion of time and timezones.
#
# Output of the crontab jobs (including errors) is sent through
# email to the user the crontab file belongs to (unless redirected).
#
# For example, you can run a backup of all your user accounts
# at 5 a.m every week with:
# 0 5 * * 1 tar -zcf /var/backups/home.tgz /home/
#
# For more information see the manual pages of crontab(5) and cron(8)
#
# m h dom mon dow command
#
#MAILTO="[email protected],[email protected]"
#* * * * * ! test -e /var/www/example.com/maintenance.flag && cd /var/www/example.com && ./cron.sh cron.php -m=default 2>&1 | tee -a var/log/cron_default.log
#* * * * * ! test -e /var/www/example.com/maintenance.flag && cd /var/www/example.com && ./cron.sh cron.php -m=always 2>&1 | tee -a var/log/cron_always.log
#* * * * * ! test -e /var/www/example.com/maintenance.flag && cd /var/www/example.com && /usr/bin/php misc_custom.php 2>&1 | tee -a var/log/cron_custom.log
#
#
CONTENTS_HEREDOC
)
set -H # re-enable history expansion
echo "${CRONTAB_ENTRIES}"
# Get current user's crontab entries
CRONTAB_ENTRIES=$(crontab -l 2>/dev/null)
#To disable every line in the crontab
CRONTAB_ENTRIES=$(echo "${CRONTAB_ENTRIES}" | perl -nle 's/^([^#])/# $1/;print')
# Update crontab
echo "${CRONTAB_ENTRIES}" | crontab -
# Get current user's crontab entries
CRONTAB_ENTRIES=$(crontab -l 2>/dev/null)
#To enable a disabled cron MAILTO entry
CRONTAB_ENTRIES=$(echo "${CRONTAB_ENTRIES}" | perl -nle 's/^#\s*(MAILTO=.*)/$1/;print')
#To enable any disabled magento cron entries
CRONTAB_ENTRIES=$(echo "${CRONTAB_ENTRIES}" | perl -nle 's/^#\s*([0-9*] [0-9*] [0-9*] [0-9*] [0-9*] .+cron\.php.+)/$1/;print')
#To enable any disabled custom entries
CRONTAB_ENTRIES=$(echo "${CRONTAB_ENTRIES}" | perl -nle 's/^#\s*([0-9*] [0-9*] [0-9*] [0-9*] [0-9*] .+misc_custom\.php.+)/$1/;print')
# Ensure these lines exist in the crontab
CRONTAB_MAGENTO_DEFAULT="* * * * * ! test -e /var/www/example.com/maintenance.flag && cd /var/www/example.com && ./cron.sh cron.php -m=default 2>&1 | tee -a var/log/cron_default.log"
CRONTAB_MAGENTO_ALWAYS="* * * * * ! test -e /var/www/example.com/maintenance.flag && cd /var/www/example.com && ./cron.sh cron.php -m=always 2>&1 | tee -a var/log/cron_always.log"
CRONTAB_CUSTOM="* * * * * ! test -e /var/www/example.com/maintenance.flag && cd /var/www/example.com && /usr/bin/php misc_custom.php 2>&1 | tee -a var/log/cron_custom.log"
# Append additional cron entry if not found in crontab
! echo "${CRONTAB_ENTRIES}" | perl -nle "\$found=1 if index(\$_, '${CRONTAB_MAGENTO_DEFAULT}') != -1; END { \$found ? exit 0 : exit 1 }" \
&& CRONTAB_ENTRIES=$(echo "${CRONTAB_ENTRIES}"; echo "${CRONTAB_MAGENTO_DEFAULT}")
! echo "${CRONTAB_ENTRIES}" | perl -nle "\$found=1 if index(\$_, '${CRONTAB_MAGENTO_ALWAYS}') != -1; END { \$found ? exit 0 : exit 1 }" \
&& CRONTAB_ENTRIES=$(echo "${CRONTAB_ENTRIES}"; echo "${CRONTAB_MAGENTO_ALWAYS}")
! echo "${CRONTAB_ENTRIES}" | perl -nle "\$found=1 if index(\$_, '${CRONTAB_CUSTOM}') != -1; END { \$found ? exit 0 : exit 1 }" \
&& CRONTAB_ENTRIES=$(echo "${CRONTAB_ENTRIES}"; echo "${CRONTAB_CUSTOM}")
# Update crontab
echo "${CRONTAB_ENTRIES}" | crontab -
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment