Shell script not getting called through cron job but executes fine manually.


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting Shell script not getting called through cron job but executes fine manually.
# 1  
Old 07-03-2012
Shell script not getting called through cron job but executes fine manually.

Hi,
My shell script not getting called through cron job.
The same works fine when executed manually.

I tried to generate logs to find if the scripts has some errors related to path using following command- trying to execute .sh file every 5 mins:
Code:
*/5 * * * * /home/myfolder/abc.sh /home/myfolder/abc.log 2>&1

But even the logs are not getting generated at the mentioned path.

The shell script is located in the folder root/home/myfolder.

Can any one help me idenfy the issue and its resolution.

Any help/suggestion would be greatly appreciated.

Last edited by methyl; 07-03-2012 at 07:41 PM.. Reason: please use code tags
# 2  
Old 07-03-2012
This question is so common its in our FAQ.

your PATH variable probably isn't set right. Cron executes with a very minimal PATH and therefore won't find many common shell commands.

Either . /etc/profile at the beginning of your script or set a proper PATH by hand.
# 3  
Old 07-03-2012
Dear Corona688,
do you mean that if the location of my .sh file is
Code:
root -> home-> myfolder-> abc.sh

than i shd mention :
Code:
*/5 * * * * ./home/myfolder/abc.sh

Am i right?

Last edited by methyl; 07-03-2012 at 08:34 PM.. Reason: code tags
# 4  
Old 07-03-2012
No. I mean the PATH variable, in your script. It defines where to look for commands.

Cron gives a very minimal PATH to programs it runs. It often doesn't even include /usr/bin.

Either set the PATH variable yourself, inside the script, or . /etc/profile inside the script to get a better PATH.
# 5  
Old 07-03-2012
sorry to bother u again, but am new to shell scripts & croning so would need more help from you:

the content of my script is :

Code:
#!/bin/sh
HOST='xxx.xx.xx.xx'
USER='xx'
PASSWD='xxx'
FILE='*.gz'
 
 
echo "
 
gzip *.dmp
ftp -n $HOST <<END_SCRIPT
quote USER $USER
quote PASS $PASSWD
mput $FILE 
quit
END_SCRIPT
 
cd \my_ARCHIVE
rm $FILE
cd ..
mv $FILE \my_ARCHIVE
"> ftp -n > ftp_$$.log
 
exit 0

can u suggest wht exactly needs to be updated in my .sh file so that it gets executed by cron job.

Also have a query- if the issue is related to path...at least error log shd be generated, even thats not happening.

Thanks in advance

Last edited by methyl; 07-03-2012 at 07:41 PM.. Reason: pleese use code tags
# 6  
Old 07-03-2012
Please post what Operating System and version you are running and what Shell you normally use (regardless of the default Shell for you cron).

If there is something fundamental wrong, the error message will be found in unix mail for the owner of the crontab.
A common issue would be if /home/myfolder/abc.sh was not executable by the owner of the cron.

Ps. The script as posted in post #3 contains many syntax/design? errors and there is no way that it works with or without cron. Please outline what the script is intended to do because deducing this from faulty code is impossible.

Pps. Please post in proper English not textspeak.

Ppps. The script in the crontab line as posted the script takes a parameter. Nowhere in the the posted script is the parameter referenced. Hmm, perhaps the crontab line is wrong.
Perhaps the crontab line should be something like this:
Code:
*/5 * * * * /home/myfolder/abc.sh 2>&1 >/home/myfolder/abc.log

That should explain your missing logfile.


I think that an important question is where precisely in the directory tree for your server is the directory my_ARCHIVE? Please be aware that the \ character in unix protects a character from interpretation by the Shell whereas a / character could be part of a path describing a directory tree. Microsoft Operating systems use \ characters in path names,but unix Operating Systems use / in path names.

Last edited by methyl; 07-03-2012 at 08:58 PM..
# 7  
Old 07-04-2012
After the #!/bin/sh line, do

. /etc/profile
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

Shell Script has different ouput via cron vs when run Manually

Hello Lads, I deployed a script on my mac to start and stop EC2 instances on AWS console. The script when started manually on the terminal does the expected stop and start. Problem is when i try to schedule it on a cron, it fails to recognize the AWS Keys which i set up as ENV variable by... (2 Replies)
Discussion started by: Irishboy24
2 Replies

2. Shell Programming and Scripting

Calling bash script works when called manually but not via Cron?

Hi, I've got a Bash backup script I'm trying to run on a directory via a cron job nightly. If I ssh in and run the script manually it works flawlessly. If I set up the cron to run evertything is totally messed up I don't even know where to begin. Basically the path structure is ... (6 Replies)
Discussion started by: wyclef
6 Replies

3. 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

4. Shell Programming and Scripting

Part of the Shell script is not running via crontab, runs fine manually

Hello Team, As a part of my job we have made a script to automate a service to restart frequently. Script having two functions when executing it's should find the existing service and kill it, then start the same service . Verified the script it's working fine when executing... (18 Replies)
Discussion started by: gowthamakanthan
18 Replies

5. Shell Programming and Scripting

Script not working in cron but working fine manually

Help. My script is working fine when executed manually but the cron seems not to catch up the command when registered. The script is as follow: #!/bin/sh for file in file_1.txt file_2.txt file_3.txt do awk '{ print "0" }' $file > tmp.tmp mv tmp.tmp $file done And the cron... (2 Replies)
Discussion started by: jasperux
2 Replies

6. Shell Programming and Scripting

Script runs fine manually but not in crontab

Hello Guys, I have scratched my head alot on this but couldn't find clue what's wrong. Can you please help me with this? My problem is as following. 1) When I manually execute following script it runs successfully with below output. bash-3.00# more smssend #!/bin/bash echo -e "<Request... (16 Replies)
Discussion started by: umarsatti
16 Replies

7. Shell Programming and Scripting

Getting issue while running it from cron while manually working fine

Hello, I am working one one script where I am using the below code which is using to connect with MKS client when I run my script manually it works effiecently i.e. it connects with MKS client but when I run it from CRON it doesn't connect. 1)Can some one tell when it is running from cron... (1 Reply)
Discussion started by: anuragpgtgerman
1 Replies

8. AIX

Script not getting executed via cron but executes when executed manually.

Hi Script not getting executed via cron but executes successfully when executed manually. Please assist cbspsap01(appuser) /app/scripts > cat restart.sh #!/bin/ksh cd /app/bin date >>logfile.out echo "Restart has been started....." >>logfile.out date >>logfile.out initfnsw -y restart... (3 Replies)
Discussion started by: samsungsamsung
3 Replies

9. Shell Programming and Scripting

Cron job fails, but works fine from command line

I have a very basic script that essentially sends a log file, via FTP, to a backup server. My cron entry to run this every night is: 55 23 * * * /usr/bin/archive_logs The script runs perfectly when executed manually, and actually worked via cron for about three weeks. However, it mysteriously... (3 Replies)
Discussion started by: cdunavent
3 Replies

10. Shell Programming and Scripting

How to determine if a script (perl) was called from a CRON job or commandline

Hi, Is there a way to determine if a Script is called from a CRON job or from a commandline Gerry. (2 Replies)
Discussion started by: jerryMcguire
2 Replies
Login or Register to Ask a Question