How to add a crontab entry from a shell script on Linux?

crontab -e will start an editor to edit the crontab entries. But how to add a crontab entry from a shell script on Linux without interaction from users?

asked Jan 19, 2015 by Eric Z Ma (44,280 points)

1 Answer

 
Best answer

You can try this piece of script:

(crontab -l; echo "@reboot echo \"rebooted\"";) | crontab -

Note that the update by this line of script is not atomic. If some other programs edit the crontab files between the first and second invokes of crontab, the edits will be lost. Hence, make sure there is not other programs/admins editing the crontab when you use this piece of script.

answered Jan 19, 2015 by Vivian (950 points)
selected Jan 19, 2015 by Eric Z Ma

Please log in or register to answer this question.

Copyright © SysTutorials. User contributions licensed under cc-wiki with attribution required.
Hosted on Dreamhost

...