+4 votes
61 views
How to create Jobs without Cron in Linux

in Linux/Unix by (552k points)
reopened | 61 views

1 Answer

+5 votes
Best answer

1 How to create Jobs without Cron in Linux using Sleep Loop
2 How to create Tasks without Cron in Linux using Systemd
3 How to create Jobs without Cron in Linux using Anacron

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 & 
image

 

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:

 

image

 

Step 4

It is possible to open it to see the content created:

 

image

 


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 
image

 

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 
image

 

 

Step 3

Some of the options that we can use in the OnCalendar section are:

 

  • Annual (Yearly): annual
  • Monthly: monthly
  • Weekly: weekly
  • 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 
image

 

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 
image

 

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 
image

 

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.

 

 

 

 

 

 

image

 


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:
  • -s: Manage job execution
  • -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
  • -h: print the help
  • -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 
image

 

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 
image

 

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:

 

 

image

 

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.

 

image

 

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.


by (3.5m points)
edited

Related questions

+5 votes
1 answer
asked Jun 23, 2019 in Linux / Unix by backtothefuture (552k points) | 180 views
+5 votes
1 answer
+5 votes
1 answer
asked Jun 20, 2023 in Linux/Unix by backtothefuture (552k points) | 58 views
+5 votes
1 answer
asked May 5, 2023 in Linux/Unix by backtothefuture (552k points) | 64 views
+5 votes
1 answer
Sponsored articles cost $40 per post. You can contact us via Feedback

Most popular questions within the last 30 days

10,659 questions
10,791 answers
510 comments
3 users