Visit Our UNIX and Linux User Community


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


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting Can run a ".sh" script as the user but not from the crontab
# 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 08: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
This User Gave Thanks to bakunin For This Post:
# 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.

Previous Thread | Next Thread
Test Your Knowledge in Computers #492
Difficulty: Medium
Transpilers are source-to-source compilers that directly produce assembly or machine code,
True or False?

10 More Discussions You Might Find Interesting

1. UNIX for Beginners Questions & Answers

How to run root level command , if user has "su -" permission in sudoers provided?

I am looking t run root level command on multiple servers, but all servers have only "su - " permission available in sudoers. please help me if any way that I can run command using help of "su -" My script for hosts in `cat hosts.txt`; do echo "###########################Server Name-... (5 Replies)
Discussion started by: yash_message
5 Replies

2. Shell Programming and Scripting

Bash script - Print an ascii file using specific font "Latin Modern Mono 12" "regular" "9"

Hello. System : opensuse leap 42.3 I have a bash script that build a text file. I would like the last command doing : print_cmd -o page-left=43 -o page-right=22 -o page-top=28 -o page-bottom=43 -o font=LatinModernMono12:regular:9 some_file.txt where : print_cmd ::= some printing... (1 Reply)
Discussion started by: jcdole
1 Replies

3. UNIX for Dummies Questions & Answers

What is the significance of sh -s in ssh -qtt ${user}@${host} "sh -s "${version}"" < test.sh?

Please can you help me understand the significance of providing arguments under sh -s in > ssh -qtt ${user}@${host} "sh -s "${version}"" < test.sh (4 Replies)
Discussion started by: Sree10
4 Replies

4. Shell Programming and Scripting

Shell script using expect to login to couple of remote servers and read "crontab -l"

I need a shell script using expect to login to couple of remote servers and read "crontab -l -u <username>" & "cat /etc/rc.local" & "df -h" and able to create output into a file saved locally with hostname.crontab & hostname.rc.local & disk.status. I can supply a file as list of hostname or IP... (4 Replies)
Discussion started by: jaipsharma
4 Replies

5. Shell Programming and Scripting

Crontab not running "nail" in script

the script is in Perl... this is one part of it: $command = "echo \"$text\" | /usr/bin/nail -s \"My $text1\" $ccstr$addstr"; system("$command") if length($bodytext)>1; crontab runs the script and sends me notifications but i cant receive any mail that i wanted! I'm using the complete path... (1 Reply)
Discussion started by: yeean
1 Replies

6. UNIX for Dummies Questions & Answers

crontab does not run "root" job

hi, I've read different posts regarding crontab but none helped out...the shell scrip that I want to run through crontab gets run through crontab when I use the following crontab statement: 13 17 * * * /usr/net/gcc/DBdrop.sh > /usr/net/gcc/DBdrop.log 2>&1 but it does not run when I scheduel... (2 Replies)
Discussion started by: linux0004
2 Replies

7. UNIX for Dummies Questions & Answers

Possible to give non root user sudo to "crontab -l"

Does anyone know if this is possible? I want to give some users access to root's crontab but only with a read privilege. Is this possible to do or can only root or people with full root sudo view root's cron? (4 Replies)
Discussion started by: LordJezoX
4 Replies

8. Shell Programming and Scripting

catalina.sh : need combination from "start" and "run"

heya, can someone help me with following problem. i am not sure how far you know the catalina.sh script from tomcat. when i start my tomcat with "catalina.sh run" then the startup-process-output will be printed out on the console, but the tomcat process is started in current shell/session, so... (1 Reply)
Discussion started by: Filly
1 Replies

9. Shell Programming and Scripting

communicating wth another user aside from "wall" and "write"

Hi, Can anyone suggest a Unix command or c-shell algorithm to simulate to behavior of "wall" command minus the "all users"? What I'm trying to do is to send a notice to just one particular user but i dont want other remotely-logged-on users to receive the message (on the pseudo-terminals). I... (6 Replies)
Discussion started by: Deanne
6 Replies

10. UNIX for Dummies Questions & Answers

Run away "bootpgw" & "inetd"

Hello All. I'm get the following messages posted to the /var/adm/syslog file ever second and not sure on how to stop the process. May 14 15:50:52 a3360 bootpgw: version 2.3.5 May 14 15:50:52 a3360 inetd: /etc/bootpgw exit 0x1 As said about this gets logged every second only thing that... (4 Replies)
Discussion started by: cfaiman
4 Replies

Featured Tech Videos