Home Man
Search
Today's Posts
Register

BSD, Linux, and UNIX shell scripting — Post awk, bash, csh, ksh, perl, php, python, sed, sh, shell scripts, and other shell scripting languages questions here.

Can run a ".sh" script as the user but not from the crontab

Tags
shell scripts

Login to Reply

 
Thread Tools Search this Thread
# 1  
Old 09-08-2009
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 07:51 PM..
# 2  
Old 09-08-2009
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
The Following User Says Thank You to bakunin For This Useful Post:
RavinderSingh13 (11-18-2015)
# 3  
Old 09-08-2009
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  
Old 09-08-2009
Quote:
Originally Posted by bhaire
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  
Old 09-09-2009
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  
Old 09-09-2009
Quote:
Originally Posted by Vi-Curious
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  
Old 09-09-2009
Quote:
Originally Posted by thegeek
So you should change it like */30 instead of just 30 to mean every 30 minutes.
If your system supports it; otherwise 0,30.
Login to Reply

« Previous Thread | Next Thread »
Thread Tools Search this Thread
Search this Thread:

Advanced Search
Display Modes

More UNIX and Linux Forum Topics You Might Find Helpful
Thread Thread Starter Forum Replies Last Post
Root running a script calling to scp using user "xyz" is not authenticating! denissi Shell Programming and Scripting 0 10-21-2014 07:02 PM
Using "mailx" command to read "to" and "cc" email addreses from input file asjaiswal UNIX for Dummies Questions & Answers 2 10-11-2014 09:32 PM
"Help with bash script" - "License Server and Patch Updates" profileuser UNIX for Dummies Questions & Answers 25 04-01-2014 02:40 PM
What is the significance of sh -s in ssh -qtt ${user}@${host} "sh -s "${version}"" < test.sh? Sree10 UNIX for Dummies Questions & Answers 4 08-16-2013 06:28 PM
Shell script using expect to login to couple of remote servers and read "crontab -l" jaipsharma Shell Programming and Scripting 4 03-20-2013 03:26 PM
Crontab not running "nail" in script yeean Shell Programming and Scripting 1 06-16-2011 11:15 AM
awk command to replace ";" with "|" and ""|" at diferent places in line of file shis100 Shell Programming and Scripting 7 03-16-2011 08:59 AM
Possible to give non root user sudo to "crontab -l" LordJezoX UNIX for Dummies Questions & Answers 4 10-10-2008 02:02 PM
communicating wth another user aside from "wall" and "write" Deanne Shell Programming and Scripting 6 05-11-2007 05:43 AM


All times are GMT -4. The time now is 07:31 PM.

Unix & Linux Forums Content Copyright©1993-2018. All Rights Reserved.
UNIX.COM Login
Username:
Password:  
Show Password