How to Troubleshoot Crontab Problems

Knowing how to troubleshoot your cron services will help determine why your script isn’t running as expected.
There are several things you can check when you need to troubleshoot CRON:

View Cron Service Stats

Get PID of Cron

Script Must be Executable

Viewing Syslog Messages About Crontab

Crontab INSECURE MODE

During backup and restore specifically, you might need to correct a permission. The requirement is only the user to have read and execute permissions to the /var/spool tab

You will need to reload the crontabs after a permission change.

Comments in Crontab

# you can add a hash in front to comment/note

Echo the Path in Crontab Scripts

Most errors are caused by path being incorrectly used when calling the script. By default only /bin and /usr/bin are available. Add this to your script that you call from crontab and look at what path shows. Add a line that tells you the path your script sees in the top of your script

You can force the path manually by adding this to the crontab -e:

Or you can modify each script with a special variable:

Which Path for Cron Scripts?

You can always use “which ” to find the path you should use, and put the whole path in your script, if you are struggling with figuring out paths.
You can see what the actual path should be using the “which” command:

Echo the Entire Cron Environment in Script

Just like path problems will prevent the script, so will environment variables. Add this to your script that you call from crontab, then analyze your environment.

Nested Crontab Variables

You can add variables to crontab -e and they will work. However, it will not work to use nested variables (a variable in another variable like bash scripting).

Last Line of Cron Job Doesn’t Run

Cron requires all commands to be terminated with a new line. Manually add a new line in your script or make sure the dos2unix has been run on your script and then view it again in vim to make sure line breaks and new lines are as expected.

Crontab Errors in Formatting

Cron doesn’t like special characters, and one prime example is the % sign. You must escape the % if you use it. This will happen if you try to use auto generated dates without escaping them. The correct format is:

Bash Shell vs sh

Try running the command in sh

or force it to run in bash:

Or force all cron jobs to use bash in top of crontab -e

Make sure your scripts also have proper shebang:

Leave a Reply

Your email address will not be published. Required fields are marked *

This site uses Akismet to reduce spam. Learn how your comment data is processed.