System Administration
cancel
Showing results for 
Search instead for 
Did you mean: 

Script Execution Unable to Log When Executed Via Cron

SOLVED
Go to solution
sysad_boy
Frequent Advisor

Script Execution Unable to Log When Executed Via Cron

I have a number of scripts that when executed manually logs everything on how it executes step by step. Problem is when these scripts are ran via cron logging is not complete.

Did some minor troubleshooting:
1. Checked the user that executes the job if he is a member of the cron.allow file.
2. Checked Directory Permissions
3. Changed the default shell of the user from /usr/bin/sh to /sbin/sh
4. Placed an ampersand "&" at the end of the script in crontab (e.g.: * * * * * /dir/script.sh &)

But to no avail. What seems to be the problem with cron?


TIA
21 REPLIES
sujit kumar singh
Honored Contributor
Solution

Re: Script Execution Unable to Log When Executed Via Cron

hi

can you please sahre the O/ps of the job as run maunally as well as the error that you get while executing the job as with cron.


the cron logs you can find at /var/adm/cron/log file.

please share as well the O/p of the command

crontab -e as run as root

regards
sysad_boy
Frequent Advisor

Re: Script Execution Unable to Log When Executed Via Cron

Actually there is no error being encountered.

I placed several lines inside my scripts to echo everything that it does to a file. There are actually two parts of the script logging. 1st part it logs the date, time of execution. 2nd part it logs several select commands to the DB.


The log looks like this when executed manually:


===> START Tue Jan 12 04:30:00 EAT 2010

Date From:
Date To:
Processing DM ACR
Interactive mode off.

(1 row affected)
Create table tempdb..XXX_ACR
(0 rows affected)
Extract ACR for Stream 0
(643 rows affected)
Consolidate data for Stream 0

===> END Tue Jan 12 15:09:52 EAT 2010


However when executed via cron, it only outputs:

===> START Tue Jan 12 15:14:00 EAT 2010

Date From:
Date To:
Processing DM ACR
Interactive mode off.
===> END Tue Jan 12 15:14:01 EAT 2010



I dont know why this is happening when the scripts is executed via cron.
sujit kumar singh
Honored Contributor

Re: Script Execution Unable to Log When Executed Via Cron

Hi

what are your crontab entries for the same job , can you please tell.



try redirecting the O/P as well as errors to some file and check.

also you can check actual result that this should perform sa run as a cron job for the same.


regards
sujit
Horia Chirculescu
Honored Contributor

Re: Script Execution Unable to Log When Executed Via Cron

Seems that your script does not find some file/utility. Maybe you should try to set in the script the PATH variable, like in:

PATH=/usr/sbin:/usr/bin:/whatever....

(You can copy this variable from the one that your system is using - see profile for root)

Best regards,
Horia.

Best regards from Romania,
Horia.
sysad_boy
Frequent Advisor

Re: Script Execution Unable to Log When Executed Via Cron

PATH is already defined in the user's .profile do I also need to place it inside my script?
john korterman
Honored Contributor

Re: Script Execution Unable to Log When Executed Via Cron

Hi,

>>PATH is already defined in the user's .profile ..<<

Please notice that no login is performed when a cron job is executed, hence the user's .profile is not read.
You should define your variables or the full command paths in your script.

regards,
John K.
it would be nice if you always got a second chance
Horia Chirculescu
Honored Contributor

Re: Script Execution Unable to Log When Executed Via Cron

As John Korterman pointed, you must declare this in your script (in order to avoid strange behavoir of your script).

Horia.
Best regards from Romania,
Horia.
sysad_boy
Frequent Advisor

Re: Script Execution Unable to Log When Executed Via Cron

Ok, i just did your advice but to no avail. Same result.
Horia Chirculescu
Honored Contributor

Re: Script Execution Unable to Log When Executed Via Cron

What exactly does your script? Can you post it here?

Horia.
Best regards from Romania,
Horia.
Dennis Handly
Acclaimed Contributor

Re: Script Execution Unable to Log When Executed Via Cron

