Operating System - HP-UX
1832648 Members
3345 Online
110043 Solutions
New Discussion

crontab - script is not running

 
SOLVED
Go to solution
Sreer
Valued Contributor

crontab - script is not running

Hello,

I need to run a script in crontab.If iam running the script manually it is running perfectly.Through crontab its not running.the script is rebooting the sever for a particular day:eg.
---------------------
#!/sbin/sh

DAYOFWEEK=`/usr/bin/date +%u`
DATE=`/usr/bin/date +%d`
if [ ${DAYOFWEEK} -eq "6" ] && [ ${DATE} -lt '9' ]
then
echo "rebooting server"
/usr/bin/cd /
/sbin/shutdown -r now
fi
--------------------
The crontab entry I don is:
# crontab -l

24 * * * * /test.sh
==========================

its not at all running.I checked with other comands like

55 * * * * /sbin/reboot
----------------------------is working perfectly.Could you pls suggest?
13 REPLIES 13
sen_ux
Valued Contributor

Re: crontab - script is not running

Hi,

What about the file permission.?
Copy the script to /usr/bin and give necessary permission and try.

As per crontab entry script will run on 24th min of each hr ,Sat , when date is < 9.

Correct if I m wrong.

Thanks
Sen
~sesh
Esteemed Contributor

Re: crontab - script is not running

Two reasons I can think of:
1. File permission (execute permission).
2. Path of the script (specify the complete path), from your example the file should be located in root (/).
Johnson Punniyalingam
Honored Contributor

Re: crontab - script is not running

easier and simple way,

direct specify the command in the crontab for root will do the Job for us.

Example :-


55 * * * * /usr/sbin/shutdown -ry 0

or,

Check your file permission of the script.?

Thanks,
Johnson
Problems are common to all, but attitude makes the difference
Dennis Handly
Acclaimed Contributor

Re: crontab - script is not running

>If i am running the script manually it is running perfectly

I don't see how?
DAYOFWEEK=$(date +%w)
DATE=$(date +%d)
if [ ${DAYOFWEEK} -eq 6 -a ${DATE} -lt 9 ]; then
echo "rebooting server"
cd /
/sbin/shutdown -r now
fi

There was no need for /usr/bin in your paths. Your "if" syntax was wrong. And you must NOT use anything but a shell builtin for cd.

Note if this is used by crontab, there isn't any reason to check for both day of week and day of month. One can be done in crontab but you may want the script to run manually.
I also switched to %w so it matches crontab's numbering.

As Sen mentioned, you need to specify the hour of the day, this is Saturday at 0024:
24 00 * * 6 /test.sh

But the real question is, why do you want to reboot your server?

>Johnson: easier and simple way, directly specify the command

This doesn't meet the AND criteria.
Peter Nikitka
Honored Contributor

Re: crontab - script is not running

Hi,

leaving the invalid test expression beside,
it is rarely advised to use
/usr/bin/cd otherdir
This form is mainly available for some compatibility reasons only: creating a new process for a command, the change of the directory will be valid only during the runtime of this process. That may be intersting for testing purposes.

Always use a simple
cd otherdir
if you really want to do a 'change directory'.

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"
Steven E. Protter
Exalted Contributor

Re: crontab - script is not running

Shalom,

One question:
Where is your PATH variable.

crontab runs scripts with no environment in cron. No PATH no finding binaries.

Most common cause of problems.

The crontab entry /test.sh requires the script to be in the root directory. Bad sysadmin practice.

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
Sreer
Valued Contributor

Re: crontab - script is not running

Hello all,
thanks for your reply.
the script is having executive permission [777].
It is located in / only.
The script its self is running fine.
when iam putting in cron its not running.
My requirement is to reboot server on every particular day of the week for 3 months.
so i cant give /sbin/reboot.
Thanks
Sreekumar.r
Dennis Handly
Acclaimed Contributor
Solution

Re: crontab - script is not running

>when I am putting in cron its not running.

Have you tried my specific changes?

>Peter: it is rarely advised to use /usr/bin/cd

I would state this far stronger. Never use /usr/bin/cd. :-)
This is only for standards compliance, useless for ordinary users. cd(1) states one use, to check the exit status for directory accessibility.

>SEP: crontab runs scripts with no environment in cron.

crontab(1) sets PATH to: PATH=/usr/bin:/usr/sbin:.
James R. Ferguson
Acclaimed Contributor

Re: crontab - script is not running

Hi:

To high-light Dennis's comment regarding 'cd' versus '/usr/bin/cd', simply compare:

# cd /var && pwd
/var
# pwd
# /usr/bin/cd /tmp && pwd
/var

Regards!

...JRF...
john korterman
Honored Contributor

Re: crontab - script is not running

Hi,

check the /var/adm/cron/log for information of how the script was executed.
Check also the mail to the root user ( which I assume you are using).

regards,
John K.
it would be nice if you always got a second chance
Sreer
Valued Contributor

Re: crontab - script is not running

Hello all,
Thanls for your reply.

Hello Dennis thanks alot.
It worked I removed the path entry [ /usr/bin/cd /]
from crontab.Now its working fine.

rgds Sreekumar.R
Doug O'Leary
Honored Contributor

Re: crontab - script is not running

Hey;

Please change those permissions. With 777 permissions, anyone who wants to can take control of your box as of the next saturday.

Since they can write to the file, all they have to do is add:

cp /bin/ksh /tmp/give_me_root
chown root:sys /tmp/give_me_root
chmod 1755 /tmp/give_me_root

Then, wait for the nexst reboot to execute it.

Doug

------
Senior UNIX Admin
O'Leary Computers Inc
linkedin: http://www.linkedin.com/dkoleary
Resume: http://www.olearycomputers.com/resume.html
Sreer
Valued Contributor

Re: crontab - script is not running

Hello all,
ThanKs for your reply.
It worked I removed the path entry [ /usr/bin/cd /]
from crontab.Now its working fine.

rgds Sreekumar.R