Operating System - HP-UX
1821917 Members
2953 Online
109638 Solutions
New Discussion юеВ

Re: cron job and shell script

 
SOLVED
Go to solution
Oliver Schmitz
Regular Advisor

cron job and shell script

Dear all,

I have a problem with sheduling a new cronjob (by the way again it is my first time developing a shell script and starting it with the cron daemon):

I developed a shell script which looks into a directory, analyses the content and start a http command, an ftp job, another http command and write everything to a log file.

I tested the functionality of this script very detailed and it worked fine. Now I want to execute the whole thing as a cron job.

To do so, I created the crontabfile for the corresponding user (checked the permissions of the script and all files involved aswell) and I saw that only the shell commands (echo xxx) were executed but unfortunately not the commands like ftp and httpget etc..

I tried these two entries in the crontab file:

0 14 * * * /location/scriptname

and after this did not work without an exec extention (I saw this in an example crontabfile but did not see any description of what it is for):

0 14 * * * exec /location/scriptname

In both cases only the shell commands (only echo xxx) were executed but nothing else.

Can anybody help me? Thanks in advance.

Best regards, Oliver

Oliver Schmitz
5 REPLIES 5
Stefan Farrelly
Honored Contributor
Solution

Re: cron job and shell script

On the first line of your script have;

#!/bin/sh -x

this will then run in debug mode. And redirect the output either in the cron command;

0 14 * * * /location/scriptname >/tmp/log 2>&1

or on the 2nd line of your script;
exec >/tmp/log 2>&1

Now when it runs you will see why it failed. Remember - scripts run from cron run in a different environment than from your shell - the PATH is different, etc. Usually scripts need a bit of extra work before running them under cron.
Im from Palmerston North, New Zealand, but somehow ended up in London...
Pete Randall
Outstanding Contributor

Re: cron job and shell script

Oliver,

Cron has only the most rudimentary of environemnt variables set when it runs scripts. You should always specify full path names to commands and ensure that any variables you need are explicitly defined.


Pete


Pete
Steven E. Protter
Exalted Contributor

Re: cron job and shell script


0 14 * * * /location/scriptname 2>&1

Note that cron has no inherent PATH in it.

You have two choices to get it to work right.

1) Set a PATH variable in the script.
2) give the full path of every command you use.

touch

becomes

/usr/bin/touch

etc

whence touch
/usr/bin/touch

Will help you look up the commands.

SEP
Steven E Protter
Owner of ISN Corporation
http://isnamerica.com
http://hpuxconsulting.com
Sponsor: http://hpux.ws
Twitter: http://twitter.com/hpuxlinux
Founder http://newdatacloud.com
Tim Adamson_1
Honored Contributor

Re: cron job and shell script

Hi Oliver,

Cron doesn't execute any of the normal startup files that are executed for an interactive login. Therefore, try setting up the PATH environment variable in each script or source a configuration file that sets up your environment.

eg,

PATH=/usr/bin:/sbin

or create a file with everything you want and to source it in the scripts:

. /usr/local/etc/

Note the .


Hope it helps.


Cheers!!!
Yesterday is history, tomorrow is a mystery, today is a gift. That's why it's called the present.
James R. Ferguson
Acclaimed Contributor

Re: cron job and shell script

Hi Oliver:

'cron' provides only a very limited environment for your script, by default. The HOME, LOGNAME and SHELL variables are set along with the (limited) 'PATH=/usr/bin:/usr/sbin:.'

A common approach is to source (read) your $HOME/.profile and then execute your script. This generally provides the environmental variables your script relies upon but leads to annoying "not at typewriter" messages whenever your script runs. This is a result of executing interactive commands ('stty', 'ttytype') contained in the $HOME/.profile.

A read of a user's profile is also invoked using the 'su - username -c script' syntax since the 'su - username' reads the 'username' profile too.

The best approach is to place the environmental variables you need in file that *both* your $HOME/.profile and scripts can source (read). This would, obviously, include any PATH you need.

Regards!

...JRF...