How to run a cron job manually

By: Shane Harter|Last Updated: Dec 13, 2023

When scheduling a cron job you sometimes will find that the job works perfectly when run from the terminal, but fails when cron runs it at the scheduled time. This guide will show you how to run a cron job manually and immediately, directly from your terminal and exactly the way cron does.

1. Start with the correct user account

If the cron job is scheduled in your user crontab (e.g. crontab -e) the command will be run as you, not by root.

If the job is in a systemwide crontab like /etc/crontab or a file in /etc/cron.d/ it's allowed to specify a user between the cron schedule and command. Jobs without an explicit user are run as root. In this example, the job is run as user dataproc:

$ cat /etc/crontab
4 5 * * * dataproc /home/crons/

After you determine the user account cron is using to run your job, move on to step 2.

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.

2. Use the right shell

The shell is the application that prints your command prompt and executes your commands. The most common shell in modern linux distributions is bash. When you run a command from your terminal you're asking bash to start the process and display the output. When cron invokes your commands, it uses a different shell by default, /bin/sh and there are a few important differences:

  • When you login to a terminal your .bashrc and .bash_profile scripts are executed automatically. This doesn't happen when cron runs your command so any environment variables created here will be missing in your cron jobs.
  • Bash has special built-ins and features that will cause syntax errors under cron.

Running a command with /bin/sh can be done several ways and in this guide we will be passing the command in as a string, like this example that will use /bin/sh to print a Hello World:

$ /bin/sh -c "echo 'Hello World'"
Hello World

Adding a SHELL=/bin/bash declaration at the top of your crontab will give you modern bash features but will not fix problems with .bashrc and .bash_profile

3. Know where to start the job

Cron starts each command from the selected user's home directory and in the example from step 1, the dataproc job will be run from /home/dataproc.

That might not be the end of it - your crontab command itself might customize the working directory with a cd, so check that too.

$ crontab -l
4 5 * * * cd /home/crons && ./

4. Conclusion

Combine these 3 steps in a single command to create a testing environment for your cron job with the same parameters that will be used by cron when your job is run on schedule.

$ sudo -u <USER> /bin/sh -c "cd ~ && <COMMAND FROM CRONTAB>"