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 -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/

This script will run only once at 7 pm.

at now + 30 minutes -f ./Downloads/

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

You can use the command atq to see what is scheduled:

In case you want to remove one scheduled task the command for it would be atrm followed by the number of the task that in the example above is 5:

If you perform the atq again there is no more scheduled task:

One Reply to “Running Commands Periodically or Scheduled in Linux”

Comments are closed.