script execution differs in cron


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting script execution differs in cron
# 1  
Old 07-15-2009
Data script execution differs in cron

I have the following script (MyScript):

#!/bin/sh
ps U erv | grep -v grep | grep -F "/usr/bin/collect -o 101"
echo "Result: $?"

When executed from the command line, I get
... (the line containing /usr/bin/collect -o 101)
Result: 0
(which is correct since collect is running)

When I put the script in cron with:
*/10 * * * * ./MyScript >>log.log

in log.log I get
Result: 1

What is wrong?
I tried to put the complete path for ps and grep, but I had the same result.
Could be because the command displayed by ps is truncated? How can I display it all?

Please help!
# 2  
Old 07-15-2009
You need to specify the full path name in cron.

Quote:
./MyScript >>log.log
e.g.

Code:
/home/myuser/MyScript >>log.log

Tip: The environment in cron is limited. Try a creating a cron containing just an "env" command and a "pwd" command and then compare the output with what you get if you type those commands when logged in as yourself. The "." in "./MyScript >>log.log" refers to your current directory which will no doubt be different from the current directory when running from cron.

BTW. Is your ps command syntactically correct? May be a typo in the post?

Last edited by methyl; 07-15-2009 at 07:40 PM.. Reason: ps command?
# 3  
Old 07-15-2009
??

who would the file contain the result of echo "Result: $?" then? it shouldnt have anything in the log if it failed, the failure was from within the script.

---------- Post updated at 05:46 PM ---------- Previous update was at 05:45 PM ----------

try

#!/bin/sh
/bin/ps U erv | /bin/grep -v grep | /bin/grep -F "/usr/bin/collect -o 101"
echo "Result: $?"
# 4  
Old 07-15-2009
I can reproduce the error response from a shell prompt on a system which is not running anything called "collect":

Code:
#!/bin/sh
ps U erv | grep -v grep | grep -F "/usr/bin/collect -o 101"
echo "Result: $?"
Result: 1

The response is coming from the "grep -F" statement. On my system the parameter "U" is ignored. If "erv" is a username maybe the command should be "ps -fu erv" ? The "ps" command as posted will not produce a wide output containing command parameters.

In other words the "grep -F" is not finding the matching string because the "ps" statement is not outputting that string.

Last edited by methyl; 07-15-2009 at 08:21 PM.. Reason: In other words...
# 5  
Old 07-17-2009
Yes, the ps command truncates the output. I added the option -w (ps -w -U erv |...)and it seems working.
Thank you.
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. UNIX for Beginners Questions & Answers

Execution problem with Cron

Hi, I'm unable to understand why the grep command in cronjob works intermittently Expected output: Grep command to look for a particular string with today's date and exit from infinite while loop else it must sleep for 5 mins I'm using the following statement in my shell script, the... (9 Replies)
Discussion started by: charlie87
9 Replies

2. UNIX for Dummies Questions & Answers

Execution problem with Cron: Script works manually but not w/Cron. Why?

Hello gurus, I am making what I think is a simple db2 call from within a shell script but I am having difficulty producing the desired report when I run the script shown below from a shell script in cron. For example, my script and the crontab file setup is shown below: #!/bin/ksh db2... (3 Replies)
Discussion started by: okonita
3 Replies

3. Shell Programming and Scripting

Execution problems with cron

I am new to creating crontab file , i just wrote below (40 19 * 3 * /root/maths/practisecron.sh), the script just prints "Hi". When ever i save the above file i am getting this . # crontab -e crontab: installing new crontab Can you please check where it went wrong.. (1 Reply)
Discussion started by: vkiyv05
1 Replies

4. Shell Programming and Scripting

Output differs when run manually and when cron job executes it

I get a different output when i manually run the .sh script and when it is run by a cron job. Please help me .. TMP1="/lhome/bbuser/script/wslog/sar.t1" TMP2="/lhome/bbuser/script/wslog/sar.t2" TMP3="/lhome/bbuser/script/wslog/sar.t3" OUTPUT="/lhome/bbuser/script/wslog/sar.out"... (8 Replies)
Discussion started by: nithinankam
8 Replies

5. Shell Programming and Scripting

Output of echo $$ differs in Script than console

Hi All, I have a very basic query for 'echo $$' command. When I am executing echo $$ on console then it is giving different output than the execution of echo $$ in a script. Console Output: ------------------------- bash-3.2$ echo $$ 15610 bash-3.2$ In Script:... (3 Replies)
Discussion started by: manishdivs
3 Replies

6. Programming

execution problems with cron

how to store a date into file? and how we can access date from the file? ---------- Post updated at 06:09 AM ---------- Previous update was at 06:08 AM ---------- how we can store date in file? (1 Reply)
Discussion started by: causalmodi777
1 Replies

7. UNIX for Advanced & Expert Users

PPID differs in script and prompt

I tried several times to get answer to the below problem. Someone can please help me? $ cat p1.sh #!/bin/sh `./c1.sh &` while # indefinite loop do x=5; done $ cat c1.sh # sleep for 10 sec and exit #!/bin/sh sleep 10; Execute P1 as ./p1 & $ ps -eaf | grep c1... (1 Reply)
Discussion started by: alexalex1
1 Replies

8. UNIX for Dummies Questions & Answers

Mac OSX Cron Script Execution

Hello, On Mac OSX, I was wondering about my Cron Script: HELL=/bin/tcsh PATH=/sbin:/bin:/usr/sbin:/usr/bin HOME=/var/log MAILTO=jwillis 25 1 * * * root /Users/jwillis/Fbcmd\Scripts/DailyBirthday.scrmy returned message is: Subject: Cron... (3 Replies)
Discussion started by: jwillis0720
3 Replies

9. Shell Programming and Scripting

Cron execution of shell script

Hi Guys, Unable to run this script from the cron,although the same executes perfectly from the command line.Please help. #!/bin/sh #### aprintd alarm creation files ##### file=`date +%m%d%Y` pid=$$ echo "$pid" /u01/app/netboss/bin/aprintd/aprintd > $file & childpid=$!... (3 Replies)
Discussion started by: ashish.sharma
3 Replies

10. Solaris

cron execution

Hi, Here is the code snippet when executing on the command line it does execute smoothly, but while i place this on cron it doesnt run. Can anybody suggest where the problem in cron is.? #!/usr/bin/ksh home_dir="/export/home/scripts/MonitorScripts" ThresholdLimit=5 touch... (6 Replies)
Discussion started by: rameek20
6 Replies
Login or Register to Ask a Question