Go Back   The UNIX and Linux Forums > Top Forums > Shell Programming and Scripting
.
google site



Shell Programming and Scripting Post questions about KSH, CSH, SH, BASH, PERL, PHP, SED, AWK and OTHER shell scripts and shell scripting languages here.

Closed Thread
English Japanese Spanish French German Portuguese Italian Powered by Powered by Google
 
Thread Tools Search this Thread Rate Thread Display Modes
  #1 (permalink)  
Old 09-08-2009
Registered User
 

Join Date: Sep 2009
Posts: 8
Can run a ".sh" script as the user but not from the crontab

Hello Everyone,

I am logged in as me. I created a script in the directory "/install/new" called "script1.sh" which basically runs another script "runapp.sh" . The "runapp.sh" is a vendor provided application strart up script that came with the installation. This is also in the same directory as "script1.sh". Briefly, my "script1.sh" looks like this:


Code:
#!/bin/ksh
m=`ps -ef | grep /install/new/application | wc -l`
if [ $m != 2 ]; then
echo "starting application"
./runapp.sh
else
echo "no action taken";
fi

I have put this script in Cron Job and I edited and saved the Crontab as the same user account that created the "script1.sh".

Here is what my crontab looks like:

Code:
$crontab -e

30 * * * * /install/new/script1.sh >> /install/new/log.txt 2>&1

Which is saying run my script every 30 minutes.

Here's the deal- When I test my script "script1.sh" running it from the putty terminal by doing ./script1.sh it runs successfully however when this is run from the Crontab it complains saying this in the "log.txt"


Code:
$more log.txt
starting application
/install/new/script1.sh [5]: ./runapp.sh:  not found

What am I doing wrong? Any suggestions will be greatly appreciated.

Thanks in Advance

Last edited by bhaire; 09-08-2009 at 08:51 PM..
Sponsored Links
  #2 (permalink)  
Old 09-08-2009
bakunin bakunin is offline Forum Staff  
Bughunter Extraordinaire
 

Join Date: May 2005
Location: In the leftmost byte of /dev/kmem
Posts: 1,670
You are falling for "scripting error number 1": you depend on an environment, which cannot be taken for granted.

Long form: a construct like "./runapp.sh" is telling the shell to execute a program located in the CURRENT directory (the "./" part of the path). This might be the case if you are logged in as your user and manually changed to this directory before. This is NOT the case when cron tries to execute your script, because cron doesn't know that it should manually change into a certain directory first.

Avoid relative paths (everything starting with a "./" in scripts.

Additionally i wonder why the rest of the script worked at all. A command like "ps" is basically an executable and probably located in "/usr/bin/ps". Without a PATH variable pointing to /usr/bin ps shouldn't be found at all. This is also relying on an environment which is probably not set.

In scripts you better set ALWAYS your own environment, because this way you will never encounter problems like these. Here is a sketch of what i mean:


Code:
#!/bin/ksh
# Example script

# ----------------- global environment
PATH=/usr/bin:/usr/sbin:<and whatever you need>
export PATH
TERM=vt100
export TERM
# ..... similar for whatever you might also need in your script .....

# -----------------  constants
BINDIR=/path/to/my/scripts
# ..... similar for whatever you might also need in your script .....

# -----------------  script code itself
m=`ps -ef | grep /install/new/application | wc -l`
if [ $m != 2 ] ; then
     echo "starting application"
     $BINDIR/runapp.sh
else
     echo "no action taken"
fi

exit 0

Another (minor) observation: if you use ksh do NOT use "echo" for output. There is the built-in "print" command for that, whereas "echo" is an external command.

I hope this helps.

bakunin
  #3 (permalink)  
Old 09-08-2009
Registered User
 

Join Date: Jul 2009
Posts: 174
Just add the full path in your script. Change ./runapp.sh to

Code:
/install/new/runapp.sh

Also, if required make the script executable.
Once you see results, then do to the advanced advice given above.
  #4 (permalink)  
Old 09-08-2009
Registered User
 

Join Date: Jul 2008
Location: Texas
Posts: 140
Quote:
Originally Posted by bhaire View Post
Hello Everyone,
Here is what my crontab looks like:

Code:
$crontab -e
 
30 * * * * /install/new/script1.sh >> /install/new/log.txt 2>&1

Which is saying run my script every 30 minutes.
Another minor observation. This says run your script every hour on the half hour.
  #5 (permalink)  
Old 09-09-2009
Registered User
 

Join Date: Mar 2008
Posts: 1,639
A couple of points.


Code:
ksh
type echo
echo is a shell builtin

To find out the environment while running ksh from cron, create a one-off script to run once from cron containing the following lines. Output is in root mail or (mail for the owner if this is not a root cron).


Code:
#!/bin/ksh
echo "==="
echo "pwd"
echo "==="
pwd
echo "==="
echo "env"
echo "==="
env
echo "==="
echo "set"
echo "==="
set
echo "===="
echo "date"
echo "===="
date
echo "====="
echo "umask"
echo "====="
umask


Now try running the same script from a login prompt and from an "at" job. This should help decide what environment to build into your main script so that it will run from the command line, "at" and "cron".

I would expect "ps" to be available from cron's limited PATH.
  #6 (permalink)  
Old 09-09-2009
thegeek thegeek is offline Forum Advisor  
Registered User
 

Join Date: Apr 2009
Location: /usr/bin/vim
Posts: 657
Quote:
Originally Posted by Vi-Curious View Post
Another minor observation. This says run your script every hour on the half hour.
So you should change it like */30 instead of just 30 to mean every 30 minutes.
  #7 (permalink)  
Old 09-09-2009
Registered User
 

Join Date: Jul 2008
Location: Texas
Posts: 140
Quote:
Originally Posted by thegeek View Post
So you should change it like */30 instead of just 30 to mean every 30 minutes.
If your system supports it; otherwise 0,30.
Sponsored Links
Closed Thread

Bookmarks

Thread Tools Search this Thread
Search this Thread:

Advanced Search
Display Modes Rate This Thread
Rate This Thread:

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


More UNIX and Linux Forum Topics You Might Find Helpful
Thread Thread Starter Forum Replies Last Post
"too big" and "not enough memory" errors in shell script jerardfjay AIX 11 03-16-2009 11:09 PM
script running with "ksh" dumping core but not with "sh" simhe02 HP-UX 9 11-04-2008 08:52 PM
Possible to give non root user sudo to "crontab -l" LordJezoX UNIX for Dummies Questions & Answers 4 10-10-2008 03:02 PM
Explain the line "mn_code=`env|grep "..mn"|awk -F"=" '{print $2}'`" Lokesha UNIX for Dummies Questions & Answers 4 12-20-2007 01:52 AM
communicating wth another user aside from "wall" and "write" Deanne Shell Programming and Scripting 6 05-11-2007 06:43 AM



All times are GMT -4. The time now is 12:18 AM.


Powered by: vBulletin, Copyright ©2000 - 2006, Jelsoft Enterprises Limited. Language Translations Powered by .
vBCredits v1.4 Copyright ©2007 - 2008, PixelFX Studios
The UNIX and Linux Forums Content Copyright ©1993-2010. All Rights Reserved.Ad Management by RedTyger

Content Relevant URLs by vBSEO 3.2.0