LinuxQuestions.org
Review your favorite Linux distribution.
Go Back   LinuxQuestions.org > Forums > Linux Forums > Linux - General
User Name
Password
Linux - General This Linux forum is for general Linux questions and discussion.
If it is Linux Related and doesn't seem to fit in any other forum then this is the place.

Notices


Reply
  Search this Thread
Old 09-08-2004, 11:38 PM   #1
bjdea1
Member
 
Registered: Oct 2003
Posts: 37

Rep: Reputation: 15
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
 
Old 09-09-2004, 12:11 AM   #2
CroMagnon
Member
 
Registered: Sep 2004
Location: New Zealand
Distribution: Debian
Posts: 900

Rep: Reputation: 33
Try this:

echo -e "`crontab -l`\n5 * * * * /path/to/your/cron.job" | crontab -
 
Old 09-09-2004, 12:27 AM   #3
bjdea1
Member
 
Registered: Oct 2003
Posts: 37

Original Poster
Rep: Reputation: 15
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?

Last edited by bjdea1; 09-09-2004 at 12:50 AM.
 
Old 09-09-2004, 11:35 AM   #4
sjspig
LQ Newbie
 
Registered: Mar 2003
Distribution: Modded-Knoppix
Posts: 23

Rep: Reputation: 15
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.
 
Old 09-09-2004, 05:16 PM   #5
CroMagnon
Member
 
Registered: Sep 2004
Location: New Zealand
Distribution: Debian
Posts: 900

Rep: Reputation: 33
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.
 
Old 09-09-2004, 06:25 PM   #6
sjspig
LQ Newbie
 
Registered: Mar 2003
Distribution: Modded-Knoppix
Posts: 23

Rep: Reputation: 15
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..
 
Old 09-09-2004, 06:45 PM   #7
CroMagnon
Member
 
Registered: Sep 2004
Location: New Zealand
Distribution: Debian
Posts: 900

Rep: Reputation: 33
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.
 
  


Reply


Thread Tools Search this Thread
Search this Thread:

Advanced Search

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off



Similar Threads
Thread Thread Starter Forum Replies Last Post
shell script using /etc/cron.hourly to execute cron.php file? rioguia Programming 3 06-11-2008 08:09 AM
script automatically add entry in cron daemon? Thinking Linux - General 1 03-30-2005 05:27 AM
PPPD Script problem when cron-scheduling the script andresurzagasti Linux - Networking 0 11-24-2004 02:54 PM
how to add a new user with all settings the same as root's? NeoAnderson Linux - General 6 10-27-2002 05:10 PM
how to add a new user with every settings the same as root's? NeoAnderson Linux - General 9 10-05-2002 11:21 AM

LinuxQuestions.org > Forums > Linux Forums > Linux - General

All times are GMT -5. The time now is 05:22 AM.

Main Menu
Advertisement
My LQ
Write for LQ
LinuxQuestions.org is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
Syndicate
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
Facebook: linuxquestions Google+: linuxquestions
Open Source Consulting | Domain Registration