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.
# 8  
Old 07-04-2012
Thanks Methyl,
I used the following command
Code:
*/5 * * * * /home/myfolder/abc.sh 2>&1 >/home/myfolder/abc.log

for croning the script and now a blank abc.log file is getting generated at the path home/myfolder and the time stamp of the blank abc.log file also gets updated after every 5 minutes. But seems the shell script is still not getting executed.

-> the script is written to transfer .dmp file from unix server to a windows OS server using ftp. the script first zips the .dmp file and than transfers it.
-> when logging to the unix system through putty and executing the following command manually:
sh abc.sh
the script executes properly and the file gets transferred to the home directory of the apache server installed in the windows system.

-> I have been granted a user account in this unix server. 'myfolder' is the folder by my name located at the path: root/home/myfolder.
-> I have placed the script 'abc.sh' in this 'myfolder' as i have access to only this folder and i can't access any other folder in the system. The .dmp file is also located in the same folder as the script.
The blank abc.log file is generated at the same folder.
-> The 'my_ARCHIVE' folder is created in the folder 'myfolder'. The script after copying the file to the windows system should move the file to this 'my_ARCHIVE' folder.

Question: Is this issue related to access and permissions?
I am the owner of the crontab and the crontab is expected to execute the shell script which is present in the folder where i have read-write access.
Is it that the cron job is not able to access the script? If thats the case, how can it be confirmed and what permissions need to be granted to me for the same?

Thanks.

---------- Post updated at 05:56 PM ---------- Previous update was at 05:40 PM ----------

Hi Corona688,
I entered the profile path as mentioned below but still the cron job not able to execute the shell script.
Code:
#!/bin/sh
. /etc/profile

Any help would be appreciated.

Last edited by methyl; 07-04-2012 at 07:37 PM.. Reason: please use code tags
# 9  
Old 07-04-2012
Quote:
Originally Posted by Dejavu20
Thanks Methyl,
I used the following command
Code:
*/5 * * * * /home/myfolder/abc.sh 2>&1 >/home/myfolder/abc.log

for croning the script and now a blank abc.log file is getting generated at the path home/myfolder and the time stamp of the blank abc.log file also gets updated after every 5 minutes. But seems the shell script is still not getting executed.
If it's generating abc.log, it is running.

As for why it's not working, post the contents of the script so we can see.
# 10  
Old 07-04-2012
Quote:
for croning the script and now a blank abc.log file is getting generated at the path home/myfolder and the time stamp of the blank abc.log file also gets updated after every 5 minutes. But seems the shell script is still not getting executed.
Clearly the shell script is being executed.
# 11  
Old 07-04-2012
Please post:
# From the local server (blotting anything confidential with X's).
Code:
id
ls -lad /home/myfolder/abc.sh

cat /home/myfolder/abc.sh

root/home/myfolder is not an absolute path. Where is the directory in the filesystem?


Please post what Operating System and version you are running and what Shell this is, blotting anything confidential like computer names and licences with X's.
Code:
uname -a
echo $SHELL


Last edited by methyl; 07-04-2012 at 08:06 PM..
# 12  
Old 07-04-2012
Hi Methyl, Corona,
PFB the details:


Code:
Output for ls -lad /home/myfolder/abc.sh :
 
-rw-rw-rw- 1 myname myname 296 Jul 4 19:06 /home/myname/abc.sh

Moderator's Comments:
Mod Comment Are you sure about the above?

Code:
Output for cat /home/myfolder/abc.sh :
 
#!/bin/sh
. /etc/profile
 
HOST='XXX.XXX.XX.XXX'
USER='XXX'
PASSWD='XXX'
FILE='*.gz'
 
echo "
 
gzip *.dmp
ftp -n $HOST <<END_SCRIPT
quote USER $USER
quote PASS $PASSWD
mput $FILE
quit
END_SCRIPT
 
cd /ARCHIVE_FOLDER
rm $FILE
cd ..
mv $FILE /ARCHIVE_FOLDER
"> ftp -n > ftp_$$.log
 
exit 0

----------------------------------
Code:
uname -a:
 
Linux XXX 2.6.18-XXX.el5 #1 SMP x86_64 x86_64 x86_64 GNU/Linux

-----------------------------

Code:
echo $SHELL:
/bin/bash

----------------------------------

Code:
The shell script is located at
in root - > (directory) home -> (directory) myfolder -> abc.sh

Pl provide your suggestions/inputs...

Last edited by methyl; 07-05-2012 at 09:06 AM.. Reason: code tags
# 13  
Old 07-05-2012
The script /home/myfolder/abc.sh as posted is rather buggy. All is does is execute a large echo of everything between the two double quote characters and dumps it into a new file called ftp in the current working directory! You may wish to find this output file, check the contents and then delete it.

The script itself needs to positively state directory names and also needs to use ftp -i to stop ftp prompting after every file in a mput.
Folder names came from your narrative. I can't claim to fully understand your folder structure, so do check it.

In you post you say that your Shell is /bin/bash but your shebang line at the top of the script says /bin/sh. On some systems they are actually the same program, but do check.

This suggested code is to demonstrate the correct structure for the script (minus the echo) and to illustrate the need to have a cd to give filenames some context when running from cron.
Untested.

Code:
#!/bin/sh
cd /home/myfolder
 
HOST='XXX.XXX.XX.XXX'
USER='XXX'
PASSWD='XXX'
FILE='*.gz'
 
gzip *.dmp
ftp -in $HOST <<END_SCRIPT > ftp_$$.log
quote USER $USER
quote PASS $PASSWD
mput $FILE
quit
END_SCRIPT
 
cd my_ARCHIVE
rm $FILE
cd ..
mv $FILE my_ARCHIVE
 
exit 0

Consider commenting out the rm line while testing and do make sure that you have a copy of your *.dmp files so you can test again and again.

Last edited by methyl; 07-05-2012 at 09:12 AM..
# 14  
Old 07-05-2012
Hi Methyl,
I tested the script with contents as mentioned by you but that did'nt work through cron job.

In order to test if the issue is related to wrong scripting ode, I created the following simple script.

Code:
#!/bin/bash
cd /home/myfolder

FILE='*.dmp'
rm $FILE
exit 0

This script if executed manually through putty: sh xyz.sh removes the .dmp file from the myfolder.
The same script when cronned: generates a blank log file but does not removes the .dmp file.
I am really not sure whatss going wrong now.

Last edited by methyl; 07-05-2012 at 03:35 PM.. Reason: please use code tags
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