Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations gkittelson on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

To add cronjob entry on remote system automatically

Status
Not open for further replies.

rrrirvine

MIS
Oct 19, 2001
7
US
Hi..

I need help...
I wont to add one cronjob entry on all my production system (200+). is there any way we can accomodate this task thru script ?

Thanks in advance.

Yogesh.
 
Sure, you could do this:

Update one crontab entry and make sure it works.
Then cgz the root crontab with absolute path of course.
Send to all servers, un-cgz the file and reboot.
To un-cgz, either run manually which you probably want to avoid or create an expect script which will go through a list, ftp the file, un-cgz the file, and then run the crontab PATH/root command. Done.
 
Use expect is my suggestion.
Automate the login process then just have a couple of commands for the whole process.
Example without error checking.
Code:
    foreach hst $iplist {
        spawn -noecho telnet $hst
        
        expect {
                -re "$loginprompt" {
                     send "$loginname\r"
                     expect -re "$pwprompt" {
                      send "$pw\r"
                     expect -re "$prompt" {
                       send "$croncmd\r"
                     expect -re "$vitab" {
                        send "$cmd1\r $cronstring\r"
                        send "$cmd2\r"
                        send_user "$expect_out(buffer)\n"
                        close $spawn_id ; wait
                        }
                       }
                      }
                  }
             eof {do_diagnostics $spawn_id}
             timeout {do_downhost $spawn_id}
        }
   }
 
There may be a third party tool you could buy that would do this. You might try a Google search.

If you have to build it yourself, I would start by writing a little script that would add the new crontab entry to one crontab. Something like this...
Code:
#!/bin/ksh

# This defines the new crontab entry you want to add
export NEWENTRY='0 1 * * * /somedir/somescript > /somedir/somelog.log 2>&1'

export TMPFILE=/tmp/crontab.tmp.$$

crontab -l > ${TMPFILE}
print "${NEWENTRY}" >> ${TMPFILE}
crontab ${TMPFILE}

rm ${TMPFILE}
Then, you need to get this run on all of your machines. The easiest way to do this would probably be with [tt]rsh[/tt]. The only problem with this is if you don't have [tt]hosts.equiv[/tt] or [tt].rhosts[/tt] files set up on all of these 200+ machines, just setting that all up would take a lot of time.

Once it's set up though, you could do them all as follows. Set up a file that has all of the host names that need the entry added. Say it's [tt]hostnames.txt[/tt] and it looks something like...
[tt]
shadowcat
phoenix
jubilee
beast
storm
[/tt]
Then, to run the script, do something like...
Code:
#!/bin/ksh

while read HOST
do
   print "Working on ${HOST}"
   rcp  addcron.ksh  ${HOST}:addcron.ksh
   rsh  ${HOST}  ./addcron.ksh
done < hostnames.txt > addcron.log 2>&1
Of course you'd want to add some error checking and such, but this would be the skeleton for what you'd want to do.

The only caveat is that if you are adding this to [tt]root[/tt]'s crontab on each machine, you may be opening up a very big security hole if you don't configure the [tt]hosts.equiv[/tt] or [tt].rhosts[/tt] files correctly.

 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top