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!

Unix Script: ps won't work in cron 1

Status
Not open for further replies.

Guest_imported

New member
Jan 1, 1970
0
This script runs fine manually, but when it is run via cron the ps -ef does not capture any result. Please help.


#!/bin/ksh
echo `jobs`
((RunJava=`ps -ef | grep -c java`))
echo "Number of Java = ${RunJava}"

((RunHttp=`ps -ef | grep -c http`))
echo "Number of Http = ${RunHttp}"
 
Hi,
If you need a Tool or a script in your script it is best not to rely on PATH because other people may have different path settings and therefore your script appears to not work.

CRON is probably finding the wrong version of PS which expects -auxw instead of -ef

You should explicily give the full path name to those tools and scripts to avoid the confusion.

there are multiple PS executables on the box and explicitly stating you want

/usr/ucb/ps

vs

/usr/bin/ps

will ensure your script operate properly.

typically I have a source file which has all the defines I want in my scripts.

PS=/usr/bin/ps
SED=/usr/bin/sed
.
.
.

and at the top of all my scripts I simply

source /home/account/bin/scriptsetup

then I use

$PS and $SED and $whatever

whereever I need to.



 

hi,

it runs ok on command line, but not on cron, because there is no output file for the cron. try this.

#!/bin/ksh
echo `jobs` > /tmp/ps.output
((RunJava=`ps -ef | grep -c java`))
echo "Number of Java = ${RunJava}" >> /tmp/ps.output

((RunHttp=`ps -ef | grep -c http`))
echo "Number of Http = ${RunHttp}" >> /tmp/ps.output

good luck...
 
tdatgod hit it on the head. cron does not use the PATH variable. So unless you specify exactly which executable to use, nothing will run.

As far as the output goes, I get email for all cron jobs that have unspecified output. I don't know if this is OS dependant or not. But it is best to control what file you want to use as the output of your cron, either in the crontab declaration:
Code:
MM HH dd mm ww /path/script >> /path/output.log 2>&1
or as shadow_eye mentioned in the script itself.

Good luck Einstein47
(Love is like PI - natural, irrational, endless, and very important.)
 
Thanks a bunch Guys.....

The key problem was the PATH, I know about the redirect of output and I was calling this script from another script so that was not the issue.

Its working fine now with the complete path in the code.
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top