Discussion Locked

No CD via Crontab?

0 helpful
13 replies
Closed to new repliesThis discussion is closed. It stays readable, but it takes no new replies.

I am using crontab to start a script which will perform an automated task daily. In this (Bourne) script there are several cd commands which will change the directory. When the script is manually started from within a shell, it runs perfectly. However, when the script is started from crontab, all the cd commands are ignored and the system stays in the 'home' directory. So, I have to give a 'full path' for all the files which are called. What could be the reason?
The environmental variable CDPATH is undefined, but this is also the case in the shell from where I start the script...

13 replies

by jandiar

Well, I don't think that the cd commands are ignored by crontab. So...

First, make sure that the cd commands are executed. Modify your script to print out the current working directory after each cd like this:
echo "cd a_dir"
cd a_dir
pwd > /tmp/your_script.log
run a_command
After your script is executed by crontab, read the /tmp/your_script.log and verify whether the cd command is not ignored.

Second, you may need to add the "./" prefix to your command that you want to run after cd. For example:
cd a_dir
./the_command

Hope this helps.

by les

what is cron telling you? each time a job is executed cron will have mail with any errors. Check that to see whats going wrong. Alternatively you can redirect the output of cron to a file using 2>&1.

One other thing to try is running the command with sh -x in front of it. (E.g 8 * * * 1-5 sh -x /myscript) This is a debug and gives you tons of output.
Once you see what is or isn't working you can fix from there.

Also check that there are no needed patches for cron. I use Sco Openserver and all versions should have a cron supplement (oss642a) applied. Check if your flavour needs anything like this to fix any little flaws with cron.

by jmcquilling

The cd command should work unless you somehow ran a restricted shell. Cd will appear to do nothing if it cannot find the destination directory so I suspect that the home dir cron is running the job in is not the same dir you have been testing from.One brute force approach is to redirect the output of the cron task to file including error output and put set -x in the front of the script. This will display each command as it is executed.

by why

Don't forget that your profile is not executed by a cron job - so for example if you rely on evirnonment variables set in your profile it won't work unless you . $HOME/profile in the script

Regards

Dave

by rfiore

Make sure that this script is being run by the Borne shell by placing a #!/bin/sh as the first line. Also does the script assume that any variables are already defined? If it does, place an "echo $var", where $var is the variable, in the file to make sure that the variable is indeed defined.

by kshcsuf

Just FYI: on Solaris, all cron jobs have their stdout and stderr logged to logfiles in /var/cron. Take a peek at the logfiles in there and see if you can see anything interesting. I'd also recommend running your shell script with debugging enabled:

#!/bin/sh -x

as the first line of your shell script. This will log every command to stdout (which should in turn be logged into a logfile in /var/cron.)

This discussion is closed to new replies.