-
Notifications
You must be signed in to change notification settings - Fork 2
Cron jobs and Docker
Stackoverflow - How to run a cron job inside a docker container
github docker-cron - docker cron example on github
Running a Cron Job in Docker Container - Knoldus blog
Excusting a python program from a shell script
crontab guru - easy way to test cron schedule expressions
cronitor - cron job monitoring service
minute hour day month weekday <command-to-execute>
* * * * * command
0 9-17 * * 1-5 command
0 */2 * * * command
* * * * * echo "test" >> logfile 2>&1
Cron can be configured to send emails with the output of jobs. It actually does this by default with your user account’s default email address, but it likely isn’t configured properly. To get email working, you’ll need a mail agent set up and configured on your server, which will allow you to use the mail command to send emails. Then, place this line above your cron jobs in your cron job:
MAILTO="yourname@gmail.com"
0 0 1 * * this_job_i_want.sh
# uncomment below to enable
# 0 0 2 * * this_job_i_dont_want.sh
echo "0 0 * * * rm -f /tmp/deepak/*" >> /var/spool/cron/deepak
Use -u to define the username for which you wish to perform the cron action
crontab -u deepak -l
#!/bin/bash
if [ `id -u` -ne 0 ]; then
echo "This script can be executed only as root, Exiting.."
exit 1
fi
case "$1" in
install|update)
CRON_FILE="/var/spool/cron/root"
if [ ! -f $CRON_FILE ]; then
echo "cron file for root doesnot exist, creating.."
touch $CRON_FILE
/usr/bin/crontab $CRON_FILE
fi
# Method 1
grep -qi "cleanup_script" $CRON_FILE
if [ $? != 0 ]; then
echo "Updating cron job for cleaning temporary files"
/bin/echo "0 0 * * * rm -f /home/deepak/cleanup_script.sh" >> $CRON_FILE
fi
# Method 2
grep -qi "cleanup_script" $CRON_FILE
if [ $? != 0 ]; then
echo "Updating cron job for cleaning temporary files"
crontab -u deepak -l >/tmp/crontab
/bin/echo "0 0 * * * rm -f /home/deepak/cleanup_script.sh" >> /tmp/crontab
crontab -u deepak /tmp/crontab
fi
;;
*)
echo "Usage: $0 {install|update}"
exit 1
;;
esac