How to Check if a Cron Job is Running

How to check if a Cron is running

A fairly common question about scheduled tasks that run in the background is, “Are they running?” These tasks, commonly known as cron jobs, need to run automatically for maintenance, backups, and other functional needs for the website and server.

To determine if your cron jobs are running, you will need to distinguish between the scheduling of the tasks (the crontab) and the actual functions themselves (the cron jobs). Cron jobs that do not produce output results require you to become familiar with the server logs. Upon determining the results of the server logs, you can then troubleshoot cron jobs that are not running. You can also modify a script so that it produces an output to show that it is running.

The following article will help you distinguish between a crontab and a cron job, describe two ways to see when a cron job is running, and troubleshoot cron jobs that are not running.

NOTE: To perform this function, you must have root access on your VPS or Dedicated server.

Difference Between Crontab and Cron Job

Understanding the meaning of the terms “crontab” and “cron job” can help prevent confusion between a beginner administrator and a developer or experienced server administrator.

If you search for “scheduling cron jobs“, you will find the term crontab defined or described with little introduction. Crontab is short for “cron table” and is a Linux command for scheduling cron jobs. It is also the actual text file that contains the scheduling for cron jobs.

A cron job is a code or script you are trying to run in the background at a particular scheduled time. The Linux command for a cron job is simply cron.

So, if someone asks you how a cron job is scheduled in the crontab, you know that they are referring to the timing and frequency information applied to the cron job in the text file known as the crontab.

For cPanel, the crontab and cron job can be edited directly in the cPanel interface on the Cron Jobs page.  Here, you can see the upper portion of the Cron Jobs page:

Cron Jobs page - upper half

If you have any existing cron jobs, then you will see them at the bottom of the Cron Jobs page.  You will see the timing for the cron job (from the crontab file) and the code for the cron job.  You can add, edit, and delete cron jobs and their corresponding schedules on this page. Here’s how the bottom section looks:

Cron Jobs page - lower half

Using the crontab information provided in cPanel you can determine when a cron job was supposed to run. In some cases, cron jobs will not generate an output, so it is important that you become familiar with the generated log files and how to search them.

2 Ways to See If Your Cron Job is Running

Checking the Log Files

grep "script.sh" /var/log/cron

If you were to search for a different script or term in the log file, then you would replace “script.sh” with the name of your script file or text string that you’re trying to find in the log.

Using the grep command, you can view the log to see the last time the script in the cron job was executed.  If the cron job does not produce a visible output, then you would need to check to see if the cron job has taken place. 

For example, you can see the last time when a cache clearing cron job was executed. You would then check to see if the cache is cleared by seeing if the cache is empty or not.  Here’s an example of seeing a cronjob running by checking the log:

The log shows a record of when the file was run. Please see our tutorial if you need information on accessing the terminal through cPanel.

Using Output In a Script to Show a Running Cron Job

The easiest way to see if a cron job (with its crontab settings) is working is to edit an existing cron job so that it produces a visible output. You can add a line of code in your existing script to output a result when the script is run. If the result of this command produces an output, then you can use this output to confirm that your cron script is running.

The modification of a cron job script should not be performed by a user who is not familiar with the code or script being executed. Since cron jobs can be written in many different ways, you may need to speak with an experienced developer to help prevent any issues with your website.

Troubleshooting Cron Jobs

If you are not seeing any activity in the crontab log or if you are not receiving an email regarding the cron activity (for cron jobs with an output), then you will want to first double-check that the time parameters for the cron job are correct. This will help to determine if (and when) the cron is supposed to be running so that you can properly troubleshoot possible issues.

If you still suspect a problem with the cron job, then there are usually 3 possibilities as to why it might not be running:

  1. The cron daemon (the system process that runs cron jobs) could not run the script or was unable to send an email
  2. There were issues with the mail server mailing the output, or the email was lost
  3. The cron job did not produce an output or error message

In all three cases, the cron job is silently failing. In other words, there is no visual result of the cron job running.

In the first possibility, you will need to check the system log on the web server to search for errors that may be recorded during the attempted execution of the cron job. You can then use the error message to isolate where the cron job script may be failing and make necessary corrections.

You may also need to go through the mail server log to double-check if there was an attempt to send an email after completing the cron job. Remember that the designated email address is near the top of the cron job page in cPanel. You can use the email address to isolate the mail server log’s mail attempts.

You would also check the mail server log if you see the record of the cron job running but no expected email. Emails are sent when cron jobs are successfully completed and have an output.

In the third possibility, you will need to add code to your cron job so that it produces an easily tracked output that allows you to verify that it is running.

If you are unsure how to check mail server logs, contact your hosting service’s technical support or work with an experienced administrator to use the logs to help you identify problems related to the mail server.

A cron job can fail in many ways. If you are not sure how to code cron jobs, consult with an experienced developer to help you make corrections or modify code for troubleshooting purposes.

If you need more information about setting up cron jobs in cPanel, you can read our full guide.

Make sure your business, agency, or reseller clients are always connected and powered on with our optimized Managed VPS Hosting.
AC
Arnel Custodio Content Writer I

More Articles by Arnel

18 thoughts on “How to Check if a Cron Job is Running

  1. Actually i have create one script for dev env. my query was i am given some x value. but script was executed in before timing.
    Anyone why its is running before time.

    1. Hello! Just to check, did you confirm that your Development Environment’s time is using the time zone you expect? If you did not configure that yourself, it may be set up to use something automatically that is incorrect. Beyond that, I’d suggest running your CRON script through a tool like crontab and double checking that it works as you want it to. Hope that helps!

    1. I recommend you check the logs as indicated in this guide to determine why the cronjob is not running as expected.

      I also recommend you check what user you are using for SSH and what user is running the cronjobs to figure out why crontab is not displaying anything.

      I do apologize, however, I am not able to determine why your cronjob is not working, with the details you have provided.

  2. Just a quick tip if you’re looking for a cron job that ran in the past run
    grep “cronscript.php” /var/log/cron-20171008 (which says cron file that is logged at 2017-10-08)

    1. Hello Simon,

      You should not have to, is the code above not working for you?

      Best Regards,
      TJ Edens

  3. i want to find how many jobs are running and on which server  these things i want but didnt get using these commond plz help me. 

     

    1. Hello Sreenu,

      You can see any crons running on the server by checking /var/spool/cron for the given user. And you will see the active crons running in the chosen user’s “crontab”.

    1. Hello Kenny,

      If you have a mailto line setup within your crontab then you should get emails from any failing cron jobs.

      Best Regards,
      TJ Edens

  4. I do not want to have to login via ssh to find out what happened with my cron job. I have an email address associated with that job and I want to get an email telling me if the job ran correctly or not. I currently get an email on one of my other jobs, so why am I not getting one on this cron? It is:
    find public_html/test -type f -name “*-invoicePDF*”

  5. I think this article is right to the point and let us know where to look exactly. No need for any improvement acc to me.

    Thanks

Leave a Reply

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