Operating System - HP-UX
1754417 Members
2740 Online
108813 Solutions
New Discussion юеВ

Can't run a simple script in crontab job

 
hangyu
Regular Advisor

Can't run a simple script in crontab job

I have a very simple script , it can sucessfully run by manual , but if I add it to the crontab , it do not run ( I check the log but didn't find it has been run ) , can advise what is wrong ? thx

#vi /tmp/test_script
pwd > /tmp/path.txt



11 REPLIES 11
Wouter Jagers
Honored Contributor

Re: Can't run a simple script in crontab job

Hi,

When running through crontab, the environment is not set (as opposed to you running the script from your shell).

The 1st line of your script should be:
#!/bin/sh

And you might want to replace 'pwd' with '/bin/pwd' or copy the 'PATH' line from your profile and make it the 2nd line of your script.

Cheers,
Wout
an engineer's aim in a discussion is not to persuade, but to clarify.
hangyu
Regular Advisor

Re: Can't run a simple script in crontab job

thx reply ,

But I think that even the script has problem , it still will run , but I found that it didn't run ( I have checked log ) , can advise what is the problem ? thx
john korterman
Honored Contributor

Re: Can't run a simple script in crontab job

Hi,

how did you schedule the job - and is the cron daemon running?

regards,
John K.
it would be nice if you always got a second chance
hangyu
Regular Advisor

Re: Can't run a simple script in crontab job

thx reply ,

the other cron job is normal to run , but only this job not run .

The crontab is as below.
30 16 * * * /tmp/test_script , it is very standand setting, I sure the setting is correct.




thx
spex
Honored Contributor

Re: Can't run a simple script in crontab job

Hello,

1) The script needs a she-bang line at the top (e.g. #!/usr/bin/sh) and should exit at the bottom.

2) Ensure the name of the user whose crontab you added the script to appears in /var/adm/cron/cron.allow.

3) Ensure the user designated to execute the script via cron has read/execute permission on the script as well as the directory hierarchy containing it.

4) Restart cron via '/sbin/init.d/cron stop && /sbin/init.d/cron start'.

5) In this instance, 'pwd' is in '/usr/bin', which is part of cron's default PATH. But if an executable appearing in the script to be executed by cron weren't in '/usr/bin' or '/usr/sbin', you would have to explicitly add its directory to the PATH.

PCS
V. Nyga
Honored Contributor

Re: Can't run a simple script in crontab job

Hi,

maybe the problem is: what should the 'working directory' be, when running this script through cron?

Test with 'cd /tmp' then 'pwd'

Volkmar
*** Say 'Thanks' with Kudos ***
john korterman
Honored Contributor

Re: Can't run a simple script in crontab job

...and check the mail to the user of the cron job to be executed; when you do not redirect output - as in your example - output will be mailed to the user.

regards,
John K.
it would be nice if you always got a second chance
Dino_4
Frequent Advisor

Re: Can't run a simple script in crontab job


Hi,

please post the output of the following commands

whoami

ll /tmp/test_script

crontab -l | grep test_script

ps -ef | grep cron
Dennis Handly
Acclaimed Contributor

Re: Can't run a simple script in crontab job

>I have checked log

I assume that was /var/adm/cron/log ?

>PCS: 5) In this instance, 'pwd' is in /usr/bin, which is part of cron's default PATH.

Actually pwd is a shell builtin.