Cron Expression for Every Minute Execution
Cron is a time-based job scheduler used in Unix/Linux operating systems to automate periodic recurring tasks. It is a powerful and flexible tool that helps system administrators or developers to schedule jobs based on date and time.
A Cron expression is a combination of fields that specify the scheduled times for a job. A Cron job can be scheduled to run at a specific time, on specific days of the week, or on specific months of the year.
In this article, we will discuss Cron Expression for Every Minute Execution. We will provide code samples in Bash, Python, and Node.js.
Cron Expression Syntax
The syntax for a Cron expression consists of five fields separated by a space. The fields represent the minute, hour, day of the month, month, and day of the week.
* * * * *
| | | | |
| | | | --- Day of the week (0 - 6) (Sunday to Saturday)
| | | ----- Month (1 - 12)
| | ------- Day of the month (1 - 31)
| --------- Hour (0 - 23)
----------- Minute (0 - 59)
In our case, we want to execute a job every minute, so the Cron expression will have an asterisk () in the minute field and the other fields will have a wildcard ().
* * * * *
This Cron expression means that the job will execute every minute of every hour, every day of the month, every month, and every day of the week.
Cron Expression Examples
Bash Script
To create a Cron job in Bash, enter the following command in the terminal:
crontab -e
This will open the Cron editor. Then, add the following line to the editor to execute the Bash script every minute:
* * * * * bash /path/to/script.sh
Make sure to replace /path/to/script.sh
with the actual path to your script.
Here is a sample Bash script that prints the current date and time every minute:
#!/bin/bash
date +%F\ %T >> /var/log/myjob.log
This script appends the date and time to a log file located at /var/log/myjob.log
every minute.
Python Script
To create a Cron job in Python, first, create a Python script and make it executable:
#!/usr/bin/env python
import datetime
with open('/var/log/myjob.log', 'a') as f:
f.write(datetime.datetime.now().strftime("%Y-%m-%d %H:%M:%S") + "\n")
Save the script and set the executable permission using the following command:
chmod +x /path/to/script.py
Then, add the following line to the Cron editor to execute the Python script every minute:
* * * * * python /path/to/script.py
Make sure to replace /path/to/script.py
with the actual path to your script.
This Python script appends the current date and time to a log file located at /var/log/myjob.log
every minute.
Node.js Script
To create a Cron job in Node.js, first, create a Node.js script:
#!/usr/bin/env node
const fs = require('fs')
const date = new Date()
fs.appendFileSync('/var/log/myjob.log', date + "\n")
Save the script and set the executable permission using the following command:
chmod +x /path/to/script.js
Then, add the following line to the Cron editor to execute the Node.js script every minute:
* * * * * /usr/bin/env /path/to/node /path/to/script.js
Make sure to replace /path/to/node
and /path/to/script.js
with the actual path to your Node.js executable and script, respectively.
This Node.js script appends the current date and time to a log file located at /var/log/myjob.log
every minute.
Conclusion
In conclusion, Cron Expression for Every Minute Execution is a powerful way to automate recurring tasks. Using Cron jobs, you can schedule tasks to run at specific times, on specific days of the week or months of the year. In this article, we provided code samples in Bash, Python, and Node.js for executing a job every minute using a Cron expression. We hope this article helps you understand how to create Cron jobs for your periodic tasks.