Take the 2-minute tour ×
Server Fault is a question and answer site for system and network administrators. It's 100% free, no registration required.

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

3 Answers 3

up vote 4 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.

/etc/cron.d/filename

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
1  
@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

 
discard

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.