When you want to automate some scripts, you will need a way to execute it in certain timing. One way to automate your script is with the use of cron/cronjob.
Table of Contents
What is cron and/or cronjob?
The software utility cron is a time-based job scheduler in Unix-like computer operating systems. Users that set up and maintain software environments use cron to schedule jobs (commands or shell scripts) to run periodically at fixed times, dates, or intervals. It typically automates system maintenance or administration—though its general-purpose nature makes it useful for things like downloading files from the Internet and downloading email at regular intervals. The origin of the name cron is from the Greek word for time, χρόνος (chronos).
from Wikipedia
If you have a repetitive task you want to schedule a job to run some scripts, cron and/or cronjob will be very useful approach to help you accomplish your repetitive tasks.
How to use the cronjob
Cron or cronjob is available on Unix-like system such as Linux Ubuntu. The cronjob usually lives in /etc/cron.d
(there is also cron.daily
, cron.hourly
, cron.monthly
, cron.weekly
but we can focus on cron.d
)
cronjob task
cronjob is a file where you need to set a timing to execute some action. Follow the below steps to create a sample cronjob.
Create a cronjob file
Filename can be pretty much anything. You don’t need an extension name. Just the name of the job.
Let’s go ahead and create a cronjob file. Open the terminal on your Linux system. (I am using Ubuntu for this example) and type the following line of command.
sudo nano /etc/cron.d/timestamp-cron
It will open a nano editor within terminal. Type the following in the nano editor. (Note: make sure there is a blank line at the end of the file)
* * * * * root date >> /time_stamp.txt
Control + o
and press Enter to save the file
Control + x
to exit the nano editor
This will create a cronjob file with the name timestamp_cron
.
cronjob Format
The format of cronjob is consist of minute, hour, day of the month, month, day of week, user to execute the task, command to execute. Take a look on the below commands.
# m h dom mon dow user command
# * * * * * root date >> /time_stamp.txt
* (asterisk) means, all the time. So, if you have all the schedule attributes with * (asterisk), execute every minute, every hour, every day of the month, every month and every day of the week. So for the above example, it will execute the date >> /time_stamp.txt
command every minute. However, if you modify to something like:
00 6 * * * root date >> /time_stamp.txt
This meas, run that command every 6 am in the morning.
By the way, date >> /time_stamp.txt
is simply outputting the date and time to time_stamp.txt
file.
How to start the cronjob
cronjob will automatically start once the cronjob file is created. As long as it has the correct format, correct command (and command path) to execute.
How to check if the cronjob is working
Couple of checkpoints you can consider if the cronjob is working correctly or not.
Check the outcome of your cronjob task
For example, if your cronjob task is to output the timestamp to a text file (for example, date >> /time_stamp.txt
), check if time_stamp.txt file has been created when it reaches the time it supposed to be executing the job. (for example, if it scheduled * * * * *
, this means it is expected to write a timestamp in every minute.
Type tail -f /time_stamp.txt
You should be seeing the content of time_stamp.txt in the terminal and you should be seeing the series of time stamp in a minute interval something like below:
Note: use Control + c
to terminal the tailing.
Enable the cronjob logs
You can enable the cronjob logs by following the below:
Type sudo nano /etc/rsyslog.d/50-default.conf
and press enter
Once the 50-default.conf
opened in nano editor, un-comment the line with cron.*
Control + o
and press Enter to save the file.
Control + x
to exit the nano editor.
Restart the rsyslog service
Type sudo service rsyslog restart
Restart the cron service
Type sudo service cron restart
Now check the cron logs in /var/log
Type sudo nano /var/log/cron.log
How to stop the cronjob
You can stop the cronjob in two ways.
1. Commenting the task
Simply add #
in front of the task in your cronjob file. Make sure to save the cronjob file after you commented the task.
This method is better if you just want to temporarily disable the cronjob task.
2. Delete the cronjob file
If you do not need the cronjob task anymore, you can simply delete the cronjob file itself.
You can delete using rm
command
sudo rm /etc/cron.d/timestamp-cron
You can verify if the cronjob has stopped by visiting the cron.log file.
sudo cat /var/log/cron.log
You will see the previous execution of cronjob but pay close attention to the date and time of the execution. There should not be new entry in the log file.
Latest Posts
- How to convert MD (markdown) file to PDF using Pandoc on macOS Ventura 13
- How to make MD (markdown) document
- How to Install Docker Desktop on mac M1 chip (Apple chip) macOS 12 Monterey
- How to install MySQL Workbench on macOS 12 Monterey mac M1 (2021)
- How to install MySQL Community Server on macOS 12 Monterey (2021)