Tips to Solve Crontab Not Running Issue

Crontab Not Running
Crontab Not Running

DISCLOSURE: This post may contain affiliate links, meaning when you click the links and make a purchase, we receive a commission.

Using a task scheduler, such as crontab, can be an enjoyable and convenient way to automate programs or scripts at a specific time or interval. However, sometimes your scheduler fails to run tasks as intended, which can be annoying and requires additional effort to resolve.

Encountering strange errors in the task scheduler may particularly be frustrating if you rely on it to perform important tasks on a regular basis. That’s exactly why this article will examine different reasons why your crontab isn’t running and how you can fix these issues yourself!

What Is Crontab

Crontab is a utility that is available on many Unix-like operating systems, including Linux, BSD, and macOS. Its purpose is to schedule commands to be executed periodically, and it is typically installed by default on these systems.

Your crontab scheduler may stop running due to various reasons, which include: incorrect crontab entry, corrupted program, invalid path, incorrect environment variables, permission issues, system issues, or issues with the cron daemon.

That said, if crontab stops running, you will no longer be able to execute scheduled tasks or “cron jobs” as they are commonly called. This means that any tasks scheduled to run at specific intervals or specific times will no longer be executed.

Person Working on Computer With Icons of Planning Schedule

Possible Reasons Behind the “Crontab Not Running” Issue

Encountering an error with crontab is common, but determining the specific cause can be difficult. Understanding the context and potential causes is important to troubleshoot the crontab issue effectively. Some common reasons that may cause crontab not to function properly include:

Invalid Crontab Entry

The crontab utility is located within the “/usr/bin/” directory, which creates/writes crontab files from the “/var/spool/cron” directory. This directory contains separate crontab files for each user on the system. However, failure to run the scheduled task may be due to the invalid crontab entry written in the crontab file.

Even if one crontab entry is invalid, it will affect all other entries which are written within that crontab file. This also means that that specific’s user won’t get his scheduled tasks to be performed automatically until the syntax is corrected.

Corrupted Program

Your crontab may stop running due to a corrupt program or script written to be executed in the crontab entry. This often happens when:

  • You fail to write the correct command in the crontab entry.
  • The script which is expected to be executed doesn’t function properly.

Computer Genius Behind Laptop Breaks the Law, Corrupted Program Code

Invalid Path

Another reason which leads to the failure of crontab is the invalid path that is attached to the command. This path explicitly denotes where your program/script is located so that the crontab may execute it from the correct directory.

If your path is incorrect, your crontab won’t finish its job successfully, or in other words, your desired script won’t run at the specific time.

Incorrect Environment Variables

Invalid environment variables can also become a leading cause of crontab’s failure to run properly. This is because Cron uses environment variables to determine how to run the commands specified in the crontab file.

If environment variables are set incorrectly or not set at all, Cron may be unable to find the necessary programs to execute the commands correctly, which can cause it to stop running.

Permission Issues

Permission denial is one of the major reasons why your crontab fails to run successfully. This is due to the fact that Cron runs commands with the permissions of the user who owns the crontab file.

Workers Working on Computer Network Graphic Overlay

If the user doesn’t have the necessary permissions to access the file or directory or to execute the command, Cron will not be able to execute the command and may stop running. You can view the permissions of a file or directory using the following command in Linux: ls -l

System Issues

In some cases, crontab may fail to function properly due to problems with the system itself. This usually happens when:

  1. The system is experiencing a high load.
  2. There are issues with the system’s clock.

This produces strange behavior where crontab doesn’t execute the commands as expected earlier.

Problems with Cron Daemon

The cron daemon is a system service that runs in the background and is responsible for executing the commands in the crontab files at specific times. If the cron daemon isn’t running, crontab won’t be able to run the commands.

Cron daemon may be manually stopped, disabled, crashed, or killed for another reason which may stop it from running, and in-turn, affect your crontab. However, before reaching to any conclusions, you can check the status of the cron daemon by using the following command:

Businessman Tick Correct Sign Mark for Document

# Check the status of cron daemon on Ubuntu
service cron status
# Check the status of cron daemon on CentOS
systemctl status cron

Finding The Exact Cause From The Log File

Due to numerous reasons, it is difficult to spot the exact cause behind the crontab issue. Therefore, don’t lose hope, as you can always view the system log file for cron located in the var/log/cron directory. You can choose one of the following commands:

CommandWhat It Does
tail /var/log/cronsee entire log file
grep “ERROR” /var/log/cronsearch for errors in log file

Keep in mind that the log file will only show the output from the commands that are run through crontab. This can include any error messages or other output that is produced when the commands are executed.

Best Troubleshooting Methods for the “Crontab Not Running” Issue

Having the exact cause in hand solves half of your problems automatically. With that being said, let’s discover the most common ways in which you can resolve the issue that’s preventing your crontab from running:

Method 1: Validating The Syntax Of the Crontab File

The Crontab file consists of multiple fields containing a specific value or a wildcard (*) to match any value. To validate the syntax of crontab, follow these steps:

Code Syntax on a Computer Screen

  1. Open the terminal window on the Linux system and type: crontab -e. This will open the crontab file for the current user in a nano text editor.
  2. Check that the lines inside the file follow this format: minute hour day_of_month month day_of_week command. This command consists of 6 fields separated by spaces. For example, the following crontab entry will run the command “command_to_run” every day at midnight (00:00): 0 0 * * * command_to_run

From the above example, each field can have the following value:

Field ValueField NameAccepted Value
0minute0-59
0hour0-23
*day_of_month1-31
*month1-12
*day_of_week0-7 (Sunday is 0 or 7, 1 is Monday, and 6 is Saturday)
command_to_runcommandAny program or script with its extension mentioned

Crontab entries also accept some special characters which serve unique functionality, for instance:

CharacterExampleMeaning
** 0 1 1 * /path/to/backup.shRun script every minute on January 1st (any weekday is acceptable) at 12:00 AM
-0-10 0 1 1 * /path/to/backup.shRun script every 10 minutes from 12:00 AM to 12:10 AM on January 1st
/0/10 0 1 1 * /path/to/backup.shRun script every 10 minutes from 12:00 AM to 12:50 AM on January 1st
  1. If you find that the format of your entries was incorrect, don’t hesitate to modify it in order to solve your crontab issue. Also, don’t forget to save the editor before closing it after the modification.

White Save Button Isolated on White

Method 2: Fixing Corrupted Program

Here, you must ensure that you’ve mentioned the extension name beside the program that is expected to be executed. You can consult this in the crontab file by typing this in your command line: crontab -e

An example of a valid crontab entry is: 0 0 * * * /path/to/backup.sh

The above entry will run the command “/path/to/backup.sh” every day at 12:00 AM.

If you’ve already mentioned the extension along with the program name, now you have to ensure that your program is bug-free. You can do this manually by running the script (command of scheduled task) without relying on crontab.

This second technique is useful to those who mistakenly schedule a program/script that was corrupt in the first place.

Method 3: Consulting Path

To specify the path to a command or script in crontab, you can use either an absolute or relative path.

  • Absolute Path

It is the full path to the command or script, starting from the file system’s root directory (/). For instance: /path/to/command

  • Relative Path

It is the path to the command or script relative to the current working directory of the crontab. For instance, if the crontab is located in the “/etc” directory and you want to run a script located in the “/etc/scripts” directory, you can use the following relative path: scripts/script.sh

It is generally recommended to use the absolute paths in crontab, as they are more reliable and easier to troubleshoot. This is especially true if the current working directory of the crontab is not the same as the location of the command or script you want to run.

  • Writing An Absolute Path

You can use the “which” command or the “pwd” command to find the absolute path to a command or script. For instance: which command. This will display the absolute path to the command.

Hacker Hands at Work With Interface Around

Next, you can display the absolute path to the current working directory using this command: pwd

Now, you can use this path in your crontab command. For instance, the following crontab entry runs the command at 2:00 AM every day. 0 2 * * * /path/to/command

Method 4: Setting Environment Variables For Crontab

There are several ways to set environment variables for crontab. One direct way is by using the “env” command to set the environment variables when running a command with Cron. For instance:

# Set PATH environment variable
0 * * * * env PATH=”/usr/local/bin:/usr/bin:/bin” command_to_run
# Set the SHELL environment variable
0 * * * * env SHELL=”/bin/bash” command_to_run

It is essential to note that environment variables set inside the crontab file or in a script run by crontab will only be available to the commands run by Cron. They won’t be available to other processes or the user’s shell.

Businessman Working On Laptop Computer With Electronics Document Icons

Method 5: Granting Permissions

If you need to grant the user additional permissions to access a file or directory, you can use the “chmod” command to change the permissions. Using the following ways, you can grant several permissions depending on their type:

  • Granting Read Write Permissions – chmod u+rw file
  • Granting Execution Permission – chmod u+x command

Method 6: Fixing System Issues

You can check the system’s load using the “top” or “htop” command. Here, you must ensure that the system is not experiencing a high load i.e., its resources are not fully occupied, and there are enough resources to perform the “cron jobs” successfully.

Moving onward, you must also check the system clock by using the “date” command and ensure it is set correctly. If there are issues with the system clock, you can try resetting it or syncing it with a reliable time source.

Method 7: Restarting The Daemon

If you suspect that the cron daemon is not running or is experiencing problems, you can try restarting the daemon using the following command:

Computer Marks Are Restarting.

# Restart the cron daemon on Ubuntu
service cron restart
# Restart the cron daemon on CentOS
systemctl restart cron

Conclusion

We hope that this guide helped you understand the “Crontab not running” error and gave you ideas on how to fix it yourself. While the crontab issue has a plethora of reasons, the sooner you’re able to find out the root cause, the sooner you can fix it!