LinuxQuestions.org
Latest LQ Deal: Linux Power User Bundle
Go Back   LinuxQuestions.org > Forums > Linux Forums > Linux - Newbie
User Name
Password
Linux - Newbie This Linux forum is for members that are new to Linux.
Just starting out and have a question? If it is not in the man pages or the how-to's this is the place!

Notices


Reply
  Search this Thread
Old 06-08-2012, 02:49 PM   #1
mahendra singh
LQ Newbie
 
Registered: Mar 2011
Location: Bangalore,INDIA
Posts: 12
Blog Entries: 1

Rep: Reputation: 0
script not working with cron


Dear Team,

I am new to unix and have written some script which is not formatted because i want a free download tool to format the below code by window xp OS to save time

Problem is that if i execute the script manually it works but when i ccron it it dont work with expected results. My cron is working because i get mail.
---------------------
crontab -l
* * * * * /bdrhomtt01/projets/BDR/bdrhom02/bin/mah_trf.ksh
---------------------
Script:
more mah_trf.ksh

Code:

cd /bdrhomtt01/projets/BDR/bdrhom02/log
for i in `ls /bdrhomtt01/projets/BDR/bdrhom02/log/test* | xargs -n1 basename`;
do
if [[ $i != "test1.txt" ]]
then
echo $i;
ftp -niv PARNAS04 << EOF
user TRANSBDR xxxxxx
cd BDRDoc/Extractions_Output_Files
put $i
bye
EOF
mv $i /bdrhomtt01/projets/BDR/bdrhom02/RC/
else
echo "No new files found to tarnsfer";
fi
done
cd /bdrhomtt01/projets/BDR/bdrhom02/bin/
cat m.txt | mailx -s "Files moved to PARNA04" mahendra.singh@gmail.com
 
Old 06-08-2012, 03:04 PM   #2
suicidaleggroll
LQ Guru
 
Registered: Nov 2010
Location: Colorado
Distribution: OpenSUSE, CentOS
Posts: 5,258

Rep: Reputation: 1947Reputation: 1947Reputation: 1947Reputation: 1947Reputation: 1947Reputation: 1947Reputation: 1947Reputation: 1947Reputation: 1947Reputation: 1947Reputation: 1947
You said the results aren't expected, but what are they? What is it doing?

Remember the cron has a very restricted PATH. Commands like mv, cd, ls, etc. should all work fine, but ftp might not, and your "mailx" routine might not. It's good practice to hardcode the full path to these commands if the script will be called from cron.
 
Old 06-08-2012, 03:12 PM   #3
mahendra singh
LQ Newbie
 
Registered: Mar 2011
Location: Bangalore,INDIA
Posts: 12
Blog Entries: 1

Original Poster
Rep: Reputation: 0
results

Quote:
Originally Posted by suicidaleggroll View Post
You said the results aren't expected, but what are they? What is it doing?

Remember the cron has a very restricted PATH. Commands like mv, cd, ls, etc. should all work fine, but ftp might not, and your "mailx" routine might not. It's good practice to hardcode the full path to these commands if the script will be called from cron.

I am trying to compare a hardcoded filename with similar filenames in log dir. if it dont match than transfer to FTP server else say no files found
 
Old 06-08-2012, 03:59 PM   #4
suicidaleggroll
LQ Guru
 
Registered: Nov 2010
Location: Colorado
Distribution: OpenSUSE, CentOS
Posts: 5,258

Rep: Reputation: 1947Reputation: 1947Reputation: 1947Reputation: 1947Reputation: 1947Reputation: 1947Reputation: 1947Reputation: 1947Reputation: 1947Reputation: 1947Reputation: 1947
I know what the script is supposed to do, I'm asking what it's actually doing when you run it in the cron, since you say it doesn't give you your expected results. What DOES it give you? What does it do? Where does it fail? What is the output? Do you get any emails from the cron with error information?
 
Old 06-09-2012, 12:25 AM   #5
mahendra singh
LQ Newbie
 
Registered: Mar 2011
Location: Bangalore,INDIA
Posts: 12
Blog Entries: 1

Original Poster
Rep: Reputation: 0
output

Quote:
Originally Posted by suicidaleggroll View Post
I know what the script is supposed to do, I'm asking what it's actually doing when you run it in the cron, since you say it doesn't give you your expected results. What DOES it give you? What does it do? Where does it fail? What is the output? Do you get any emails from the cron with error information?

I get a mail with message written in m.txt
 
