How to Run a Cron Job Every Minute?

The “cron” daemon is the built-in software utility that generally runs the process on the system at a scheduled time. This process can be run at a specific time, i.e., while the time is divided into “hours”, “minutes”, “days”, “days of the month ”, and “days of the week”. The cron reads the crontab file to schedule the predefined command or scripts at the mentioned time interval.

The objective of this guide is to describe the process of running a cron job for every minute.

Prerequisites: Check the cron Service Status

Check the “crontab” before running “Script.sh” as a  cron job every minute. For this purpose, run the “systemctl status” command in this way:

$ sudo systemctl status cron

The green indicator shows that the crontab service is in an “active (running)” state. Press the “q” tab to exit the above command from the terminal.

Tip: Start the Crontab Service (If Inactive)

If the “crontab” service is not active and displays the “inactive” status, then use the “systemctl start” command to start it:

$ sudo systemctl start cron

How to Create and Run a Cron Job Every Minute?

The following essential steps are carried out to schedule the cron job for every minute.

Step 1: Create a Task (For Crontab File)

The first step is to create a specific task into the crontab file that can be scheduled as a cron job for a specific time interval. The “crontab” file is not created by default. Run the below command to create the crontab file in the current working system.

If you are creating the “crontab” file for the first time, this command asks to select the text editor. In this scenario, the “1. /bin/nano” is selected:

$ crontab -e

After that, the new cron tab “ /tmp/crontab.qcIxWj/crontab *” file is created and opened:

Step 2: Insert the Cron Job Into the Crontab File

Scroll down the file and add the following line into the end of the newly created “crontab” file. As we have redirected the output of the “echo” command to a “New.txt” file in the “home” directory that will print out the specified message every minute:

* * * * * echo “Hello Linux at $(date)” >> $HOME/New.txt

The above command contains the following parts:

  • 1st* corresponds to “minutes(0-59)”
  • 2nd* corresponds tohours(0-23)”
  • 3rd* corresponds to theday of the month(1-31)”
  • 4th* corresponds to themonth(1-12)”
  • 5th* corresponds to the “day of the week(0-7)”
  • command: Represents the “echo” command

Save the file by pressing the “Ctrl+S” shortcut key and exit the “nano” editor via the “Ctrl+X” key.

Step 3: Install the New Cron Job

Once the file is saved, the “crontab -e” command status has been changed. As it verifies that the new cron job “Script.sh” is “installed” successfully:

$ crontab -e

Step 4: Verify the New Cron Job

Now, run the cat command to verify the new cron job that is added into the crontab file:

$ cat New.txt

After every minute, the code in the script will automatically be executed.

That’s all about running the cron job every minute.

Conclusion

To run a corn job every minute, we must create a cron job (maybe a Linux command or a bash script) and put it in the crontab file. Access the “crontab” file via the “crontab -e” command. Once done, the job will automatically be executed after every minute. This guide has provided a brief explanation of the process of running a corn job every minute in Linux.