3

I'd like to execute a task at a given time, once.

I know I can just use crontab -e to add it there, but I'd like to do that through a shellscript. crontab -e gives me some temporary file to edit which is unpredictable.

Also, once the task executes I'd like to remove it from the crontab again to make sure it'd not leaving behind mess.

So is there a standardized way to add/remove entries from my personal crontab through a script?

I know I could roll my own system: Have a script that executes every minute that executes and deletes .sh files from a folder and have my "addtask" script create .sh files in that folder, but before rolling my own I wonder if there is something already.

If it matters, I use Mac OS X and zsh but I wanted to use something that works on other *nixes as well.

  • I found the "at" command, but that essentially implements my second solution of running a script every minute and requires atrun to be enabled. I'd still like to know how to edit the crontab through a shell script. – Michael Stum Jan 9 '11 at 12:00
  • 3
    at should do exactly what you want. Why make things hard on yourself by insisting on a more complicated solution? – Wim Coenen Jan 9 '11 at 12:35
  • 2
    editing the crontab in a script is possible (call crontab -e with EDITOR set to an automated program instead of an interactive editor), but it's definitely not a way to do what you want. It would be a way to set up a recurring task. For a one-off, at is the appropriate tool. – Gilles 'SO- stop being evil' Jan 9 '11 at 13:14
14

I think the at command is what you are after.

E.g.:

echo "mail -s Test mstumm < /etc/group" | at 16:30

This will e-mail you a copy of /etc/group at 4:30 PM.

You can read more about at here: http://www.softpanorama.org/Utilities/at.shtml

  • I'm experimenting with at right now. It seems to be a bit weird and complains about "garbled time" when I try to use it and it required me to enable a 'service' (man atrun). I'm going to continue experimenting with it, but I'm still interested if there's a way to edit the user crontab through a shell script in a safe and portable way. – Michael Stum Jan 9 '11 at 12:04
  • Got at working (it's weird that it wants to read the command from stdin rather than just accepting a one-liner as a parameter) after some experimentation. – Michael Stum Jan 9 '11 at 13:17
0

Adding a new cron job:

newJob="/path/to/new/script.sh"
newJobSchedule="0 0 * * 0 $newJob"
cat <(crontab -l) <(echo "$newJobSchedule") | crontab -

Deleting an old cron job:

oldJob="/path/to/old/script.sh"
cat <(fgrep -i -v "$oldJob" <(crontab -l)) | crontab -

Replacing an outdated cron job:

oldJob="/path/to/old/script.sh"
newJob="/path/to/new/script.sh"
newJobSchedule="0 0 * * 0 $newJob"
cat <(fgrep -i -v "$oldJob" <(crontab -l)) <(echo "$newJobSchedule") | crontab -

Temp files are avoided by using pipes and redirects.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.