You have one command or one program that you want it to be executed automatically in one specific moment, like tomorrow at 4 am or in 1 hour from now or every day at 11 pm.

Each one has one specific usage, for backups you would probably schedule to do it automatically at one specific time that does not affect your work. In this case, you have to use cron. It is one native functionality of Linux kernel that reads the crontab every minute and check if any of the rules matches to the present time.

To edit the crontab you can type:

crontab -e

If it is the first time you edit this file it will ask you the preferred editor you want yo use. If you are not familiarized with vi ou vim use nano that is more intuitive.

Inside this file, called table, or cron table, you have to use the syntax to inform the system when your command needs to be executed.

Let’s assume the command you want to run every one minute is pinging another server on the internet to see if the connection is working:

* * * * * * ping test.com -c 1

It will run the command every minute because the cron will check the parameters and every time it will match the current time.

Let’s assume the command you want to run is to shut down the machine every day at 11:00 pm:

* 0 23 * * * sudo shutdown now

As you can see you have to use 24h syntax, not AM or PM.

This will only be executed when the clock turns to 23:00. If you turn on your computer at 23:01 it will only execute the command on the next day.

Let’s assume the command you want to run is to update the database in the locate command. So you can locate files easily with the database no older than one-day:

* * 22 * * * sudo updatedb

In this command, there is one logical error. The command will be performed every day but not only once at 10 pm, but every minute from 22:00 to 22:59. This is probably what anyone wants and will just make one unnecessary workload on the computer for this our.

To solve this issue set precisely when you want to run it, for example, 30 minutes before the computer shutdown:

* 30 22 * * * sudo updatedb

There are situations when you want to perform one specific command/program only once at one specific time and not periodically. For this, you may use the at command.

Let’s assume you are in your work and you want to download one big file but you do not want to slow down the internet for all the other workers. One simple solution is to schedule the download for one specific time after the company hours:

at 19:00 -f ./Downloads/script.sh

This script will run only once at 7 pm.

at now + 30 minutes -f ./Downloads/script.sh

In this other example, the script will run only once in 30 minutes from now.


RELATED TOOLS

Cronitor – Crontab.Guru [Link]. The best two-way translator for corn syntax.

AnaCron is a command used to execute commands periodically with a frequency-specified period [Link]. It checks for the last time a periodical command was executed is greater than the defined, not based on a specific time.

sudo apt install anacron -y
cat /etc/anacrontab

Use Cronic to wrap the command in corn or anacron. It will only send email notifications when the wrapped command exits with a non-zero status [Link].

sudo apt install cronic -y

The at command is a scheduler for single-time execution “at” an exact time.

sudo apt install at -y
sudo systemctl status atd

Example:

at 10:45
at> echo `date` > /tmp/output.txt

Use Ctrl+D to exit.

You can use the command atq to see the queue of commands and atrm to remove any scheduled job.