I'm stuck with the following (simple) problem : I want a script to be executed every 10 minutes. This script calls executable files. I use crontab and ksh on a AIX 5.3 system.
The script makes use of relative paths, but changing the executable path to absolute didn't make any difference. So, after a few tries and this answer, I came up with the following crontab entry (*/10 doesn't work)
rs14:/home/viloin# crontab -l
0,10,20,30,40,50 * * * * cd /home/viloin/cardme/bin && /bin/ksh myScript.ksh
here is the script :
#!/bin/ksh
Main(){
printf "executed in : %s\n" $(pwd);
executableFile 2>/dev/null 1>&2;
exeResult=$?; # expected return value : 90
printf "%s\n" $exeResult;
}
Main;
Here is the output when I run the command manually :
rs14:/home/viloin/cardme/bin# cd /home/viloin/cardme/bin && /bin/ksh myScript.ksh
executed in : /home/viloin/cardme/bin
90
And finally the output when crontab runs it for me (from mail) :
Subject: Output from cron job cd /home/viloin/cardme/bin && /bin/ksh myScript.ksh, [email protected], exit status 0
Cron Environment:
SHELL = /usr/bin/sh
PATH=/usr/bin:/etc:/usr/sbin:/usr/ucb:/usr/bin/X11:/sbin:/usr/java14/jre/bin:/usr/java14/bin
CRONDIR=/var/spool/cron/crontabs
ATDIR=/var/spool/cron/atjobs
LOGNAME=viloin
HOME=/home/viloin
Your "cron" job executed on rs14.saprr.local on Wed Aug 24 11:50:00 CEST 2016
cd /home/viloin/cardme/bin && /bin/ksh myScript.ksh
produced the following output:
executed in : /home/viloin/cardme/bin
127
*************************************************
Cron: The previous message is the standard output
and standard error of one of your cron commands.
My file myScript.ksh has all rights :
rs14:/home/viloin/cardme/bin# ll -al myScript.ksh
-rwxrwxrwx 1 viloin cardme 174 Aug 24 10:54 myScript.ksh
To make sure that my executableFile is not really exiting with code 127, I used the echo binaries, renamed it and I have the same behavior (Except that it returns 0 instead of 90 when I run the command manually).
What is causing this difference between manually typing the command and asking crontab to do it for me ?
1 Answer 1
change your shell script to provide a full or relative path to the executable:
./executableFile ...
In interactive use, you must either have .
or the cardme/bin
directory in your PATH: that will not be true in cron's environment.
-
1Thanks again. My executableFile (the original one) used PATH variables, so I changed my crontab entry to 0,10,20,30,40,50 * * * * . $HOME/.profile && cd /home/viloin/cardme/bin && /bin/ksh myScript.ksh This change fixed the previous issue too since my PATH is now correct for cronEtsitpab Nioliv– Etsitpab Nioliv2016年08月24日 13:24:34 +00:00Commented Aug 24, 2016 at 13:24
-
Good job. cron problems almost always are environment-related.glenn jackman– glenn jackman2016年08月24日 13:31:16 +00:00Commented Aug 24, 2016 at 13:31
-
you might be able to use
*/10
instead of0,10,20,30,40,50
-- check yourcrontab(5)
man page.glenn jackman– glenn jackman2016年08月24日 13:32:43 +00:00Commented Aug 24, 2016 at 13:32 -
I can't see any reference to this notation in my man page, and editing crontab accordingly gives me the following error : crontab: error on previous line; unexpected character found in line. It looks like the AIX 5.3 implementation doesn't allow this notation.Etsitpab Nioliv– Etsitpab Nioliv2016年08月24日 13:47:58 +00:00Commented Aug 24, 2016 at 13:47
-
oh, I didn't notice your aix tagglenn jackman– glenn jackman2016年08月24日 13:48:27 +00:00Commented Aug 24, 2016 at 13:48
PATH
. What happens if you runenv -i myScript.ksh
manually?executableFile
something that might reasonably expect to be run interactively and would fail if it's not connected to a terminal?./executableFile ...
-- in interactive use, you either have.
or the cardme/bin/directory in your PATH: that will not be true in cron's environment.