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.
Scheduling a job/command in Linux is an acronym to cron. Whenever we need to schedule a job, we call cron command, but do you know we can schedule a job at a later time without corn? You can do it using the following common approaches.
Table of Contents
1. Scheduling Jobs in Linux Using Sleep Loop
Run a command (say date) every 5 sec and write the output to a file (say date.txt). To achieve this scenario, we need to run the below one-liner script directly on the command prompt.
$ while true; do date >> date.txt ; sleep 5 ; done &
Anatomy of the above one-liner script:
- 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.
- 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
).
The syntax for calling script at above described interval is:
$ while true; do /bin/sh script_name.sh ; sleep 100 ; done &
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.
2. Schedule Tasks with Systemd Timers
In most modern Linux distributions, Systemd is the default init system and it comes with a timer functionality that can allow you to schedule your tasks.
First, create a new systemd timer unit file with a .timer
extension as shown.
$ sudo nano /etc/systemd/system/myjob.timer
Add the following content to the myjob.timer
file:
[Unit] Description=My Job Timer [Timer] OnCalendar=*-*-* 00:00:00 # Replace the OnCalendar value with the desired schedule [Install] WantedBy=timers.target
The OnCalendar field in a systemd timer unit allows you to specify the schedule for your job using a specific format.
- Yearly: yearly or annually
- Monthly: monthly
- Weekly: weekly
- Daily: daily or midnight
- Hourly: hourly
- Minutes: You can specify a specific minute using the format
*:MM
(e.g.,*:15
for every 15 minutes) or a specific range using MM-MM (e.g., 10-30 for every minute from 10 to 30).
Here are some examples to illustrate the format:
## Run every day at 3:00 AM ## OnCalendar=*-*-* 03:00:00 ## Run every Monday and Friday at 10:00 AM ## OnCalendar=Mon,Fri *-*-* 10:00:00 ## Run every 30 minutes: ## OnCalendar=*-*-* *:0/30:00
Next, create a corresponding service unit file with a .service
extension in the same directory:
$ sudo nano /etc/systemd/system/myjob.service
Add the following content to the myjob.service
file:
[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
Enable and start the timer:
$ sudo systemctl enable myjob.timer $ sudo systemctl start myjob.timer
This will schedule your job to run according to the specified timer.
3. Scheduling Tasks Using Anacron
Anacron is a time-based job scheduler that allows you to schedule jobs periodically on systems that are not always powered on. It is designed for systems that may not have regular access to cron. If anacron is installed on your system, you can use it to schedule your job.
If it’s not installed, you can install it using your package manager.
$ sudo apt install anacron [On Debian, Ubuntu and Mint] $ sudo yum install 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 install anacron [On OpenSUSE]
Create a new configuration file (myjob.sh) for your job in the /etc/anacrontab.d/ directory.
$ sudo nano /etc/anacrontab.d/myjob.sh
In the configuration file, specify the details of your job.
# Run myjob.sh every day with a delay of 5 minutes 1 5 myjob /path/to/myjob.sh
The fields in the configuration file have the following meanings:
- 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.
Thanks !
And as every advice can be useful, I would like to contribute to your article with another tip I learned from a collegue few month ago :
How to execute a command without leaving it in history file ?
– just put an empty space first ! :-)
hi, one way among others to exit properly a process sent in background is to use the fg command (FrontGround) to get it back (if you are still in the same terminal) and then CTRL+C.
Franck
excellent !!!
thanks a lot !!!
OMG
(cd /home/avi/Downloads/ && ls -l)
Or
ls -l /home/avi/Downloads/
This aint a trick or hack, but rather a waste of time…
How to stop this – while true; do date > /tmp/date.txt ; sleep 5 ; done &
I could not find a way other than killing the session.
kill -s STOP ?
3. Run a command and come back to the current working directory automatically.
also can do like this:
avi@deb:~$ cd /home/avi/Downloads/
avi@deb:~$ls -l
avi@deb:~$cd –
Agree,
But why to type an extra line of command?
All my intention is to do it as quick and effective as possible.
hi
I am also learning scripting, can we restrict a user to use a specific amount of data in a directory. Lets say I need to restrict a user to save or copy 100mb of data only not more than that.
thanks in advance
satish amrutwar
You should see into Quota Management.
Install Quota. apt/ yum/ dnf the required packages.
How to stop this:
while true; do date > /tmp/date.txt ; sleep 5 ; done &
Kill the session.
The last two will definitely help me. :)
Pleased to know @ Tanmaya Meher,
that my writing would be helpful to you.
No.3!!!
What number 3!
I would have appreciated if you had been clear.