View Full Version : Crontab Conundrum

03-05-2005, 02:10 PM
I'm having a problem trying to execute a particular script on my server. It is a rebuild mechanism for the publishing system Movable Type ("MT") that is triggered by a request from the crontab. Problem is that Westhost's recommendation as to how the command line should appear differs from MT's recommendation.

Here's what MT suggests

0,15,30,45 * * * * cd /path/to/mt_dir; ./tools/run-periodic-tasks

And here is Westhost's alternative (due to the reason that the semi-colon is invalid)

0,15,30,45 * * * * /path/to/mt_dir/tools/run-periodic-tasks

The problem is that the script does not run properly under Westhost's alternative because it MUST be run from the base directory of MT. I can't use MT's suggested format because the server won't recognize the run command which is apparently due to the semi-colon.

Million dollar question - how do I get the command to run from the crontab out of the base directory of MT?

03-05-2005, 02:37 PM
There are probably a billion ways to do this, but for now try this one . . .

0,15,30,45 * * * * cd /path/to/mt_dir && ./tools/run-periodic-tasks

The difference is the && in the middle instead of the semi-colon.

03-06-2005, 05:50 AM
THANK YOU. I tested this out last night and it worked but I wanted to make sure it wasn't a rebuild that I accidentally triggered while I was working on my site. To make sure I set a future post to be published at 2 a.m. in the morning (the purpose of the script is to publish future posts in MT) and it was there this morning when I logged on.

If you ever need a job I'm sure Westhost could use you. Thanks for the great solution.

03-06-2005, 08:11 AM
Awesome, I'm glad you got it working. :)

I'm actually a little surprised that the semi-colon didn't work.
cd / ; ls I typed that at the command line of my westhost account and it worked correctly. Maybe the semi-colon is only invalid in a crontab file. I'll have to play with that a bit.

Just as a side note . . .
command1 && command2 command2 will only run if command1 succeeded. So in your example if the cd /path/to/mt_dir doesn't succeed then ./tools/run-periodic-tasks will not run. This is probably what you want.

03-06-2005, 11:52 AM
You would (I think) need to escape the ';', for instance, this should work:

0,15,30,45 * * * * cd /path/to/mt_dir \; ./tools/run-periodic-tasks

or this (using quotes)

0,15,30,45 * * * * "cd /path/to/mt_dir; ./tools/run-periodic-tasks"

But I haven't tried them... ;)