Run Cron Job Only If It Isn’t Already Running in Linux



Introduction

Cron is a utility in Linux that allows users to schedule commands or scripts to run automatically at a specific date and time. However, sometimes it may be necessary to ensure that a cron job does not run more than once at a time. In this article, we will discuss two ways to prevent overlapping cron tasks: using process tracking and using a “.pid” file.

Locate running instances by process

One way to avoid overlapping a cron task run is to check for the presence of the task's process before running it. This can be done using the pgrep command, which allows us to search for processes with a specific name and view their process IDs. If the process is found, it means that the task is already running and the new instance shouldn't be running.

To use this method, we can modify the cron task to include the following command at the beginning −

if pgrep -x "script_name" > /dev/null ; then
   exit 0
fi

In the above command, "script_name" is the name of the script or command to run as a cron job. The "-x" option tells pgrep to match the exact process name, and the "> /dev/null" part redirects the output to null so it doesn't clutter up the terminal. If the script is already running, the if statement will be true and the new instance will close without running.

For example, if we have a script called "my_script.sh" that we want to run as a cron job, we can modify the cron job to include the following command −

if pgrep -x "my_script.sh" > /dev/null ; then
   exit 0
fi

This will check for the presence of the "my_script.sh" process before running the cron task. If the process is found, the new task instance will not run.

Use a .pid file

Another way to avoid overlapping cron task execution is to use a “.pid” file. A .pid file is a simple text file that contains the process ID of a running process. When a task runs, it creates a “.pid” file with the task's process ID. If the .pid file already exists, the task is already running and the new instance should not run.

To use this method, we can modify the cron task to include the following commands at the start −

pid_file="/path/to/pid/file/task.pid"
if [ -f "$pid_file" ] && kill -0 $(cat "$pid_file") 2>/dev/null; then
    exit 0
fi
echo $$ > "$pid_file"

In the above commands, "pid_file" is the path to the .pid file, which should be a writable path for the user running the cron task. The if statement first checks if the .pid file exists and then uses the kill command to send a signal to the process ID contained in the .pid file. The "-0" option tells kill to send a null signal, which doesn't actually kill the process, but allows us to check if the process is still running. If the process is still running, the signal will be delivered successfully and the if statement will be true, which will cause the new instance to exit without being executed. If the process is not running, the signal will not be delivered and the if statement will be false, allowing the task to run.

The second line of the if statement, "2>/dev/null", redirects all error messages to null so they don't clutter up the terminal.

After the task has finished running, the .pid file must be deleted to allow future instances of the task to run. This can be done with the following command −

$ rm "$pid_file"

For example, if we have a script called "my_script.sh" that we want to run as a cron job and we want to store the .pid file in the "/tmp" directory, we can modify the cron job to include the following commands −

pid_file="/tmp/my_script.pid"
if [ -f "$pid_file" ] && kill -0 $(cat "$pid_file") 2>/dev/null; then
   exit 0
fi
echo $$ > "$pid_file"

This will create a “.pid” file called "my_script.pid" in the "/tmp" directory and store the process ID of the running task in it. If the “.pid” file already exists, the task is already running and the new instance will not run. Once the task has finished running, the “.pid” file will be deleted, allowing for future instances of the task to run.

Conclusion

In this article, we discuss two methods to prevent cron tasks from overlapping execution. The first method was to use process discovery to check for the presence of the activity process before executing the activity and the second method was to use a “.pid” file to store the activity process ID running. Both of these methods can be useful for ensuring that a cron task doesn't run more than once at a time, which can be useful in cases where the task is resource-intensive or needs to be run only once at a time. It is important to note that both methods require changes to the task script itself and may not be suitable for all types of tasks. However, they can be useful tools for ensuring that cron tasks run correctly.


Advertisements