LinuxQuestions.org
Welcome to the most active Linux Forum on the web.
Go Back   LinuxQuestions.org > Forums > Non-*NIX Forums > Programming
User Name
Password
Programming This forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game.

Notices


Reply
  Search this Thread
Old 05-29-2009, 07:09 PM   #1
scofiled83
Member
 
Registered: Nov 2008
Posts: 70

Rep: Reputation: 15
cron job doesnt work, it works manually


hi

Hp-Unix


I have a very small shell script.
I can run it manually like :

sh /opt/orcl/ctrace.sh


However when I put it in a cron, it doesnt work,it generates a log file which is empty. What should I do?


55 11 * * * /opt/orcl/ctrace.sh> /opt/orcl/ctrace.log
 
Old 05-29-2009, 07:15 PM   #2
jlinkels
Senior Member
 
Registered: Oct 2003
Location: Bonaire
Distribution: Debian Wheezy/Jessie/Stretch/Sid, Linux Mint DE
Posts: 4,634

Rep: Reputation: 713Reputation: 713Reputation: 713Reputation: 713Reputation: 713Reputation: 713Reputation: 713
Since you run it manually using the sh command, and you do not from within cron it might be a permissions thing, or you did not put #!/bin/sh as first line.

After you checked the permissions of the file (it should be readable by the user which runs the cron job) change the line in cron to:

Code:
55 11 * * * /bin/sh /opt/orcl/ctrace.sh> /opt/orcl/ctrace.log
Check if /bin/sh is the correct path (which sh). Make sure you have one empty line at the end of the crontab file.

Check /var/log/syslog to see what CRON has to tell you.

jlinkels
 
Old 05-29-2009, 07:19 PM   #3
scofiled83
Member
 
Registered: Nov 2008
Posts: 70

Original Poster
Rep: Reputation: 15
-rwxrwxrwx permissions.

and my script starts with

#!/bin/sh


which sh shows : /usr/bin/sh

I dont have permission to check /var/log/syslog



I also changed crontab entry to :

24 12 * * * usr/bin/sh /opt/orcl/ctrace.sh> /opt/orcl/ctrace.log

It again produce empty logfile and script was not executed.

Last edited by scofiled83; 05-29-2009 at 07:25 PM.
 
Old 05-29-2009, 08:07 PM   #4
jlinkels
Senior Member
 
Registered: Oct 2003
Location: Bonaire
Distribution: Debian Wheezy/Jessie/Stretch/Sid, Linux Mint DE
Posts: 4,634

Rep: Reputation: 713Reputation: 713Reputation: 713Reputation: 713Reputation: 713Reputation: 713Reputation: 713
Quote:
Originally Posted by scofiled83 View Post
#!/bin/sh
which sh shows : /usr/bin/sh
It is not good that these two are different. For sure that is the cause that the script did not run in the first place.

However, now you are calling it with /usr/bin/sh which should execute the script.

Is the log file being created at the time the cron job runs?

If so, cron does execute the command line, including the redirecting, but for some reasons either the statements in the script file are not executed, or they don't produce any output.

Find the path to echo, and put this line as first in your script:
Code:
/path/to/echo "this script starts now" > /opt/orcl/ctrace.log
Did you use the full path for each call to programs in your script? Remember that paths in the cron environment are different from those in your shell, or do not exist at all.

I recommend that you remove the #!/bin/sh in the script, or replace it with #!/usr/bin/sh

And if this all doesn't work, I am out of ideas, sorry.

jlinkels
 
Old 05-29-2009, 08:35 PM   #5
scofiled83
Member
 
Registered: Nov 2008
Posts: 70

Original Poster
Rep: Reputation: 15
Here is my script:
As I said, I can run it manually, but when I put it in a cron, it doesnt run.
Empty logfile is generated after the execution


view ctrace.sh

#!/bin/sh
export ORACLE_SID=uw1
sqlplus -s << ENDOFSQL
/ as sysdba
set linesize 121
spool /opt/orcl/control_traces/ctrace.sql
SELECT 'alter database backup controlfile to trace as ''/opt/orcl/control_traces/ctrl_'||TO_CHAR(SYSDATE, 'ddmmyy')
||'.trc'';'
FROM DUAL;
spool off;
@/opt/orcl/control_traces/ctrace.sql
ENDOFSQL
rm /opt/orcl/control_traces/ctrace.sql

Last edited by scofiled83; 05-29-2009 at 09:09 PM.
 
Old 05-29-2009, 09:39 PM   #6
jlinkels
Senior Member
 
Registered: Oct 2003
Location: Bonaire
Distribution: Debian Wheezy/Jessie/Stretch/Sid, Linux Mint DE
Posts: 4,634

Rep: Reputation: 713Reputation: 713Reputation: 713Reputation: 713Reputation: 713Reputation: 713Reputation: 713
Quote:
Originally Posted by jlinkels View Post
Find the path to echo, and put this line as first in your script:
Code:
/path/to/echo "this script starts now" > /opt/orcl/ctrace.log
Did you use the full path for each call to programs in your script? Remember that paths in the cron environment are different from those in your shell, or do not exist at all.

I recommend that you remove the #!/bin/sh in the script, or replace it with #!/usr/bin/sh
jlinkels
 
Old 05-29-2009, 10:11 PM   #7
scofiled83
Member
 
Registered: Nov 2008
Posts: 70

Original Poster
Rep: Reputation: 15
yes I tried
It didnt work
Anyway thanks
 
Old 05-30-2009, 02:05 PM   #8
Sergei Steshenko
Senior Member
 
Registered: May 2005
Posts: 4,481

Rep: Reputation: 454Reputation: 454Reputation: 454Reputation: 454Reputation: 454
Quote:
Originally Posted by scofiled83 View Post
yes I tried
It didnt work
Anyway thanks
I vaguely remember 'cron' is tricky/picky regarding environment variables, i.e. to launch jobs under 'cron' one might need to set environment variables for the job to be launched from the enveloping script that 'cron' launches.
 
Old 05-30-2009, 03:12 PM   #9
colucix
LQ Guru
 
Registered: Sep 2003
Location: Bologna
Distribution: CentOS 6.5 OpenSuSE 12.3
Posts: 10,509

Rep: Reputation: 1978Reputation: 1978Reputation: 1978Reputation: 1978Reputation: 1978Reputation: 1978Reputation: 1978Reputation: 1978Reputation: 1978Reputation: 1978Reputation: 1978
Try to redirect standard error together with standard output to the log file and check if the shell gives some useful error message:
Code:
24 12 * * * /opt/orcl/ctrace.sh > /opt/orcl/ctrace.log 2>&1
 
  


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
Why my cron job doesn't work byng08 Linux - Newbie 7 11-26-2008 01:17 AM
shell prompt works, cron job fails btuley Linux - General 1 11-13-2008 11:40 AM
Cron Job only runs when it is manually started sinister1 Linux - General 4 02-25-2008 08:35 AM
Command not working in cron job, but works fine in root Roosta21 Linux - Software 4 11-22-2007 08:08 AM
Script works when run manually but not in cron job Harlin Linux - Software 7 10-16-2007 06:36 PM

LinuxQuestions.org > Forums > Non-*NIX Forums > Programming

All times are GMT -5. The time now is 04:24 PM.

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