Server Fault is a question and answer site for system and network administrators. 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 need to add a line to a user's crontab file. Normally, I would do this with crontab -e, but I want to do it with a provisioning script.

Any clever methods?

share|improve this question
up vote 5 down vote accepted

How about:

(crontab -u USERNAME -l ; echo "line to be added") | crontab -u USERNAME -

...or (although directly editing crontab files is not recommended):

echo "line to be added" >> /var/spool/cron/crontabs/USERNAME

(Assuming your provisioning script is running as root.)

share|improve this answer

There are a few ways

This will work if you want the user to edit it

echo "normal crontab line here" >> /var/spool/cron/user

If you don't want the user to edit it.. I'd drop a file into /etc/cron.d that is like

0 0 * * * username /path/to/file

You can call it whatever you want.

share|improve this answer

You could directly add new crons (if you have root privileges). you can also specify the run-as user.


share|improve this answer
writing directly to the crontab file will not produce the expected results - the cron daemon needs to know about the changes – symcbean Aug 3 '11 at 12:36
from what I know , this works ! Assuming the script written by the user validates the cron, this is the best way ! – Sairam Aug 3 '11 at 15:44
@symcbean, This actually depends on "cron" implementation in use. Traditional implementations needed to be somehow informed about the changes. Many modern implementations monitor the files and pick up any changes automatically. – snap Aug 4 '11 at 7:04

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.