|
|
|
|
google site
|
|||||||
| Forums | Register | Blog | Man Pages | Forum Rules | Links | Albums | FAQ | Users | Calendar | Search | Today's Posts | Mark Forums Read |
| Shell Programming and Scripting Post questions about KSH, CSH, SH, BASH, PERL, PHP, SED, AWK and OTHER shell scripts and shell scripting languages here. |
![]() |
|
|
Thread Tools | Search this Thread | Rate Thread | Display Modes |
|
|||
|
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 | ||
|
|
|
|||
|
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 0Another (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 |
|
|||
|
Another minor observation. This says run your script every hour on the half hour.
|
|
|||
|
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. |
|
|||
|
If your system supports it; otherwise 0,30.
|
| Sponsored Links | ||
|
|
![]() |
| Bookmarks |
| Thread Tools | Search this Thread |
| Display Modes | Rate This Thread |
|
|
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 |