We help IT Professionals succeed at work.

scripts runs correctly on command line but not as cronjob

duficy
duficy asked
on
The following script will run completely from a command prompt. However, I have a problem when running it as a cronjob. The job is executed, since I get the email however, "/etc/init.d/radiator restart > /dev/null 2>&1" is not run. I'm using debian 2.2, I tried adding -u root without success.


>>>>>>>>>>>>>>>>>>>>>>>>>>.


#!/bin/sh

HOSTNAME=`hostname`
ADMIN="emergency@cshore.com"


/bin/ps axwwww | grep radiusd | grep -v "grep" > /dev/null 2>&1

if [ $? -eq 0 ]; then
        :
else
        /etc/init.d/radiator restart > /dev/null 2>&1
        echo "Radius restarted on Mozart" | Mail -s "Radius for ${HOSTNAME} rest
arted!" ${ADMIN}
fi


Comment
Watch Question

Did you try 'start'?  If it's not running, then 'restart' may not apply.

Author

Commented:
>>Did you try 'start'?  If it's not running, hen 'restart' may not apply.


Yes. I have doubled points to 200.
Then the only thing I can think of is that there's something missing from the PATH or some other environment problem when it runs from cron.

Author

Commented:
I have specified the path

"/etc/init.d/radiator restart > /dev/null 2>&1"
Yes, but the radiator script may need some PATH info to find the binaries it's supposed to execute, or there may be some other environment stuff missing.

Try running the script by hand in a shell with a stripped down environment (and without redirecting stdout and stderr).

Commented:
would it be possible if you could post the whole script? Also under which account have you installed the cronjob and which account did you test it?

Commented:
first, take out the > /dev/null 2>&1 so that you can see the errors that are actually occuring... then try changing Mail to mail (or better yet, /bin/mail).

Commented:
Hi.
Since I don't use debian I'm not sure about this, but the line "/bin/ps axwwww | grep radiusd | grep -v "grep" > /dev/null 2>&1"
Shouldn't it be "/bin/ps -axwwww | grep radiusd | grep -v "grep" > /dev/null 2>&1".
It looks like bsd-ps and I don't believe that the third "w" does anything.

Hope this helps.
BRONZE EXPERT

Commented:
does /etc/profile or $HOME/.profile of the user running this script contain any tty-related commands, like stty?
Also check /etc/init.d/radiator or such bogus commands.
If the cron fails to execute a command, it sends mail too root, what does this mail tell you?
Make sure that:
1. If you point the cronttab file to the right script.
2. Any file been used in your script should not be a symbol link. If so, find the origin file and use that name.
3. Is your crond daemon program worked well? :)


Good Luck:)
BRONZE EXPERT

Commented:
> Any file been used in your script should not be a symbol link.

Why that?
In Redhat, you can put the script in directory:/etc/cron.daily(hourly | weekly) then, the daemon program will run it after very xxxx times, in these directorys if you creat a script by command "ln -s" it will not be run at all.
BRONZE EXPERT

Commented:
.. that's not a cron behaviour, but one of the script called by cron (or at) calling etc/cron.{daily,hourly,weekly}
Not sure if you have the "service" command on debian, but if so, you could try "service radiator restart" instead.
Julian ParkerSenior Systems Administrator
BRONZE EXPERT

Commented:
Not sure if you have done this but specify the full path to all the commands;

eg/
/bin/ps axwwww | /bin/grep radiusd | /bin/grep -v "grep" > /dev/null 2>&1

You will have to find the correct paths for your system and may have to include the paths for the mail proggy as well.

jools

Commented:
No comment has been added lately, so it's time to clean up this TA.
I will leave a recommendation in the Cleanup topic area to:
Accept chris_calabrese's answer
Please leave any comments here within the next seven days.
 
PLEASE DO NOT ACCEPT THIS COMMENT AS AN ANSWER!
 
majorwoo
EE Cleanup Volunteer
per recommendation

SpideyMod
Community Support Moderator @Experts Exchange

Explore More ContentExplore courses, solutions, and other research materials related to this topic.