To stay up to date, remember to subscribe to our YouTube channel!
SUBSCRIBE ON YOUTUBE
Creating automatic tasks in Linux may seem like an arduous task but what many users do not know is that within the distribution itself there is a method for this type of segment and it is cron, cron uses a daemon called crond which is in charge of reading the various configuration files and there is a cron file for each user in the /etc/cron.d/ directory and the /etc/crontab file is system wide, this allows each user to manage their scheduled jobs and the file cron setup
TechnoWikis will explain some different methods to the native use of cron to perform scheduled tasks and we will see it in detail in this tutorial that will show you different ways to Create Tasks Without Cron in Linux..
1 How to create Jobs without Cron in Linux using Sleep Loop
Our first method is sleep loop which, as its name indicates, is a small script or command that will make our distribution be in suspend or hibernation mode for a specific time and then activate it again, it can be used for tasks such as energy saving, administrative tests or carrying out some conditions as will be our example.
Step 1
To use this command we are going to open the terminal and then execute a command similar to this as appropriate:
while true; do date >> name.txt ; sleep # ; done &
Step 2
In this command we have indicated the necessary parameters for the execution of the task, these are:
- while true: this value will cause the script to be executed as long as the condition is true, it performs the function of a loop which allows the command to be executed over and over again in a repetitive manner
- do: will execute the command or commands that are available before the do statement
- date >> name.txt: this is the output for the date command which in turn is written to the file
- name.txt, the signs >> allow the file not to be overwritten with each execution of the script
- sleep # - Tells the shell to hold that # second time range before running again
- done: is responsible for marking the end of a while loop
- & : will make the whole process a loop in the background
Step 3
Now we open the path where the file was created and there we will see it:
Step 4
It is possible to open it to see the content created:
2 How to create Tasks without Cron in Linux using Systemd
Systemd is the default Linux startup system and as such it integrates a timer function with which we can create our tasks in a simple and safe way.
Step 1
Let's open the terminal and create a timer unit (.timer) with the desired name using the following syntax:
sudo nano /etc/systemd/system/name.timer
Step 2
Press Enter, enter the password and there we register the following information:
[Unit]Name Job Timer [Timer] OnCalendar=*-*-* 00:00:00 # Replace with the desired values to schedule [Install] WantedBy=timers.target
Step 3
Some of the options that we can use in the OnCalendar section are:
- Daily (Daily): daily or midnight
- Per hour (Hourly): per hour
- Minutes: it is possible to indicate a specific minute with the format *:MM ( *:15 for every 15 minutes) or a specific range with the syntax MM-MM (20-30 for every minute from 20 to 30)
Step 4
We save the changes using the following key combination:
Ctrl + O
We exit the editor using:
Ctrl + X
step 5
After this we execute the following command to create the service using the desired editor:
sudo nano /etc/systemd/system/name.service
step 6
When creating the file we enter the following content:
[Unit] Description=Name [Service] ExecStart=/path/file.sh # Replace the path with the .sh command or script [Install] WantedBy=multi-user.target
step 7
We save the changes using the following key combination:
Ctrl + O
We exit the editor using:
Ctrl + X
step 8
Once you have done this we are going to enable the service with the command:
sudo systemctl enable name.timer
step 9
Now you are going to start the service using the command:
sudo systemctl start name.timer
step 10
From this moment on, based on the structure created, the task will be executed.
3 How to create Jobs without Cron in Linux using Anacron
Anacron is a utility to execute commands periodically using a frequency in days, it can be used on computers that do not work 24 hours a day and allows you to control routine jobs with daily, weekly and monthly frequencies.
Anacron reads the list of jobs hosted in the /etc/anacrontab configuration file and there each job entry indicates the period in days, the delay in minutes, a unique job identifier and a shell command, so Anacron will check if that job has already been executed in the last n days, (n is the period specified for that task), if not, the job shell command will be executed..
Options
Your usage options are:
- -f: Force execution of jobs even before their time
- -n: run jobs without delay, requires use with -s
- -d: Do not run the process in the background
- -q: clear stderr messages
- -u : update timestamps without running anything extra
- -t: makes use of anacrontab
- -V: Print version information
- -T; does anacrontab tests
- -S: select a different spool directory
Step 1
To start we open the terminal and we are going to install Anacron with the following command:
sudo apt install anacron
Step 1
In this case we use Ubuntu, for other distros we can execute:
sudo yum install anacron (RHEL/CentOS/Fedora and Rocky/AlmaLinux) sudo pacman -S anacron ( Arch Linux) sudo zypper install anacron (OpenSUSE)
Step 2
Now we are going to access the Anacron edition file with the following command:
sudo nano /etc/anacrontab
Note
it is possible to create our own file if necessary.
Step 3
We will see the following: There are the predefined tasks of Anacron, in the final part we enter our task:
Step 4
There we can enter numeric values or preceded by @ if we want (@daily, @weekly, @monthly, or @yearly).
We apply the changes and the task scheduled with Anacron will be ready.
Each of these options is an alternative to the well-known Linux cron and each one allows our tasks to be scheduled in an integral way.