Friday, March 23, 2012

Setup cron job from command line

The following command will insert 0 22 * * * /opt/scripts/backup into the crontab

(crontab -l 2>/dev/null -u; echo "0 22 * * * /opt/scripts/backup") | crontab -

If it is a new crontab, 2>/dev/null will get past the message of "crontab does not exist"

If you need to add for a different users, create a script called backupjob
#!/bin/bash
(crontab -l 2>/dev/null ; echo "0 22 * * * /opt/scripts/backup") | crontab -

Then call the script sudo -u jimbob backupjob