Search code examples
unixdirectoryrelative-pathcron

Crontab - Run in directory


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?


Solution

  • 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).