How to Schedule a Linux Job Without Cron

Introduction

If you are looking for a way to schedule a Linux job without using the traditional cron utility, there are several options available. In this tutorial, we will discuss how to schedule a Linux job without cron, including using the at command, systemd timers, and the crontab command. We will also discuss the advantages and disadvantages of each approach. By the end of this tutorial, you should have a better understanding of how to schedule a Linux job without cron.

How to Schedule a Linux Job Without Cron

1. Use the at command: The at command is used to schedule a command or script to run at a specified time and date. It is similar to cron, but it is used for one-time tasks rather than recurring tasks.

2. Use the systemd timer: Systemd is a system and service manager for Linux operating systems. It includes a timer unit that can be used to schedule tasks.

3. Use the watch command: The watch command is used to execute a command or script periodically and display the output. It can be used to schedule a task to run at a specific interval.

4. Use the nohup command: The nohup command is used to run a command or script in the background, even after you log out of the system. It can be used to schedule a task to run at a specific time.
[ad_1]

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.

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.

[ad_2]

How to Schedule a Linux Job Without Cron

Cron is a powerful tool for scheduling tasks in Linux, but it isn’t the only option. If you need to schedule a job without using Cron, there are several alternatives available. In this article, we’ll discuss some of the most popular methods for scheduling jobs in Linux without using Cron.

Using at Command

The at command is a simple way to schedule a job to run at a specific time. It allows you to specify a one-time job that will run at a specific time. To use the at command, you need to specify the time and date when the job should run, as well as the command that should be executed. For example, to run a job at 10:00 PM on the 15th of April, you would use the following command:

at 10:00 PM 15 April command

The command can be any valid Linux command, such as a script or a program. The at command is a great way to schedule a job to run once at a specific time.

Using systemd Timers

Systemd timers are a more advanced way to schedule jobs in Linux. They allow you to schedule jobs to run at regular intervals, such as every day or every week. To use systemd timers, you need to create a timer unit file that specifies when the job should run. For example, to run a job every day at 10:00 PM, you would use the following timer unit file:

[Timer]
OnCalendar=*-*-* 10:00:00

[Install]
WantedBy=timers.target

Once the timer unit file is created, you can enable it with the following command:

systemctl enable mytimer.timer

Systemd timers are a great way to schedule jobs to run at regular intervals without using Cron.

Using systemd Services

Systemd services are another way to schedule jobs in Linux. They allow you to run a job at startup or when a specific event occurs. To use systemd services, you need to create a service unit file that specifies when the job should run. For example, to run a job at startup, you would use the following service unit file:

[Unit]
Description=My Service

[Service]
Type=oneshot
ExecStart=/path/to/command

[Install]
WantedBy=multi-user.target

Once the service unit file is created, you can enable it with the following command:

systemctl enable myservice.service

Systemd services are a great way to schedule jobs to run at startup or when a specific event occurs without using Cron.

Conclusion

Cron is a powerful tool for scheduling tasks in Linux, but it isn’t the only option. There are several alternatives available for scheduling jobs in Linux without using Cron. In this article, we discussed some of the most popular methods for scheduling jobs in Linux without using Cron, such as the at command, systemd timers, and systemd services.

Jaspreet Singh Ghuman

Jaspreet Singh Ghuman

Jassweb.com/

Passionate Professional Blogger, Freelancer, WordPress Enthusiast, Digital Marketer, Web Developer, Server Operator, Networking Expert. Empowering online presence with diverse skills.

jassweb logo

Jassweb always keeps its services up-to-date with the latest trends in the market, providing its customers all over the world with high-end and easily extensible internet, intranet, and extranet products.

GSTIN is 03EGRPS4248R1ZD.

Contact
Jassweb, Rai Chak, Punjab, India. 143518
Item added to cart.
0 items - 0.00