Run a Cron Job Every Minute

Run a Cron Job Every Minute
If you want to run a program or script in the background on Linux then cron job is very important. With the help of cron jobs, you can execute a program or script in the background after a given interval of time.

Let’s take a look at some of the real life examples of cron jobs.

  • Let’s say, you own a website and you want to send all the users of your website an email every day. All you have to do is, write an email sending script and set up a cron job to run that script every day.
  • Let’s say, you own an advertising agency and you want to remind all the advertisers whose balance is below 5$ to recharge. All you have to do is write a script that checks the balance of all the advertisers and when it’s below 5$, it will send a notification to the advertiser’s mobile number or email. Then set up a cron job to run the script every 5 to 10 minutes, or every hour.

There are many other usages of cron jobs in Linux.

In this article, I will show you how to run cron jobs every minute on Linux. I will be using Debian 9 Stretch for the demonstration. But you can use any modern Linux distribution of your choice. Let’s get started.

Basics of Crontab:

On Linux, you don’t have to be root in order to run cron jobs. You can run cron jobs as any user. Every user on Linux can use a crontab file to run their own set of cron jobs.

By default, a user doesn’t have a crontab file on Linux. You can create a crontab file with the following command:

$ crontab -e

If you’re running this command for the first time, then you should be asked to pick a text editor from the list. I will pick nano, the default one. You can pick the one you like. Once you’re done, press <Enter>.

The crontab file should be created (if not available already) and opened with your favorite text editor.  Now you can add your own cron jobs at the end of this file and once you’re happy, just save it and exit out of the text editor.

Syntax of Running a Command Every Minute:

The syntax of crontab file is as follows:

minute hour dayOfMonth month dayOfWeek commandToRun

Here,

  • minute can be 0 to 59.
  • hour can also be 0 to 59.
  • dayOfMonth can be 1 to 31.
  • month can be 1 to 12.
  • dayOfWeek can be 0 to 7. 0 and 7 means Sunday, 1 means Monday, 2 means Tuesday and so on.

To run a commandToRun command every minute, you should write it in the crontab file as follows:

* * * * * commandToRun

Running a Crob Job Every Minute:

Now that we know the theories, let’s add a simple script timer.sh to the crontab file and see how to manage it.

In the timer.sh script, I only have the following lines of codes. All it does is create a new file /home/shovon/bin/timer.log (if does not exist already) and appends the output of the date command to it.

Now let’s add the script to our crontab and let it run every minute with the following line:

* * * * * /home/shovon/bin/timer.sh

Once you save the crontab file and exit out of the text editor, the new crontab file should be installed.

After a minute is passed, a new file is timer.log is created in the desired directory as you can see in the marked section of the screenshot below.

From the timer.log log file, it is obvious that the script timer.sh runs every minute.

Catching Errors from the Cron Jobs:

To catch errors from a cron job, you can send the errors to a error.log file and normal outputs to access.log file for example. Of course you can name the files anything you want.

To demonstrate this, I modified my script timer.sh a little bit. Now the errors are send to error.log file in the /home/shovon/bin directory and the outputs are sent to access.log in the /home/shovon/bin directory.

At first the /tmp/i_must_be_here file does not exist, so I get the error in the error.log file as you can see.

The access.log file is empty at the moment.

Now I am going to create the file /tmp/i_must_be_here

And as you can see, the output is in the access.log file now.

If you want, you can redirect the output and the errors in the same file as follows:

As you can see, STDIN and STDERR outputs are sent to the out.log file.

Making Sure the Last Job Finished Running Before Running the Job Again:

For this to work, you can create a temporary file just after the job starts and remove it just before it finishes. Then you can check whether the temporary file exists before starting the job. If it does, you can exit out of the job and run the job only when the temporary file is unavailable.

This simple script does just that.

As you can see, the timer.pid file is created.

Reading the access.log file proves that the cron job do not run before the previous cron job finishes running. As you can see, it ran at 01:32:01 and the next time it should’ve run at 01:33:01, but it didn’t. Instead, it ran at 01:35:01, about 3 minutes later.

Organizing Cron Job Outputs for Easy Debugging:

You can format the outputs nicely to make your cron job easier to debug.

An example of how it can be done is given in the following script.

As you can see, the outputs, errors and success messages are nicely printed in the log file.

You can do amazing things with cron jobs and shell scripts. I demonstrated some of the ideas here. But the sky is your limit. Feel free to experiment with any ideas you have. Thanks for reading this article.

Related Posts
Leave a Reply

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