“crontab” is a robust tool that can be utilized for automating tasks on Linux-based operating systems. It enables users to schedule scripts or commands to execute automatically at predetermined intervals or times, which can save time as well. More specifically, configuring crontab can increase productivity and minimize the chance of human error by automating repetitive processes like backups, updates, and reporting, and scheduling security-related operations, such as system scans.
We have used Ubuntu 22.04 for experimentation.
This blog will cover the following approaches:
- Method 1: Running a crontab on Monday and Thursday Using the crontab Command
- Method 2: Running a crontab on Monday and Thursday Using the systemd timer
Method 1: Running a crontab on Monday and Thursday Using the crontab Command
In Linux-based operating systems, “crontab” is a time-based job scheduler that permits the users to schedule scripts or tasks. These tasks then automatically get executed at the defined days of the week, times, or intervals.
To utilize it, have a look at the given syntax.
In the syntax of crontab, each entry consists of five fields, separated using spaces:
|* * * * *|
The below-given diagram illustrates the syntax of a cron job:
Here, each field refers to a date value or time unit. Moreover, you can also utilize special characters for defining a range of values or specific patterns.
Now, head toward the provided instructions for running a crontab on Monday and Thursday.
Step 1: Edit crontab File
As the first step, press “CTRL+ALT+T” to open up the terminal, and then run the given command for editing the crontab file:
As a result, you will be asked to choose an editor. In our case, we have selected the “nano” editor which is easiest to use:
Step 3: Run a cron job on Monday and Thursday
Now, add the following line in the crontab file for defining a cron job that runs on Monday and Thursday:
0 0 * * 1,4 /path/to/script
Here is the breakdown of the above-given command:
- In the first field, “0” refers to “at minute 0” (i.e., at the start of the hour).
- In the second field, “0” refers to “at hour 0” (i.e., at midnight).
- In the third field, “*” represents “every day of the month”.
- In the fourth field, “*” represents “every month”.
- “1,4” in the fifth field means “on Monday (1) and Thursday (4)”.
- Lastly, “/path/to/script” refers to the path of the script you want to run at the specified schedule.
Following the given syntax, we will set up a cron job to run the “my_script.sh” at midnight on Mondays and Thursdays of every month:
0 0 * * 1,4 /home/user/scripts/my_script.sh
Hit “CTRL+O” to save the added changes and press “CTRL+X” to exit the editor:
It can be observed from the highlighted message that the new crontab has been defined successfully:
Method 2: Running a crontab on Monday and Thursday Using the systemd timer
You can also utilize “systemd timer” for scheduling a user job or system service periodically or at a defined time. The timer can be set up to execute a unit at the specified time. Moreover, systemd timer can be integrated with systemd to get benefits from other features, such as logging, service monitoring, and dependency management.
For the purpose of executing a crontab on Monday and Thursday by utilizing the systemd timer, check out the below steps.
Step 1: Create/Make a systemd timer Unit File
Write out the following command for creating a systemd timer unit file:
sudo nano /etc/systemd/system/mytimer.timer
Step 2: Set up a cron job
Next, add the provided code for setting up a cron job on Monday and Thursday:
[Unit] Description=My Timer[Timer] OnCalendar=Mon,Thu *-*-* 00:00:00 Unit=myjob.service[Install] WantedBy=timers.target
Note: According to the code, the “myjob.service” systemd service will run at midnight (00:00) on Monday and Thursday.
Save the changes by pressing “CTRL+O” and exit by hitting “CTRL+X”:
Step 3: Create a systemd service Unit File
Then, create a new systemd service unit file with the help of the nano editor:
sudo nano /etc/systemd/system/myjob.service
Add the following lines to the file to specify the script or command that you want to run. For instance, we have defined the path for the “my_script.sh” script:
[Unit] Description=My Job[Service] ExecStart=/home/user/scripts/my_script.sh
Save the code and exit the nano editor:
Step 4: Reload systemd daemon
Now, it’s time to reload the systemd daemon:
sudo systemctl daemon-reload
Step 5: Enable timer
Then, enable and start the timer accordingly:
sudo systemctl enable --now mytimer.timer
Step 6: Verification
Lastly, verify whether the timer and the relevant service have been executing or not:
sudo systemctl list-timers
It can be observed that both timer and service have been running successfully:
That’s how you can easily run a crontab on Monday and Thursday on your system.
For the purpose of running a crontab on Monday and Thursday, utilize the crontab file or the systemd timer. In the first approach, open up the crontab file in the nano editor and add the “0 0 * * 1,4 /path/to/script” line. Whereas, in the second method, firstly, create a systemd timer unit file and define the cronjob. Then, create the system service unit file, reload the daemon, and start the timer. This blog covered two effective approaches for running a crontab on Monday and Thursday.