Old 06-09-2012, 12:33 AM   #6
mahendra singh
LQ Newbie
 
Registered: Mar 2011
Location: Bangalore,INDIA
Posts: 12
Blog Entries: 1

Original Poster
Rep: Reputation: 0
output

Quote:
Originally Posted by mahendra singh View Post
I get a mail with message written in m.txt
but when i go and check the files in the log dir. still exists which shouldn't be the case and also it has not transferred the files to PARNAS04 server
 
Old 06-09-2012, 05:14 AM   #7
Jost
LQ Newbie
 
Registered: Apr 2012
Location: Augusta, GA
Distribution: Fedora
Posts: 7

Rep: Reputation: Disabled
Generally a script that will run OK manually but not from cron is due to environment settings. Something that is set in your interactive shell is not being set by cron. It could be as simple as a difference in the PATH variable (it is very generic in cron). Normally any output that is not redirected from a cron jobs is sent to the owner of the crontab. Check your local system email for any messages. You'll probably find many unread messages.

Try redirecting STDOUT and STDERR to a file to see if there are any messages. Do this on your crontab line:

Code:
* * * * * /bdrhomtt01/projets/BDR/bdrhom02/bin/mah_trf.ksh >mah_trf.out 2>&1
(Set the filename and path as you want.)

You should also explisitly set the command interpreter and turn on command expansion by entering this on the first line of your script:

Code:
#!/bin/ksh -x
This is the same as invoking the script as ksh -x script.ksh. The -x turns on command expansion which displays the command with all variables expanded to their set value. This ensures your script is being called by the intended interpreter (ksh in this case). The default shell on most most modern Linux systems I've used is bash. There are differences between bash and ksh executuon. Remove the -x after you get it working to quiet the output.

If it is still not working put an "env" command (without quotes) near the top of the script (after the shell line above but before your cd command. Then compare the output from env of a manual job ans a cron job.
 
Old 06-11-2012, 02:10 PM   #8
mahendra singh
LQ Newbie
 
Registered: Mar 2011
Location: Bangalore,INDIA
Posts: 12
Blog Entries: 1

Original Poster
Rep: Reputation: 0
Smile Perfect JOB

Quote:
Originally Posted by Jost View Post
Generally a script that will run OK manually but not from cron is due to environment settings. Something that is set in your interactive shell is not being set by cron. It could be as simple as a difference in the PATH variable (it is very generic in cron). Normally any output that is not redirected from a cron jobs is sent to the owner of the crontab. Check your local system email for any messages. You'll probably find many unread messages.

Try redirecting STDOUT and STDERR to a file to see if there are any messages. Do this on your crontab line:

Code:
* * * * * /bdrhomtt01/projets/BDR/bdrhom02/bin/mah_trf.ksh >mah_trf.out 2>&1
(Set the filename and path as you want.)

You should also explisitly set the command interpreter and turn on command expansion by entering this on the first line of your script:

Code:
#!/bin/ksh -x
This is the same as invoking the script as ksh -x script.ksh. The -x turns on command expansion which displays the command with all variables expanded to their set value. This ensures your script is being called by the intended interpreter (ksh in this case). The default shell on most most modern Linux systems I've used is bash. There are differences between bash and ksh executuon. Remove the -x after you get it working to quiet the output.

If it is still not working put an "env" command (without quotes) near the top of the script (after the shell line above but before your cd command. Then compare the output from env of a manual job ans a cron job.
Dear finally the issue got resolved bu putting #!/bin/ksh -x

Thank you all Guys fro your prompt response
 
  


Reply


Thread Tools Search this Thread
Search this Thread:

Advanced Search

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off



Similar Threads
Thread Thread Starter Forum Replies Last Post
[SOLVED] CRON not working ; any other way to schedule a script ? arjundey AIX 14 03-21-2012 03:34 AM
[SOLVED] Shell Script Not Working in Cron 3rods Linux - Newbie 6 02-10-2010 09:24 PM
Variable in script not working in cron Aslan_Eident Linux - Server 6 01-02-2009 06:04 PM
PHP script through cron not working fine. vikasumit Linux - General 5 09-04-2008 03:22 PM
Mysql Cron Script using select not working fallen Linux - General 2 11-04-2003 08:37 AM


All times are GMT -5. The time now is 11:04 AM.

Main Menu
Advertisement
My LQ
Write for LQ
LinuxQuestions.org is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
Syndicate
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
Facebook: linuxquestions Google+: linuxquestions
Open Source Consulting | Domain Registration