Crontab not running a script

Hi, I posted this in the Solaris forum but I think this one would be more appropriate.

I created a script starting with the following lines:

#!/usr/bin/ksh

flag=n
export flag
typeset -i quant=0
(...)

When running it I'm getting the following 2 errors:

/tmp/tstscript/testfail.ksh: typeset: not found
/tmp/tstscript/testfail.ksh: function: not found

crontab uses /bin/sh (Bourne shell in Solaris, if I'm not mistaken), it looks like it's not recognising the shebang (first line of the scripts, with no blank lines before).

Any help on this?

Are you capturing the output of script in any log?

If not , capture it and you can see the erros from log.

Cron will run the with whatever shell you mentioned in the first line.

Yes, the error output is what I pasted in the previous message:

/tmp/tstscript/testfail.ksh: typeset: not found
/tmp/tstscript/testfail.ksh: function: not found
/tmp/tstscript/testfail.ksh: syntax error at line 29: `}' unexpected

Bourne shell doesn't recognise typeset, that's why I thought that maybe it's not using the korn shell. Other than that I don't know what could be wrong.

Please can you send us the output of crontab -l please.

Robin
Liverpool/Blackburn
UK

To be honest i suppose this has nothing to do with scripting but is a Solaris-specific problem.

I'm by no means a Solaris expert, so i may be mistaken, but: isn't the ksh in Solaris residing in /bin/ksh or in /usr/xpg4/bin/sh ?

Might it be that cron simply doesn't know which shell to start and hence reverts back to sh?

I hope this helps.

bakunin

I've tried both (didn't know about the second one, thanks) and it still gives the same error. I also think it's a problem with Solaris, but I'm not sure anymore. It's frustrating, I can't seem to find any info in google.

Many years ago I found a way to force the shell for a cron job. It's probably not the best way to do it and it may be as bakunin has suggested. What I coded within cron (and it was running some 15 years before we moved platform :eek:) was something like this mess:

00 07 * * * /usr/bin/ksh %/usr/bin/my_ksh_script

I can't remember how I worked this out or why I thought it was a good idea, but it seemed to work. The % marker did something special and it worked, but the machine it was on is now gone by 2 years, so I can't be sure. I remember thinking that /usr/bin was a great place to put my code, seeing as it was in everyone's path. I think I'd recommend against that now!

I must say that what you have all looks to be in order. Perhaps a quick test script could be knocked together along the lines of:

#!/bin/ksh
ps -f>/tmp/ps-f
ps -l>/tmp/ps-l

Have a look at the processes listed to see if that gives you more hints.

Robin
Liverpool/Blackburn
UK

Hi Robin,

Btw I didn't mention it before, the script runs fine if I start it, it's when crontab executes it that fails. Anyway, I modified the script:

#!/bin/ksh

ps -f>/tmp/ps-f
ps -l>/tmp/ps-l
flag=n
export flag
(...)

And ran it from crontab, the output files /tmp/ps-f and /tmp/ps-f end up empty.

This has to be so because crontab jobs are started from init, which has no environment. For "ps" to be found it would have to be in the path and $PATH would be empty at this point of execution. If you want to have the same environment as your login shell in your cron job do something similar to the following:

#! /bin/ksh

. /your/home/.kshrc

ps -l > /tmp/ps-l

I hope this helps.

bakunin

Thanks! I'm not getting the typeset and function errors anymore!

I'm getting an error regarding another variable, but that's another story, it loks like this one is solved, I hope I can use this thread if I can't solve the variable thing.

Thanks very much for your help.