How to Schedule a Linux Job Without Cron

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.

If you read this far, tweet to the author to show them you care. Tweet a thanks
Ravi Saive
I am an experienced GNU/Linux expert and a full-stack software developer with over a decade in the field of Linux and Open Source technologies

Each tutorial at TecMint is created by a team of experienced Linux system administrators so that it meets our high-quality standards.

Join the TecMint Weekly Newsletter (More Than 156,129 Linux Enthusiasts Have Subscribed)
Was this article helpful? Please add a comment or buy me a coffee to show your appreciation.

19 thoughts on “How to Schedule a Linux Job Without Cron”

  1. Hello, don’t forget the “at” command, which is also used to schedule tasks or commands at a specified time.

    Reply
  2. I tried the first one ($ while true; do date >> date.txt ; sleep 5 ; done &) on my box and its creating a huge file every minute, please provide a way to remove this job.

    Reply

Got something to say? Join the discussion.

Thank you for taking the time to share your thoughts with us. We appreciate your decision to leave a comment and value your contribution to the discussion. It's important to note that we moderate all comments in accordance with our comment policy to ensure a respectful and constructive conversation.

Rest assured that your email address will remain private and will not be published or shared with anyone. We prioritize the privacy and security of our users.