Saturday, February 22, 2014

A Linux Cron Job

Introduction

In my recent consulting, I was asked to help to complete a task schedule in RedHat Linux environment. Here is the requirement:
  1. The task should run every 5 minutes
  2. If a task takes longer than 5 minutes, no new task should run while a live task is running
  3. The task should not run between 5:30 am and 7:30 am

Solution

Apparently, this is a candidate for using Linux cron job based on the first statement. The requirement requires us to do a bit more than just cron job. I know that the second and third requirement can be done with the shell script. Thus, I decided to write a simple bash script. Of course, this can be done by using Perl as well. For now, I will provide the solution here.

The Cron Definition

The cron job definition is very simple as the following:

*/5     *       *       *       *       /opt/Apexus_DI/bin/ExecuteApexus_DI.sh

Linux/Unix cron job syntax can be in the following form. There are plenty references you can find in the internet. Here is the brief summary.

# * * * * *  command to execute
# ┬ ┬ ┬ ┬ ┬
# │ │ │ │ │
# │ │ │ │ │
# │ │ │ │ └───── day of week (0 - 7) (0 to 6 are Sunday to Saturday, or use names; 7 is Sunday, the same as 0)
# │ │ │ └────────── month (1 - 12)
# │ │ └─────────────── day of month (1 - 31)
# │ └──────────────────── hour (0 - 23)
# └───────────────────────── min (0 - 59)
Field name Mandatory? Allowed values Allowed special characters Remarks
Minutes Yes 0-59 * / , - -
Hours Yes 0-23 * / , - -
Day of month Yes 1-31 * / , - ? L W -
Month Yes 1-12 or JAN-DEC * / , - -
Day of week Yes 0-6 or SUN-SAT * / , - ? L # -
Year No 1970–2099 * / , - This field is not supported in standard/default implementations.

List Of The script

The complete list of the script is shown below. Line 5 - 9 is to check if a job is running. If yes, then just exit. Line 12 and 14 are to check if the time is between 5:30 am and 7:00 am.

Cron Job Log

The cron job logs are located at /var/log/cron. You need to have admin permission to view the log.

No comments:

Post a Comment

Anypoint Studio Error: The project is missing Munit lIbrary to run tests

Anypoint Studio 7.9 has a bug. Even if we following the article: https://help.mulesoft.com/s/article/The-project-is-missing-MUnit-libraries-...