tecmint.com-How to Schedule a Linux Job Without Cron
tecmint.com-How to Schedule a Linux Job Without Cron
tecmint.com/schedule-job-without-cron-linux
The world of Linux is filled with so much fun and interesting stuff, the more we go in, the
more we find kinds of stuff.
In our efforts to bring those little hacks and tips for you that make you different from
others, here we have come up with a few alternative methods to schedule a job without
using the cron utility in Linux.
Table of Contents
while true – Ask the script to run while the condition is true, it acts as a loop that
makes the command run again and again or say in a loop.
do – do perform what follows, i.e., execute command or set of commands that lies
ahead of do statement.
1/4
date >> date.txt – here the output of the date command is being written to a file
date.txt. Also, note that we have used >> and not >.
>> ensures that the file (date.txt) is not overwritten every time the script executes. It
just appends the changes. Whereas > overwrite the file again and again.
sleep 5 – It asks the shell to keep a time difference of 5 seconds before it executed
again. Note the time here is always measured in seconds. Say if you want to
execute the command every 6 minutes, you should use (6*60) 360, in a succession
of sleep.
done – marks the end of a while loop.
& – Put the whole process in a loop to the background.
Similarly, we can execute any script in the same manner. Here is the command to call a
script after a certain interval (say 100 sec) and the name of the script is script_name.sh.
Also worth mentioning is that the script above should be run in the directory where the
script to be called lies, else you need to provide a full path
(/home/$USER/…/script_name.sh).
Note: The above one-liner is not a replacement for Cron, because Cron utility supports a
whole lot of options, as compared, and is very flexible as well as customizable.
However, if we want to run certain test cases or I/O benchmarks, then the above single
command will serve the purpose.
First, create a new systemd timer unit file with a .timer extension as shown.
[Unit]
Description=My Job Timer
[Timer]
OnCalendar=*-*-* 00:00:00
# Replace the OnCalendar value with the desired schedule
[Install]
WantedBy=timers.target
2/4
The OnCalendar field in a systemd timer unit allows you to specify the schedule for your
job using a specific format.
Next, create a corresponding service unit file with a .service extension in the same
directory:
[Unit]
Description=My Job
[Service]
ExecStart=/path/to/your/job.sh
# Replace "/path/to/your/job.sh" with the actual command or script to execute
[Install]
WantedBy=multi-user.target
This will schedule your job to run according to the specified timer.
3/4
your job.
If it’s not installed, you can install it using your package manager.
Create a new configuration file (myjob.sh) for your job in the /etc/anacrontab.d/
directory.
The first field is the time period at which the job should be run @daily, @weekly,
@monthly, or @yearly.
The second field is the time in minutes to delay the job execution after the system
starts.
The third field is the job name, which will be used to create log files.
The fourth field is the command or script to be executed.
Now, Anacron will automatically execute your job according to the specified schedule.
That’s all for now, if you know any such Linux hacks or tricks you may share them with us
via our comment section, and don’t forget to share this article with your friends.
4/4