Operating System - Linux
1752427 Members
5738 Online
108788 Solutions
New Discussion юеВ

Re: how to configure crontab ?

 
SOLVED
Go to solution
Maaz
Valued Contributor

how to configure crontab ?

I have some(10) scripts that will be run via root's crontab, on almost 70 servers.

I have configured crontab on one server, to run the scripts(all of the scripts are placed under /root/bin/ directory)

question
1, is there an automated way to configure the crontab on rest of the 69 servers ?
e.g we can copy /etc/sudoers configured on 1 machine to other machines .. I mean is there any way to configure the crontab too ?


9 REPLIES 9
Fredrik.eriksson
Valued Contributor

Re: how to configure crontab ?

What you can do is use a crontab-file.

Problem with this is that crontab -l or -e won't show them (if i remember correctly).

If you want to use this just edit a file like /etc/crontab and write normal crontab procedures. Then run
# crontab /etc/crontab

Best regards
Fredrik Eriksson
Vitaly Karasik_1
Honored Contributor
Solution

Re: how to configure crontab ?

Yes, you can copy your /var/spool/cron/root file to all your linux boxes.

Note - if these boxes already have root's crontab, you should use something like

cat /tmp/root >>/var/spool/cron/root

Other way is using /etc/cron.d/ directory
Maaz
Valued Contributor

Re: how to configure crontab ?

Dear Vitaly Karasik thanks for your kind help.

# crontab crontab-file

> If you want to use this just edit a file like /etc/crontab and write normal crontab > procedures. Then run
> # crontab /etc/crontab

yes the above solution works :)

> Problem with this is that crontab -l or -e won't show them (if > i remember correctly).
No, atleast the distro(SUSE) I am using works just apposite to what you said
in case of
# crontab /path/to/file
what happens is that it overwrites the root's crontab (/var/spool/cron/tabs/root)
and 'crontab -l' and 'crontab -e' shows all the jobs in /path/to/file

Thanks Dear Vitaly Karasik
your suggestion also works ;).
the only difference I found is the path(it might be due to the distro difference).
On my box(SUSE 10 SP2) its inside /var/spool/cron/tabs directory

Regards
emha_1
Valued Contributor

Re: how to configure crontab ?

as mentioned above, it is possible to distribute somehow your crontab file to all of your servers and install it there.

However, with such number of servers I would see problematic to maintain script up to date according latest changes/requirements.
What I would do is that I would export some shared folder from one of the servers (where crontabfile would be placed) and mounted it e.g. via NFS on the rest of the servers. this way you always have one copy of the script available on all the servers
Now it depends, whether all of your servers should have just the same (shared) crontab file or there can be slight differencies. if they are the same, you may want just to link root's crontab (/var/spool/cron/root) to your NFS-shared crontabfile. If there might be differencies you need to write short script that will compile together your central NFS shared crontabfile and local one and install it after every change of either central or local script.


emha.
Dennis Handly
Acclaimed Contributor

Re: how to configure crontab ?

>emha: you may want just to link root's crontab (/var/spool/cron/root)

Directly changing the file /var/spool/cron/root may or may not work. On HP-UX, cron needs to be notified of any changes, otherwise they would be ignored.
emha_1
Valued Contributor

Re: how to configure crontab ?

yes, I believe different platforms may implement thing different ways (I have experience when on Tru64 it took some time until cron accepted changes made via crontab -e and new things 1 or 2 cycles weren't executed)
However, question is posted in linux forum ans there is SUSE distro mentioned above.
Here is what cron daemon man page on my SUSE 7.3 box says:

----
Additionally, cron checks each minute to see if its spool directory's modtime (or the modtime on /etc/crontab) has changed, and if it has, cron will then examine the modtime on all crontabs and reload those which have changed. Thus cron need not be
restarted whenever a crontab file is modified.
----

similar statement I have found in RHEL AS4 man page.

so, to satisfy cron, it may be necessary to update timestamps of mentioned folders somehow (if SUSE 10.2 still relys on folder timestamps), but there is still advantage of having one central crontab file

emha.
Maaz
Valued Contributor

Re: how to configure crontab ?

thanks emha for such a nice tip
> However, with such number of servers I would see problematic to
> maintain script up to date according latest changes/requirements.
Yes I have done the following setup
on a server I have configured rsync

on rsync server(192.168.0.6) I placed all the scripts under the /root/bin/scripts directory
and
under /root/bin/cron directory, I created a file 'root' that contains cron jobs

# cat /etc/rsyncd.conf

[scripts]
path = /root/bin/scripts

[cron-jobs]
path = /root/bin/cron

----
I then created a script, that will configure the cron jobs on all 69 servers.

#!/bin/bash

while [ 1 != 0 ]; do

clear
echo -e "\nTo quit, press CTRL+C\n"
echo -e "Please Enter the IP: "
read IP

ssh -v "$IP" "echo -e '*/1 * * * * /usr/bin/rsync -av 192.168.0.6::scripts /root/bin' >> /var/spool/cron/tabs/root"
ssh -v "$IP" "echo -e '*/1 * * * * /usr/bin/rsync -av 192.168.0.6::cron-jobs /var/spool/cron/tabs' >> /var/spool/cron/tabs/root"

if [ "$?" == "0" ]
then echo "$IP on "$(date '+%d-%m-%Y')" crontab configured properly .... OK" >> /root/Servers_Done.log
else echo "$IP on "$(date '+%d-%m-%Y')" crontab not configured .... failed" >> /root/Servers_Failed.log
fi

done

and the above is working

the only thing that doesnt seems to work is

#ssh -v "$IP" "echo -e '*/1 * * * * /usr/bin/rsync -av 192.168.0.6::scripts /root/bin' >> /var/spool/cron/tabs/root" && "echo -e '*/1 * * * * /usr/bin/rsync -av 192.168.0.6::cron-jobs /var/spool/cron/tabs' >> /var/spool/cron/tabs/root"

i.e if I use "&&"(instead of using ssh two times), then I got the following error

test.sh: line 9: echo -e '*/1 * * * * /usr/bin/rsync -av 192.168.0.6::cron-jobs /var/spool/cron/tabs' >> /var/spool/cron/tabs/root: No such file or directory

please help
Maaz
Valued Contributor

Re: how to configure crontab ?

issue resolved

ssh -v "$IP" "echo -e '*/1 * * * * /usr/bin/rsync -av 192.168.0.6::scripts /root/bin' >> /var/spool/cron/tabs/root && echo -e '*/1 * * * * /usr/bin/rsync -av 192.168.0.6::cron-jobs /var/spool/cron/tabs' >> /var/spool/cron/tabs/root"

i.e following cause error:
ssh -v $IP "....." && "...."

and following works
ssh -v "$IP" "..... && ...."

DONE ;)
Ciro  Iriarte
Valued Contributor

Re: how to configure crontab ?

With that number of servers, would be wise to implement some sort of job scheduler that will automatically spread the changes, the scripts can even be embedded in the job...

Ref: http://jobscheduler.sourceforge.net/