Operating System - HP-UX
1828345 Members
2958 Online
109976 Solutions
New Discussion

Re: setting cron by script.

 
SOLVED
Go to solution
ankurp
Frequent Advisor

setting cron by script.

Hi friends,
need ur help to setup cron job on a remote server by script.


rgds
Ankur
11 REPLIES 11
Eric Antunes
Honored Contributor

Re: setting cron by script.

Hi Ankur,

You need to create a .tmp file in the /usr/spool/cron/crontabs directory of that server with all the commands/scripts you want to execute, time, days. Something like this:

...
50 03 16 * * $HOME/scripts/a.sh
55 03 1,10,20 * * $HOME/scripts/b.sh
00 04 1,16 * * $HOME/scripts/b.sh
...

And activate the cron scheduler:

#crontab /usr/spool/cron/crontabs/.tmp

Best Regards,

Eric Antunes
Each and every day is a good day to learn.
Keith Bryson
Honored Contributor

Re: setting cron by script.

Hi Ankur

Can you expand your question. I think you mean you want to execute a script on one server (server A) which connects to another server (server B) and creates a crontab on that server (B)?

If so, remsh may be your best option to enable you to remotely submit the crontab on server B. Be aware though that this isn't totally secure. As an example (cronjob at 8am everyday):

echo "00 08 * * * /myjob" >/tmp/newcrontab
rcp /tmp/newcrontab SERVERB:/tmp/newcrontab
remsh SERVERB "/usr/bin/crontab /tmp/newcrontab"

Remember, /myjob must be on server B already for cron to execute it!

Give us a little more information if this is not what you require.

All the best - Keith
Arse-cover at all costs
Gordon  Morrison_1
Regular Advisor

Re: setting cron by script.

If the user on the remote machine already has a crontab file, it would be safer to append the required line to the user's crontab file:

remsh hostb "echo '00 08 * * * /myjob' >> /var/spool/cron/crontabs/"
remsh hostb /sbin/init.d/cron stop
remsh hostb /sbin/init.d/cron start
What does this button do?
Steven E. Protter
Exalted Contributor

Re: setting cron by script.

You can create a file on the machine remotely and then cat filename >>

Its usually more trouble to write such a script than it is to log on the machine and simply edit the cron file or run crontab -e.

I endorse the latter approach.

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
A. Clay Stephenson
Acclaimed Contributor

Re: setting cron by script.

Simply updating a user's cron file on the remote machine is not sufficient. The cron daemon won't know anything has changed. It is also necessary that a SIGHUP (kill -1) be sent to the cron daemon. Use the crontan command. It updates the file AND sends the SIGHUP.
If it ain't broke, I can fix that.
ankurp
Frequent Advisor

Re: setting cron by script.

That was overwhelming responce thanks for that.
I wrote a script, but it doesnt get executed on all HP ux servers (worked fine on some).
i dont know why ? .
Also i dont want to rsh / rmsh commands, also they ask for passwords.

SCRIPT :
for i in `cat servers`
do OS=`ssh $i uname -s`
echo $OS
if [ $OS = HP-UX ]
then
echo "Creating /var/adm/sa $i server"
ssh $i mkdir /var/adm/sa
echo "Setting up cron for $i server"

ssh $i cp -p /var/spool/cron/crontabs/root /var/spool/cron/crontabs/root.1
ssh $i echo "0 * * * * /usr/lib/sa/sa1 600 6" >> /var/spool/cron/crontabs/root.1
ssh $i crontab /var/spool/cron/crontabs/root.1
echo " Present : Crontab -l "
ssh $i crontab -l


elif [ $OS = Linux ]
then
echo "linux Box : $i server"
fi
done

Pls. help

Rgds
Ankur
Keith Bryson
Honored Contributor

Re: setting cron by script.

Ankur

If you're using ssh to remotely connect and execute commands, make sure you have distributed your public key from the central server to the remote server(s) and added the necessary syntax to the same users $HOME/.ssh/authorization file. If you run the script manually, does it pause when trying to connect to some systems? I would also recommend creating the temporary (new) crontab in /tmp rather than the cron spool directory and submitting it to cron from there.

Keith
Arse-cover at all costs
ankurp
Frequent Advisor

Re: setting cron by script.

hi ,

I have distributed the keys and can execute the command w/o password.
This thing doesnt work.

ssh $i echo "0 * * * * /usr/lib/sa/sa1 600 6" >> /var/spool/cron/crontabs/root.1

Why do i need to create a tmp directory.

Waitn for ur help.

Rgds
Ankur
Keith Bryson
Honored Contributor
Solution

Re: setting cron by script.

It's due to the double quotes after the echo command. SSH expects the remote command to be in the double quotes!!!

Try this instead:

ssh2 -l username server "echo 'blabla bla bla' >/tmp/me"
(obviously modified to your command) - the quote for the exho is the one under the @ sign!!

Keith
Arse-cover at all costs
Keith Bryson
Honored Contributor

Re: setting cron by script.

Basically, your script would then read:

ssh $i "cp -p /var/spool/cron/crontabs/root /var/spool/cron/crontabs/root.1"
ssh $i "echo '0 * * * * /usr/lib/sa/sa1 600 6' >> /var/spool/cron/crontabs/root.1"
ssh $i "crontab /var/spool/cron/crontabs/root.1"

All the best - Keith
Arse-cover at all costs
ankurp
Frequent Advisor

Re: setting cron by script.

VEry Thanks to all my dear friends.
I hope to be like u ;-}

rgds
Ankur