TechMende.com
Easy Technology Tips, Tricks & Guides

4 Ways to Check Whether cron Is Working on Linux

cron is a job scheduler for Linux and Unix-like operating systems. It comes pre-installed on all Linux distributions and is most suitable for automating repetitive tasks.


For cron to function properly, you must periodically check that the utility is running fine on your system. You can do that using four different ways, including checking the cron service status, examining cron logs, running a test cron job, and listing down running processes on your system.


What Is cron?

The silent job handler, cron, automates and schedules system tasks. Users who configure and maintain software environments make use of cron to schedule jobs such as commands or shell scripts—also called cron jobs—to run periodically at fixed times or intervals.

cron automates system maintenance or administration tasks that you might need to carry out frequently.

How to Check if cron Is Working on Linux

Here are some of the ways you can check if cron is working properly:

Method 1: Check the cron Service Status on Linux

One way to check whether cron is working is by checking the status of the cron service by running a basic Linux command. Open the Linux terminal by pressing Ctrl + Alt + T and use the systemctl command to check the status of cron:

 sudo systemctl status cron 

If you see the following output, this means cron is active and running fine on your system.

cron status displayed on ubuntu terminal

But what if cron is in an inactive state as shown below?

cron service stopped on ubuntu

You can start the service like this:

 sudo service cron start 

After starting the cron service, enable it so that it starts every time the system reboots:

 sudo service cron enable 

Method 2: Check cron Logs on Linux

Another way to check if cron is working properly is by examining the log files. cron logs are stored in the /var/log/syslog directory on Linux.

syslog is a protocol that Linux systems use to centralize event data logs. Logs are then accessed to perform audits, monitoring, troubleshooting, reporting, and other necessary IT operational tasks.

You can see the cron job logs in the syslog file by executing the following command:

 grep CRON /var/log/syslog 

You will see a similar output indicating that cron is running fine and its logs are being stored in the log file:

cron logs displayed on ubuntu terminal

As the syslog folder contains other system logs along with the cron logs, it can be a little tricky to examine only the cron-related logs in the file. You can resolve this by creating a separate log file that only stores cron entries that appear in the syslog file.

To do that, open the following file using the nano editor:

 nano /etc/rsyslog.d/50-default.conf 

Locate the line that starts with:

  

Uncomment this line by removing the hash (#) sign. Then, save and exit the file by pressing Ctrl + X, then press Y and hit Enter.

Restart the rsyslog service by issuing this command:

 sudo service rsyslog restart 

You will now receive logs in the cron.log file located in the /var/log directory.

If your terminal fails to fetch any logs, that could mean cron is not running. In that case, confirm if cron is working or not using other methods.

Method 3: Running a cron Job on Linux

You can also check if cron is working by simply creating a test cron job and executing it. If the job succeeds in performing the task, this means it's functioning fine.

You first need to create a Bash script. Bash scripting allows you to automate everyday tasks on Linux.

Let's create a Bash script that will print “my cronjob is working!” into a TXT file. To create the file, first, find out your present working directory using this command:

 pwd 

Create a file in the current directory using the touch command:

 touch file.txt 

After this, create a Bash file using nano:

 nano script.sh 

Add the following contents to the file:

 #! /usr/bin/bash
echo "my cronjob is working!" >> /home/username/file.txt
bash.sh script created for cronjob

Make sure to provide the correct location of the text file that you created. Save and exit the file by pressing Ctrl + X, then Y, and hit Enter.

Give execute permissions to the Bash script:

 chmod +x script.sh 

Now create a cron job to execute the script. To do that, open the crontab file with:

 crontab -e 

Enter the following line at the end of the file.

 * * * * * /path/to/script.sh 

The five asterisks match the time of execution, in which the first asterisk represents the minutes, the second one represents hours, the third asterisk means the day, the fourth one indicates the month, and the last asterisk indicates the year.

The name of the executable file and its path is also mentioned in the file.

After saving and closing the file, you will see a “crontab: installing new crontab” message on the terminal.

To check if the cron job worked or not, go to the directory of file.txt and print its contents on the terminal using cat:

 cat file.txt 
ubuntu terminal displaying contents of a text file

This indicates that cron is working fine.

Method 4: Check Running Processes on Linux

Another way you can check if the cron daemon is working is by listing down the running processes on your system. You can achieve this using the ps command. The cron daemon will show up in the output as crond.

 ps -ef | grep crond 
ubuntu terminal showing crond process is running

This confirms that the cron process is running on your Linux system.

Automate and Schedule System Tasks With cron

cron is one of the most vital utilities on Linux that allows the system to perform efficiently. Sometimes you have to check whether cron is working while troubleshooting system issues.

You can achieve this by either checking cron logs or seeing the running processes on your system. You can also check the cron status using the systemctl utility. Besides that, running a test cron job can also tell you if the service is running or not.

The smooth working of cron allows you to automate and schedule everyday system jobs. Most of the cron jobs are automatically created by installed applications. Automating and scheduling tasks improves the overall system performance and keeps it healthy.

Linux,Troubleshooting,Linux Commands

>>Here is the Original Post!

Leave A Reply

Your email address will not be published.