Crontab - Run in directory

user333746 picture user333746 · Jan 17, 2012 · Viewed 86.3k times · Source

I would like to set a job to run daily in the root crontab. But I would like it to execute it from a particular directory so it can find all the files it needs, since the application has a bunch of relative paths.

Anyway, can I tell crontab to run from a particular directory?

Answer

All jobs are executed by a shell, so start that shell snippet by a command to change the directory.

cd /path/to/directory && ./bin/myapp

Concerning the use of && instead of ;: normally it doesn't make a difference, but if the cd command fails (e.g. because the directory doesn't exist) with && the application isn't executed, whereas with ; it's executed (but not in the intended directory).