What is the crontab working directory?

By: Shane Harter|Last Updated: Jan 14, 2024

A common problem when setting up a cron job is a misunderstanding about the working directory that cron uses when running jobs. In this guide we will explain the cron job working directory, and how to fix any problems you're having with it.

The cron working directory is the owner's home directory

When cron jobs are invoked they are run as the user who owns them. This may be different from the user who schedules them. Figuring out the owner is fairly easy:

When creating cron jobs asCron job "run as" user is
$ crontab -eThe current user
$ sudo crontab -eThe root user
$ sudo -u foo crontab -eThe user foo
File in /etc/cron.d/*The user specified on the cron line, or root
Entry in /etc/crontabThe user specified on the cron line or root

Once you've identified which user a cron job is run as, with sufficient privileges, you can drop into their home directory and run the script yourself.

 $ cd ~username

Want alerts if your cron jobs stop working?

Monitor your cron jobs with Cronitor to easily collect output, capture errors and alert you when something goes wrong.

Fixing problems with the cron working directory

When scheduling cron jobs, you have a few options for dealing with the working directory.

1. Use absolute paths when possible

Knowing that jobs are run from the home directory, it can be tempting to quickly hack the relative path to whatever script or command you are trying to run. Your cron job might work fine -- as long as nothing ever changes. Instead, you should put a few extra minutes in when scheduling your job to be specific about what you are trying to run.

2. You might need to change the directory

Invoking a script with an absolute path is more reliable and durable than using a relative path, but for some cron jobs, that may not be enough. If your script is invoking other commands using a relative path, you may need to cd into the same directory as your cron job. Here's an example using an ever-hour cron job:

 0 * * * * cd /path/to/command ; cron-job.sh

3. Or, add the directory you need to the cron path

When you run a command, the PATH variable is checked to know where to look for scripts and commands that are run. By default cron has different environment variables, but you can set a PATH directly in your crontab:

 PATH=/root/it/scripts
 0 * * * * cron-job.sh

4. Try to be consistent

Cron is flexible and can run commands that live anywhere, but your life will be easier if you have the self-discipline to always deploy cron jobs the same way. One idea we like is to add all cron jobs to the same directory and to always invoke that directory when running your job.

In this example, we are always adding cron jobs into Joe's cron directory and using a crontab environment variable to keep the code in our crontab clean.

$ crontab -l
CRON_DIR=/home/dataproc-user/cron
0 * * * * cd $CRON_DIR ; cron-job.sh
*/15 * * * * cd $CRON_DIR ; python other-cron-job.py

With a clear understanding of how cron invokes your jobs and adherence to a few best practices your cron jobs will be more reliable and more durable.