How can you get a script to add a cron to root's crons?
The title says it all? I'm writing a script that is going to be executed by a cron everyday. I want to sell this script and during installation I want to enter this cron automatically (ie the person installing the software doesn't have to setup the cron themeselves but the installation script sets up the cron). How do you enter crons from within a script? , because crontab -e is interactive.
Thanks |
Try this:
echo -e "`crontab -l`\n5 * * * * /path/to/your/cron.job" | crontab - |
YES !
Brialliant ! Thanks it works !!
Actually One Problem with it - The Message on the cron : # DO NOT EDIT THIS FILE - edit the master and reinstall. # (- installed on Thu Sep 9 15:21:02 2004) # (Cron version -- $Id: crontab.c,v 2.13 1994/01/17 03:20:37 vixie Exp $) Keeps getting added each time you execute this command, so you end up with multiple repeats of it. Any ideas to stop this? |
You could just append your statement to the cron file in /var/spool/cron/root
echo "\n5 * * * * /path/to/your/cron.job" >> /var/spool/cron/root You might need to touch the /var/spool/cron folder as cron checks the modtime of that directory in order to dtermine if things should parse "touch /var/spool/cron" should work. |
This is mentioned in my crontab man page - if you set the variable CRONTAB_NOHEADER to 'N' at the top of your script, this problem should go away.
sjspig's solution is good too, but specific to root. Using crontab will ensure it works with any user. |
Good to know - I'll definitely try that next time. What cron are you using and what man page? I've looked in mine (Fedora Core 1 or 2) using vixie cron in manpages crontab(1 and 5) and cron. Didn't see mention but I'll try it to see if it works.. It's not working on the cron I'm running.. i wish it would..
|
I am using vixie cron, but I've just noticed that that piece from the manpage is under the heading "debian specific", so it is probably a custom mod. Sorry guys. As a more generic solution, try this:
Code:
echo -e "`crontab -l | tail -n +4`\n5 * * * * /path/to/your/cron.job" | crontab - |
All times are GMT -5. The time now is 11:43 PM. |