LinuxQuestions.org

LinuxQuestions.org (/questions/)
-   Linux - General (https://www.linuxquestions.org/questions/linux-general-1/)
-   -   How can you get a script to add a cron to root's crons? (https://www.linuxquestions.org/questions/linux-general-1/how-can-you-get-a-script-to-add-a-cron-to-roots-crons-228429/)

bjdea1 09-08-2004 11:38 PM

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

CroMagnon 09-09-2004 12:11 AM

Try this:

echo -e "`crontab -l`\n5 * * * * /path/to/your/cron.job" | crontab -

bjdea1 09-09-2004 12:27 AM

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?

sjspig 09-09-2004 11:35 AM

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.

CroMagnon 09-09-2004 05:16 PM

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.

sjspig 09-09-2004 06:25 PM

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..

CroMagnon 09-09-2004 06:45 PM

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 -
This will trim the first 3 lines from the output of crontab -l.


All times are GMT -5. The time now is 11:43 PM.