Unix & Linux Stack Exchange is a question and answer site for users of Linux, FreeBSD and other Un*x-like operating systems. Join them; it only takes a minute:

Sign up
Here's how it works:
  1. Anybody can ask a question
  2. Anybody can answer
  3. The best answers are voted up and rise to the top

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.

share|improve this question
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
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
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 Jan 9 '11 at 13:14
up vote 12 down vote accepted

I think the at command is what you are after.


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

share|improve this answer
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

Adding a new cron job:

newJobSchedule="0 0 * * 0 $newJob"
cat <(crontab -l) <(echo "$newJobSchedule") | crontab -

Deleting an old cron job:

cat <(fgrep -i -v "$oldJob" <(crontab -l)) | crontab -

Replacing an outdated cron job:

newJobSchedule="0 0 * * 0 $newJob"
cat <(fgrep -i -v "$oldJob" <(crontab -l)) <(echo "$newJobSchedule") | crontab -

Temp files are avoided by using pipes and redirects.

share|improve this answer

Your Answer


By posting your answer, you agree to the privacy policy and terms of service.

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