1833824 Members
2432 Online
110063 Solutions
New Discussion

Regarding crontab

 
SOLVED
Go to solution
vind123
Regular Advisor

Regarding crontab

I need to change the unix user id fcc's crontab from root unix user id. I want to automate these steps in a shell script and it is going to be run by root. How do i do it?

For changing the crontab for fcc unix user id i am going to do the below steps
login as fcc unix user id
$ crontab -l > tempfile
$ cat addfile >> tempfile
$ crontab tempfile
17 REPLIES 17
rariasn
Honored Contributor

Re: Regarding crontab

Hi,

With user "root":

# crontab -l fcc > /tmp/crontab.fcc.txt

# crontab -e root

add crontab.fcc.txt (:r /tmp/crontab.fcc.txt)


# crontab -l root

if ok,

# crontab -r fcc

rgs

ran
rariasn
Honored Contributor

Re: Regarding crontab

Sorry,

# crontab -l fcc > /tmp/crontab.fcc.txt

# cat /tmp/crontab.fcc.txt >> /var/spool/cron/crontabs/root


ran
Peter Godron
Honored Contributor

Re: Regarding crontab

hi,
how about :
As root:
cat addfile >> /var/spool/cron/crontabs/fcc

If you use crontab tempfile as root it will change the root crontab file!

Alternatively you can run your script with:
su fcc -c yourscriptname

But ensure you specify all paths and variables required.
vind123
Regular Advisor

Re: Regarding crontab

>cat updatecron_fcc.sh
crontab -l > tempfile
cat addfile >> tempfile
crontab tempfile


In my main script run_crn_update.sh
echo "cron update is started"
su scc -c "/top1/frien/new/updatecron_fcc.sh"
echo "cron update is completed"

I hope after execute the updatecron_fcc.sh script successfully only the next line echo will get executed. I dont know how it works. Do i need to run as nohup?
Hope this one will work?




Peter Godron
Honored Contributor

Re: Regarding crontab

Hi,
I would amend to:
(Please note the addfile path to be added)
>cat updatecron_fcc.sh
#!/usr/bin/sh
/usr/bin/crontab -l > /tmp/tempfile
#Please add your path before the addfile
/usr/bin/cat addfile >> /tmp/tempfile
/usr/bin/crontab /tmp/tempfile
exit

And, should it be scc or fcc root su's to ?
Jonathan Fife
Honored Contributor

Re: Regarding crontab

In order to see if the changes took effect you could do a diff of the resulting crontabs.

crontab -l fcc > /tmp/beforecronupd
echo "cron update is started"
su fcc -c "/top1/frien/new/updatecron_fcc.sh"
echo "cron update is completed"
crontab -l fcc > /tmp/aftercronupd
$(diff /tmp/beforecronupd /tmp/aftercronupd >/dev/null 2>&1) && echo "crontab did not change" || echo "crontab changed"
Decay is inherent in all compounded things. Strive on with diligence
vind123
Regular Advisor

Re: Regarding crontab

Login as Root and if i follow the below
step will it work.

crontab -l fcc > /tmp/tempfile
cat modifile >> /tmp/tempfile
crontab -l fcc tempfile
Jonathan Fife
Honored Contributor
Solution

Re: Regarding crontab

No, the last line of your script won't put a new crontab in place.

You'd need to either write directly to /var/spool/cron/crontabs/fcc (BAD)
or do an:
su fcc -c "crontab /tmp/tempfile"
Decay is inherent in all compounded things. Strive on with diligence
Steven E. Protter
Exalted Contributor

Re: Regarding crontab

Shalom,

I know linux crontab has a -f option to read from a file. crontab tempfile is not going to work.

If you are very careful, you can cat the addfile >>

The actual crontab file is in /var/spool/cron

Make a backup before you proceed.

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
Jonathan Fife
Honored Contributor

Re: Regarding crontab

Hi Stephen,

'crontab file' should install file as the crontab.

# crontab -l fifejj > /tmp/mycron
# cat /tmp/mycron
* * * * * /bin/true
# echo "* * * * * /bin/false" >> /tmp/mycron
# su fifejj -c "crontab /tmp/mycron"
warning: commands will be executed using /usr/bin/sh
# crontab -l fifejj
* * * * * /bin/true
* * * * * /bin/false

Decay is inherent in all compounded things. Strive on with diligence
vind123
Regular Advisor

Re: Regarding crontab

su fcc -c "crontab /tmp/tempfile happens to work. Thanks a lot
if i do a crontab -r will it stop the cron under my unix user id? Is it a way to stop the cron?


Jonathan Fife
Honored Contributor

Re: Regarding crontab

crontab -r will remove the users crontab file, so it will effectively stop cron from doing anything for that user until a new crontab is put in place.

Note that the when you do a crontab -r, the crontab file will be removed completely, so if you'll need it again be sure to save it off first (eg. crontab -l user > /home/user/my.crontab)
Decay is inherent in all compounded things. Strive on with diligence
spex
Honored Contributor

Re: Regarding crontab

Hi,

Another way to effectively prevent cron from running as a particular user is to comment out every line of that user's crontab.

PCS
vind123
Regular Advisor

Re: Regarding crontab

I tried the below one in my machine as root and it's not working HP-UX 10.2. It's working in HP-UX 11.0

crontab -l fcc > /tmp/tempfile

So i changed this command to the below one
su fcc -c "crontab -l >/tmp/fcccron_old"

It seems to be working but i got some doubts
when i checked the /var/crontab/cron/fcc directory.

Before executing the "su fcc -c..." command the permission
happens to be below
-> cd /var/spool/cron/crontabs
-> ll
total 30
-r-------- 1 adm adm 535 Jan 4 2006 adm
-r-------- 1 informix informix 263 Jan 4 2006 informix
-r-------- 1 root fcc 19 Sep 15 2003 myd
-r-------- 1 root sys 1606 Apr 25 11:11 root
-r-------- 1 fcc fcc 9109 Jan 4 2006 fcc
-r-------- 1 root amotng 219 Oct 25 2005 uam40



I checked the fcc file under crontab directory a day after the command was run . I did see the permission of fcc file under /var/spool/cron/crontab directory. The permissions,owner and group is changed. I dontknow whether someone had changed manually the permissions and owner of fcc file or the command had changed it. dont know whether the fcc cron will run if the permission and owner group are like this.


ll
total 32
-r-------- 1 adm adm 535 Jan 4 2006 adm
-r-------- 1 informix informix 263 Jan 4 2006 informix
-r-------- 1 root fcc 19 Sep 15 2003 myd
-r-------- 1 root sys 1606 Apr 25 11:11 root
-rwxrwxrwx 1 fcc fcc 9228 Aug 23 02:42 fcc
-r-------- 1 root amotng 219 Oct 25 2005 uam40



The file permission ,owner are changed. Dont know how it got changed

rwxrwxrwx 1 fcc fcc 9228 Aug 23 02:42 fcc
Jonathan Fife
Honored Contributor

Re: Regarding crontab

How did you install the new crontab? If you used 'crontab file' or 'crontab < file' it should have kept the 100 permissions. Did you manually copy a file into the cron directory?
Decay is inherent in all compounded things. Strive on with diligence
vind123
Regular Advisor

Re: Regarding crontab

changing the crontab by this command from shell script (root id)

su fcc -c "crontab /tmp/fccmodcron"

I want to know if we do this whether the permissions,owner and group will change in
/var/spool/cron/crontabs/fcc
vind123
Regular Advisor

Re: Regarding crontab

Thanks a lot for the info