1752721 Members
7162 Online
108789 Solutions
New Discussion юеВ

crontab entry automation

 
Pat Peter
Occasional Advisor

crontab entry automation

Hi,

I need to automate updating the cron entry in my server.

Is there any way in which I can add an entry to the end of the crontab without actually opening the cron file.

-Pat
5 REPLIES 5
Patrick Wallek
Honored Contributor

Re: crontab entry automation

You could do something like:

echo "5 4 * * 1 /dir/command_to_run" >> /var/spool/cron/crontabs/root

If you do that, you will then have to stop and restart the cron daemon for the change to take effect. Otherwise your job will not run.
Steven E. Protter
Exalted Contributor

Re: crontab entry automation

Shalom Pat,

Yes,

You can echo directly to /var/spool/cron/crontab.root

This is not recommended by me but quite possible

echo "10 16 * * * /usr/local/bin/job 2>&1" >> /var/spool/cron/crontab.root

I don't do this, and you may need to start and stop the cron daemon.

Really the best way to go is to edit the file or a copy of he file.

Some will not run crontab -e.

I once had an operator come to me with apologies because a PC power supply failure wile editing a regular users crontab file caused it to be empty.

It was restored from backup, but messing with the file directly does risk that outcome.

SEP

Steven E Protter
Owner of ISN Corporation
http://isnamerica.com
http://hpuxconsulting.com
Sponsor: http://hpux.ws
Twitter: http://twitter.com/hpuxlinux
Founder http://newdatacloud.com
Peter Nikitka
Honored Contributor

Re: crontab entry automation

Hi,

if you want a change without changing - that is not possible :-) .
If you mean a 'batch change' without opening an editor:

crontab -l >/tmp/crontab.save
cp /tmp/crontab.save /tmp/crontab.new
print '1 2 3 4 * new-cronjob' >>/tmp/crontab.new
crontab /tmp/crontab.new

You have '/tmp/crontab.save' as a backup if something goes wrong ...

Perhaps you better check if 'new-cronjob' does not already exists.

mfG Peter
The Universe is a pretty big place, it's bigger than anything anyone has ever dreamed of before. So if it's just us, seems like an awful waste of space, right? Jodie Foster in "Contact"
A. Clay Stephenson
Acclaimed Contributor

Re: crontab entry automation

Never, ever edit the crontab directly. If you do, cron won't have a clue that that has been done until cron is restarted or receives a SIGHUP. The correct procedure is to use the crontab command to extract the current cron entries and then to read them back in. Crontab also sends a SIGHUP (kill -1) to cron to trigger a re-read.

Something like this should work:

#!/usr/bin/sh

typeset TDIR=${TMPDIR:-/var/tmp}
typeset C1=${TDIR}/X${$}_1.cron

crontab -l > ${C1}
echo "15 23 * * * /new/command/goes_here # comment for new command" >> ${C1}
crontab < ${C1}

rm -f ${C1}

You should add a little error checking and probably get the new entry from the command line but that's the gist of it.
If it ain't broke, I can fix that.
Patrice Le Guyader
Respected Contributor

Re: crontab entry automation

Demat Pat,

Another way to do it is to use dsau(Distributed Systems Administration Utility) if you have several servers. If you just have one it's not a good idea and should be a waste of time to configure it.

http://docs.hp.com/en/B2355-90950/ch03s04.html

If you can't use it directly due to some OS restriction, go directly to the site of the GNU product cfengine(http://www.cfengine.org/). This product deserve to work on it,it should verify and edit (if it need) the cron for you and restart or not the daemon. Cfengine can do much more, take a look.

Hope this helps
Kenavo
Pat

Good judgement comes with experience. Unfortunately, the experience usually comes from bad judgement.