The world of Linux is full of a lot enjoyable and attention-grabbing stuff, the extra we go in, the extra we discover sorts of stuff.
In our efforts to convey these little hacks and ideas for you that make you completely different from others, right here now we have give you just a few different strategies to schedule a job with out utilizing the cron utility in Linux.
Scheduling a job/command in Linux is an acronym to cron. Every time we have to schedule a job, we name cron command, however are you aware we will schedule a job at a later time with out corn? You are able to do it utilizing the next frequent approaches.
1. Scheduling Jobs in Linux Utilizing Sleep Loop
Run a command (say date) each 5 sec and write the output to a file (say date.txt). To attain this situation, we have to run the beneath one-liner script instantly on the command immediate.
$ whereas true; do date >> date.txt ; sleep 5 ; completed &
Anatomy of the above one-liner script:
- whereas true – Ask the script to run whereas the situation is true, it acts as a loop that makes the command run time and again or say in a loop.
- do – do carry out what follows, i.e., execute command or set of instructions that lies forward of do assertion.
- date >> date.txt – right here the output of the date command is being written to a file date.txt. Additionally, be aware that now we have used
>>
and never>
. >>
ensures that the file (date.txt) will not be overwritten each time the script executes. It simply appends the modifications. Whereas>
overwrite the file time and again.- sleep 5 – It asks the shell to maintain a time distinction of 5 seconds earlier than it executed once more. Observe the time right here is at all times measured in seconds. Say if you wish to execute the command each 6 minutes, you must use (6*60) 360, in a succession of sleep.
- completed – marks the top of some time loop.
&
– Put the entire course of in a loop to the background.
Equally, we will execute any script in the identical method. Right here is the command to name a script after a sure interval (say 100 sec) and the title of the script is script_name.sh
.
Additionally value mentioning is that the script above must be run within the listing the place the script to be referred to as lies, else it’s essential to present a full path (/house/$USER/…/script_name.sh
).
The syntax for calling script at above described interval is:
$ whereas true; do /bin/sh script_name.sh ; sleep 100 ; completed &
Observe: The above one-liner will not be a alternative for Cron, as a result of Cron utility helps an entire lot of choices, as in contrast, and could be very versatile in addition to customizable.
Nonetheless, if we wish to run sure check instances or I/O benchmarks, then the above single command will serve the aim.
2. Schedule Duties with Systemd Timers
In most trendy Linux distributions, Systemd is the default init system and it comes with a timer performance that may permit you to schedule your duties.
First, create a brand new systemd timer unit file with a .timer
extension as proven.
$ sudo nano /and so forth/systemd/system/myjob.timer
Add the next content material to the myjob.timer
file:
[Unit] Description=My Job Timer [Timer] OnCalendar=*-*-* 00:00:00 # Change the OnCalendar worth with the specified schedule [Install] WantedBy=timers.goal
The OnCalendar subject in a systemd timer unit means that you can specify the schedule on your job utilizing a particular format.
- Yearly: yearly or yearly
- Month-to-month: month-to-month
- Weekly: weekly
- Every day: day by day or midnight
- Hourly: hourly
- Minutes: You may specify a particular minute utilizing the format
*:MM
(e.g.,*:15
for each quarter-hour) or a particular vary utilizing MM-MM (e.g., 10-30 for each minute from 10 to 30).
Listed here are some examples as an example the format:
## Run day-after-day at 3:00 AM ## OnCalendar=*-*-* 03:00:00 ## Run each Monday and Friday at 10:00 AM ## OnCalendar=Mon,Fri *-*-* 10:00:00 ## Run each half-hour: ## OnCalendar=*-*-* *:0/30:00
Subsequent, create a corresponding service unit file with a .service
extension in the identical listing:
$ sudo nano /and so forth/systemd/system/myjob.service
Add the next content material to the myjob.service
file:
[Unit] Description=My Job [Service] ExecStart=/path/to/your/job.sh # Change "/path/to/your/job.sh" with the precise command or script to execute [Install] WantedBy=multi-user.goal
Allow and begin the timer:
$ sudo systemctl allow myjob.timer $ sudo systemctl begin myjob.timer
This may schedule your job to run in keeping with the required timer.
3. Scheduling Duties Utilizing Anacron
Anacron is a time-based job scheduler that means that you can schedule jobs periodically on methods that aren’t at all times powered on. It’s designed for methods that will not have common entry to cron. If anacron is put in in your system, you need to use it to schedule your job.
If it’s not put in, you may set up it utilizing your package deal supervisor.
$ sudo apt set up anacron [On Debian, Ubuntu and Mint] $ sudo yum set up anacron [On RHEL/CentOS/Fedora and Rocky/AlmaLinux] $ sudo emerge -a sys-apps/anacron [On Gentoo Linux] $ sudo apk add anacron [On Alpine Linux] $ sudo pacman -S anacron [On Arch Linux] $ sudo zypper set up anacron [On OpenSUSE]
Create a brand new configuration file (myjob.sh) on your job within the /and so forth/anacrontab.d/ listing.
$ sudo nano /and so forth/anacrontab.d/myjob.sh
Within the configuration file, specify the main points of your job.
# Run myjob.sh day-after-day with a delay of 5 minutes 1 5 myjob /path/to/myjob.sh
The fields within the configuration file have the next meanings:
- The primary subject is the time interval at which the job must be run @day by day, @weekly, @month-to-month, or @yearly.
- The second subject is the time in minutes to delay the job execution after the system begins.
- The third subject is the job title, which will probably be used to create log recordsdata.
- The fourth subject is the command or script to be executed.
Now, Anacron will mechanically execute your job in keeping with the required schedule.
That’s all for now, if you understand any such Linux hacks or methods you could share them with us by way of our remark part, and don’t overlook to share this text with your pals.