Skip to content

Instantly share code, notes, and snippets.

@PieterScheffers
Last active April 20, 2019 19:54
Show Gist options
  • Save PieterScheffers/0fc44696fcc45761a2b3f9c111bd33b0 to your computer and use it in GitHub Desktop.
Save PieterScheffers/0fc44696fcc45761a2b3f9c111bd33b0 to your computer and use it in GitHub Desktop.
Add entry to crontab with script
#!/usr/bin/env sh
# http://stackoverflow.com/questions/878600/how-to-create-cronjob-using-bash
# http://stackoverflow.com/questions/4880290/how-do-i-create-a-crontab-through-a-script
# http://stackoverflow.com/questions/14450866/search-for-a-cronjob-with-crontab-l/14451184#14451184
JOB='*/5 * * * * /path/to/job -with args'
FINDJOB=$(crontab -l | grep -F "$JOB")
echo " "
if [ -z "$FINDJOB" ]
then
echo "Cron job doesn't exist yet, adding it to crontab"
(crontab -l 2>/dev/null; echo "$JOB") | crontab -
else
echo "Cron job already added!"
fi
echo " "
echo "Contents of crontab: (crontab -l)"
crontab -l
echo " "
echo "To edit cron manually, execute: 'crontab -e'"
echo " "
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment