Fix: Cron Script does not Execute as Expected from crontab

In Linux, the cron is a command line utility allowing users to schedule tasks repeatedly at a specific time. Cron is a daemon, a background process that executes non-interactive jobs and is the same as the Services on Windows. When executing the cron script, some users reported that it does not execute as expected from crontab. It could have multiple reasons, such as the cron daemon’s inactive status, incorrect script, invalid path, or syntax errors in the script.

This guide explores the reasons and fixes for the cron script not executing as expected from crontab.

  • Reason 1: The cron Daemon is Inactive
  • Solution: Activate the cron Daemon Service
  • Reason 2: Invalid Path of the cron Job
  • Solution: Validate the Path of the cron Job
  • Reason 3: No Execute Permissions for the Script
  • Solution: Allow Execute Permissions for the Script
  • Reason 4: Incorrect Script
  • Solution: Check the Script for Errors

Reason 1: The cron Daemon or Service is Inactive

In Linux, the “crontab” is assigned a background service called daemon. It checks if a job is scheduled to be executed in the background at a specific time. One of the major reasons why the cron script is not executing is that the daemon service is inactive. To check its status, use this command:

$ sudo systemctl status cron

The above image shows that the cron daemon service is inactive(dead). Let’s activate it.

Solution: Activate the cron Daemon Service

To activate the cron daemon service, use this command:

$ sudo systemctl start cron

The above image confirms that the cron daemon service is started.

Reason 2: Invalid Path of the cron Job

While executing the scripts, the path is often the issue, and users must ensure that the script’s absolute path is valid. The scripts and their paths are defined in the “/tmp/crontab.fDNOaT/crontab” file, and it can have a different name in your system. 

Solution: Validate the Path of the cron Job

Use this command to open the file /tmp/crontab.fDNOaT/crontab” and enter “1” if prompted:

$ crontab -e

Scroll down to the bottom and add the desired script. In the below image, we have added a script with an invalid path just to help you understand. The actual path of the script is /home/itslinuxfoss/scripts/Script.sh:

Let’s add the valid path to it, which is the absolute path to the directory where the script is placed on the system.

The path to the script is now valid, and this should fix the problem because nothing will be executed when it does not find the script at the specified location.

Reason 3: No Execute Permissions for the Script

The script cannot be executed if there are no execute permissions. To view the permissions, for example, “Script.sh”, use this command:

$ ls -l /home/itslinuxfoss/scripts/Script.sh

By looking at the above image, the “x” cannot be seen for the execute permissions. Learn more about file permissions in this detailed guide

Solution: Grant Execute Permissions

To change permissions, the chmod+x command is used in this way: 

$ sudo chmod +x /home/itslinuxfoss/scripts/Script.sh

The above command adds the execute permissions to the “Script.sh” file in “/home/itslinuxfoss/scripts.” To confirm the changes, check the permissions again using this command:

$ ls -l /home/itslinuxfoss/scripts/Script.sh

The above image confirms the execute (x) permissions to the script Script.sh.

Reason 4: Incorrect Script

A human error is always expected while writing the scripts, and the script cannot be executed when there is an error. For example, in Script.sh file, the user forgot to add the “Shebang”, as seen in the below image:

Solution: Fix the Script

In the above image, the Shebang is missing. Let’s add the “Shebang” into the “Script.sh” file like this:

The issue should now be resolved by following the methods mentioned above.

Conclusion

To fix the problem “cron script does not execute as expected from crontab”, users must start the crontab daemon, provide a valid path & execute permissions and fix the errors in the script. The cron script will not execute if the crontab daemon is stopped, the path is not valid, no execute permissions or the errors in the script. This guide provided the possible fixes for “Cron Script does not Execute as Expected from crontab.”