1753701 Members
4895 Online
108799 Solutions
New Discussion

Cron job

 
mvr
Regular Advisor

Cron job

Hello,

 

I am trying to run a simple script via cron on HP-UX 11i. Script executed manually works fine, but when executed via cron, nothing happens. Script seems to run, but no result.

       1.) name of the script is "xxxx.sh"

       2.) script is setup with 777 permission

       3.) script location is /home/user

       4.) script runs as "root" user

       5.) script group owner is "sys"

       6.) script in cron looks like this:   00,15,30,45 * * * * /home/user/xxxx.sh

 

In a cron, do I need to execute script same as I do it manually ( sh /home/user/xxxx.sh ), or do I just specify a path for script?

 

Miro

 

 

 

5 REPLIES 5
Patrick Wallek
Honored Contributor

Re: Cron job

Have you checked the /var/adm/cron/log file to see what it shows?

 

When a script does not run via cron it is usually because there is some environment variable that is not set.  You must remember that cron has a very sparse environment.  It will NOT have the same environment you have when you log in.

James R. Ferguson
Acclaimed Contributor

Re: Cron job

Hi:

 

As Patrick noted, a 'crontask' is given only a rudimentary set of environment variables.  The 'crontab' manpages document this.  You automatically get:

 

HOME=user’s-home-directory

LOGNAME=user’s-login-id

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

SHELL=/usr/bin/sh

 

...with everything else you normally have defined in a login profile absent.

 

Regards!

 

...JRF...

mvr
Regular Advisor

Re: Cron job

Thanks guys.

Here is output from /var/adm/cron/log

CMD: /home/mvr/xxxx.sh
> root 4813 c Wed Sep 28 23:30:00 EDT 2011
< root 4813 c Wed Sep 28 23:30:00 EDT 2011 rc=127
< root 4810 c Wed Sep 28 23:30:01 EDT 2011
< root 4811 c Wed Sep 28 23:30:01 EDT 2011

Do I need to specify enviroment for "root" user?
V. Nyga
Honored Contributor

Re: Cron job

The best would be to use no enviroment (in mind).
So just use full pathes and only variables, which you define inside the script.

HTH
V.
*** Say 'Thanks' with Kudos ***
James R. Ferguson
Acclaimed Contributor

Re: Cron job


@mvr wrote:
Here is output from /var/adm/cron/log

CMD: /home/mvr/xxxx.sh
> root 4813 c Wed Sep 28 23:30:00 EDT 2011
< root 4813 c Wed Sep 28 23:30:00 EDT 2011 rc=127
< root 4810 c Wed Sep 28 23:30:01 EDT 2011
< root 4811 c Wed Sep 28 23:30:01 EDT 2011

Do I need to specify enviroment for "root" user?

First, a return code of 127 points to a non-existent file.

 

I'd add '-x' to the interpreter line of your script:

 

#!/usr/bin/sh -x

 

...and let 'cron' mail you the output (trace).

 

By the way, setting permissions on a script to 777 is a bad choice -- anyone could change the script!  A better choice would be 755 if everyone needs to be able to execute it.  Shell scripts need only be readable and executable to be run.

 

Regards!

 

...JRF...