>but to no avail.

Adding "set -x" to the start of your script will log commands as executed. Then look need the end for the error.

I assume you aren't trying to use any aliases you have defined in your .profile?
Dennis Handly
Acclaimed Contributor

Re: Script Execution Unable to Log When Executed Via Cron

>4. Placed an ampersand "&" at the end of the script in crontab (e.g.: * * * * * /dir/script.sh &)

Why would you want to do that? The crontab entry is already being processed asynchronously and you should let it wait until done.
john korterman
Honored Contributor

Re: Script Execution Unable to Log When Executed Via Cron

Hi again,

check also the mail of the script-executing user: if you have not redirected output from cron execution to a file, it will appear there.

regards,
John K.
it would be nice if you always got a second chance
Viktor Balogh
Honored Contributor

Re: Script Execution Unable to Log When Executed Via Cron

Try to redirect also the standard error, maybe it not get its way to the logfile. Something like:

command 2>&1 1>/path/to/logfile

may help. Edit your script.
****
Unix operates with beer.
sysad_boy
Frequent Advisor

Re: Script Execution Unable to Log When Executed Via Cron

actually there is no error while the script is being executed either manually or via cron. Its just that the logging capabilities are not working when the script runs automatically with the use of cron.

what actually the script does it is selecting data from a table, and then drops it.

If the script is ran manually, an output of affected rows is logged. The output looks like this:

(1 row affected)
Create table tempdb..XXX_ACR
(0 rows affected)
Extract ACR for Stream 0
(644 rows affected)
Consolidate data for Stream 0
(644 rows affected)
DROP TABLE for tempdb..XXX_ACR_0
Extract ACR for Stream 1
(608 rows affected)

Using cron, the above output is not logged when the script executes.

I don't know why I can't successfully log everything to my logfile when the script runs using cron. What's with cron and manually executing the script?
Steven Schweda
Honored Contributor

Re: Script Execution Unable to Log When Executed Via Cron

> [...] What's with cron and manually
> executing the script?

Who can see "the script"? You and the
psychics? I'm neither.

> Adding "set -x" [...]

Did you try that?

> command 2>&1 1>/path/to/logfile

Did you try that?

Did you try anything which might give us some
useful information?
Dennis Handly
Acclaimed Contributor

Re: Script Execution Unable to Log When Executed Via Cron

>What's with cron and manually executing the script?

Have you removed that "&"? cron may "hangup" the process tree when the initial shell exits because it isn't waiting for your script.

How long does the script take to run? Use time in both cases:
time /dir/script.sh
(This will send cron mail so remove when done.)
Viktor Balogh
Honored Contributor

Re: Script Execution Unable to Log When Executed Via Cron

>actually there is no error while the script is being executed either manually or via cron.

No, it's not about errors. The standard error is a 'separate channel' to output messages to, and some applications like rsync output messages also to stderr even if they are succesfully finished. Please try that method I suggested and report back if it helped.

# command 2>&1 1>/path/to/logfile

****
Unix operates with beer.
Dennis Handly
Acclaimed Contributor

Re: Script Execution Unable to Log When Executed Via Cron

>Viktor: command 2>&1 1>/path/to/logfile

Did you really want to redirect FD in this order? This will send stderr to the previous stdout, then the new stdout is going to logfile.
Typically if you want both to the same file:
... > /path/to/logfile 2>&1
Viktor Balogh
Honored Contributor

Re: Script Execution Unable to Log When Executed Via Cron

Dennis, you are right. Until now I never noticed that the order also counts. This way works:

# command 1>/path/to/logfile 2>&1

****
Unix operates with beer.
sysad_boy
Frequent Advisor

Re: Script Execution Unable to Log When Executed Via Cron

thanks so much for the help.

I was able to solve the problem already.


Viktor Balogh
Honored Contributor

Re: Script Execution Unable to Log When Executed Via Cron

>I was able to solve the problem already.

How? Let us know, maybe we can also learn something new!
****
Unix operates with beer.