1835921 Members
2677 Online
110088 Solutions
New Discussion

Run crontab job

 
SOLVED
Go to solution
hangyu
Regular Advisor

Run crontab job

I hv a crontab job (script) that can be run normally , now I try to run it on the shell but can't be run , can advise what is difference between run a script on crontab and run it manually ? is it the difference of system enviornment ?thx
34 REPLIES 34
A. Clay Stephenson
Acclaimed Contributor
Solution

Re: Run crontab job

This is a very common problem. The environment of cron is intentionally very sparse and cron does not source the user's .profile (nor should it). The solution is rather simple: explicitly set and possibly export any needed environment variables (especially PATH) inside your cron'ed script.
If it ain't broke, I can fix that.
Jov
Honored Contributor

Re: Run crontab job

Hi,

This kind of problem is usually the otherway around. As in within the Shell you can run the script fine, but fails in Cron.

Since its failing in the shell, execute 'set -x' (assuming you're run Bourne/Korn) then run the script. This should show you when the problem is or the last command that failed.

Jov
A. Clay Stephenson
Acclaimed Contributor

Re: Run crontab job

Oh, I may have misread your problem (although it's still not quite clear). Almost certainly if the script runs under cron but not from an interactive shell then the problem is permissions. Are you running as the same user as the cron'ed version? Is the execute bit set on this file for the user that you are running as? Your script could also be testing to see if stdin is a tty device but that would be unlikely. Add set -x and you should immediately see your problem.
If it ain't broke, I can fix that.
hangyu
Regular Advisor

Re: Run crontab job

thx replies,

I hv two more questions

1. as A.Clay said , how can I "export any needed environment" ? how can I know all my environment ?

2. As A.Clay said , when login with the batch user , it will run the .profile and global profile ( I have set it for my application ) . Is it possible when I run it on shell , the enviornment change to the same as run it by crontab ? thx
A. Clay Stephenson
Acclaimed Contributor

Re: Run crontab job

The "env" command will list all your environment variables.

It's a little difficult to understand your problem.

Answer this very precisely. If you run your script from the shell (NOT FROM CRON), does the script execute properly? Yes or No?
If it ain't broke, I can fix that.
hangyu
Regular Advisor

Re: Run crontab job

No , it is NOT properly run.

thx

A. Clay Stephenson
Acclaimed Contributor

Re: Run crontab job

Ok, in that case the environment set by your .profile or the fact that stdin, stdout, and/or stderr are terminal devices is somehow altering the behavior OR as this user you simply don't have sufficient permissions.

Add set -x near the top of your script and try to run it. You should see exactly what is wrong.
If it ain't broke, I can fix that.
hangyu
Regular Advisor

Re: Run crontab job

thx reply ,

I tried to run "set -x" on shell , but no any output , what is the function of this command ?

ps. I use bash.
Dennis Handly
Acclaimed Contributor

Re: Run crontab job

>I tried to run "set -x" on shell, but no any output, what is the function of this command?

In a real shell, set -x will list to seterr the commands as it executes them. See ksh(1) or sh(1).

>I use bash.

Change your shell to sh or ksh? At least until you get it working.
A. Clay Stephenson
Acclaimed Contributor

Re: Run crontab job

Let it be known that I am not a real big fan of bash.

Here's one reason:
echo "One Two Three" | read A B C
echo "A = ${A} B = ${B} C = ${C}"

That works on the POSIX shell and most Korn shell implementations; it fails on most bash implementations. We can argue about the proper handling of lvalues and subprocess but the above construct is very useful.

Now, is this an HP-UX question or a Linux question?
If it ain't broke, I can fix that.
hangyu
Regular Advisor

Re: Run crontab job

thx reply ,

I have a bit confuse ,

do you mean I should change it to ksh to test it , add "set -x" to my script and try to run it on shell to see any error message ? thx
A. Clay Stephenson
Acclaimed Contributor

Re: Run crontab job

First, is this an HP-UX issue or or some other OS? That helps to define the problem.

Secondly, I don't care what shell YOU use; that's your choice but you should at least know how to enable debugging mode for whatever shell you do use. All I am saying is that contrary to world opinion bash may not be the world's best shell. For example, on Linux, I use the zsh when possible. Moreover, it is very possible that the problem you are describing is a direct consequence of running under different shells when under cron and when in an interactive environment.
If it ain't broke, I can fix that.
Dennis Handly
Acclaimed Contributor

Re: Run crontab job

>do you mean I should change it to ksh to test it , add "set -x" to my script and try to run it on shell to see any error message?

Well, as Clay says, you shouldn't use bash at all. ;-)

First you need to look at your bash documentation and see if it supports a set -x like option will enable tracing of script executation.

From my 3.0 bash man page, set -x does do what a real shell says it should. So this means if you don't get any output, we have to address this.

(You can test this by adding "set -x" to a simple bash script and then see of the rest of the commands in the script get echoed.)

For a real shell, you can also add the -x on the first line:
#!/usr/bin/sh -x
Or:
#!/usr/bin/bash -x
(Whatever your bash path is.)

Try that for your bash script.
First try adding -x, then try changing the shell.
Yogeeraj_1
Honored Contributor

Re: Run crontab job

hi,

one dirty way to do this is to create a crontab entry that runs the job as if you ran it manually.

this should look something like this:

#*******************************************************************************

12 18 * * * echo "/path/scripts/myscript.sh; exit"|su - yogeeraj 1>/home/yogeeraj/logfiles/output.crn 2>/home/yogeeraj/logfiles/error.crn

#*******************************************************************************
# END OF TABLE day0->Sunday day6->Saturday
#*******************************************************************************


hope this helps!

kind regards
yogeeraj
No person was ever honoured for what he received. Honour has been the reward for what he gave (clavin coolidge)
hangyu
Regular Advisor

Re: Run crontab job

thx reply ,

I am checking how to use "set -x" , I think the function of this command is to debug error. Except this method , is there other simply way to make the environment ( run by manually ) to the same as run by crontab ? thx
Dennis Handly
Acclaimed Contributor

Re: Run crontab job

>is there other simply way to make the environment (run by manually) to the same as run by crontab?

Nobody would EVER want to do that except to debug a bad crontab entry. This was mentioned by Clay and Jov.

To do this, you would have to remove ENV vars and limit the PATH to what crontab(1) says.

And as Clay mentioned, if you have functions that work only on tty devices, you need to exclude those.
hangyu
Regular Advisor

Re: Run crontab job

thx reply ,

The below is user's env after login.

MANPATH=/usr/share/man:/usr/local/man
src=/ora_usr/src
HOSTNAME=ora

Now , if I want all schedule job ( crontab ) also has this env , what can I do ? thx
Jov
Honored Contributor

Re: Run crontab job

Add them to the start of the script. If its a command then you'll need to wrapper it within a script.

But why you need to do this? Removing things tend to break crontabs not fix them.


Jov
hangyu
Regular Advisor

Re: Run crontab job

thx reply ,

Add them to the start of the script. If its a command then you'll need to wrapper it within a script -- > can advise how can I wrapper it within a script ?

But why you need to do this? Removing things tend to break crontabs not fix them.

--> I guess it is the simplest way to fix it , I know it will "break crontabs" , do you think it is harmful to the system ? thx
hangyu
Regular Advisor

Re: Run crontab job

thx all,

As my previous question , now the crontab can run normally by manual but can't run by crontab , so is it possible to do that - when run a script by crontab , it is exactly the same env as run by manual ? thx
hangyu
Regular Advisor

Re: Run crontab job

thx all,

what I want is just to change the env of a crontab job , I tried to add the below script to a crontab job but still fail , can advise what is wrong ? thx


where env_cron is user's env.

for i in $(cat /tmp/env_cron)
do
export ${i}
done
Arturo Galbiati
Esteemed Contributor

Re: Run crontab job

Hi,
pls, can you post your script and the output you get running it manually?
This may help to go to rifght direction.
Rgds,
Art
Yogeeraj_1
Honored Contributor

Re: Run crontab job

hi again,


"so is it possible to do that - when run a script by crontab , it is exactly the same env as run by manual ? "

as mentioned previously, you should try the easy method of just doing:

echo ".<script>.."| su - user

where is the user which you have use to test your script "manually"

hope this helps too!


kind regards
yogeeraj
No person was ever honoured for what he received. Honour has been the reward for what he gave (clavin coolidge)
hangyu
Regular Advisor

Re: Run crontab job

thx Arturo Galbiati reply ,


I also want to post the output , but this involve my application , the script is very long , but as the previous reples , this problem is very common as they have different env ( i still think this is the reason ) , so I would like to fix this problem by simply changing the env as my previous question .

thx a lot.