What is the cron job working directory

A common problem when setting up a cron job is a misunderstanding about the working directory that cron uses when invoking your job. Clear up any confusion with the best practices in this guide.

You can't trust cron jobs!

We created Cronitor after a critical cron job failed silently for months. Cronitor is easy to use and provides you with instant alerts when things go wrong. Learn more

Cron jobs run in their owner's home directory

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

When creating cron jobs as... Cron job "run as" user
$ crontab -e The current user
$ sudo crontab -e The root user
$ sudo -u foo crontab -e The user foo
File in /etc/cron.d/* The user specified on the cron line
Entry in /etc/crontab The user specified on the cron line

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

Avoiding problems with the cron working directory

When scheduling cron jobs, we follow 3 simple best-practices to make life easier.

  1. Absolute, not relative

    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. 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. 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/joe-user/cron
    0 * * * * cd $CRON_DIR ; cron-job.sh
    */15 * * * * cd $CRON_DIR ; python other-cron-job.py
  4. With a clear understanding of how cron invokes your jobs and adherance to a few best practices your cron jobs will be more reliable and more durable.


Our software, CronitorCLI, includes the free tools we always wished we had.  Scan your system and list every cron job in seconds using cronitor list.  Run any cron job instantly using cronitor select.