SSH - Scheduled tasks

Procédure

SSH - Scheduled tasks

Much system administration can be automated via Perl scripts, or shell scripts run at regular intervals. For example you could have a script to check that your disk is not full that runs once an hour to let you know if there are any problems. The most common mechanism for scheduling commands on Linux systems is via the cron package.

In Debian, the cron package is part of the base system and is running by default.

Cron is supplied with Debian for two purposes:

  • To run system tasks on a daily/weekly/monthly basis.

  • To allow users to configure their own scheduled tasks.


The Cron package automatically creates the following directories:

/etc/cron.d /etc/cron.daily /etc/cron.hourly /etc/cron.monthly /etc/cron.weekly


Apart from the first, which is special, these directories allow you to schedule tasks for the whole system in a simple way. Any script placed inside them will run as often as its name suggests.

For example, if you place a script inside /etc/cron.daily it will run once a day, every day.

These directories are useful in some cases, but if you need to adjust the execution times further, this can be done by editing the /etc/crontab file, which we'll explain next:

The normal way that people use cron is via the crontab command. This allows you to view or edit your crontab file, which is a per-user file containing entries describing the commands to run and the time to run them.

To show your file you run the following command:

crontab -l


The root user can view the crontab file of any user by adding -u username, for example :

crontab -u username -l


The format of these files is fairly simple to understand. Each line is a collection of six fields separated by spaces.

The fields are :

  1. The number of minutes after the hour (0 to 59)

  2. The hour in military time (24 hours) format (0 to 23)

  3. The day of the month (1 to 31).

  4. Month (1 to 12).

  5. Day of the week (0 or 7).

  6. The command to execute.


They would look like this:

* * * * * Command to be executed - - - - - | | | | | | | +-----> Day of the week (0-7) | | +-----------> The month (1 - 12) | +-----------------> The day of the month (1 - 31) | +-----------------------> The hour (0 - 23) +-----------------------------> Minutes (0 - 59)

(Each of the first five fields contains only numbers, but they can be left as * to indicate that any value is acceptable).


Now that we've seen the structure we should try running a couple of examples:

Edit your crontab file:

crontab -e


This will run your default editor on your crontab file (creating it if necessary). When you save the file and exit your editor, your crontab will be installed in the system unless it has errors.

The default editor is nano

Enter the following text:

0 * * * * /bin/ls > /var/log/ls.log


When you have saved the file and exited your editor you will see a message like :

crontab: installing new crontab


You can check that the file contains what you expect with:

crontab -l


Here we've told Cron to run the /bin/ls command and redirect the output of the command to the /var/log/ls.log file every time the minute equals 0, i.e. it will run the command on the hour, every hour.

Other examples:

Run the command "night" at ten minutes after midnight, every day

10 0 * * * /bin/night


Run the "Monday" command every Monday at 2am

0 2 * * 1 /usr/local/bin/lundi


If you want to run something very regularly, you can use a different syntax: instead of just using numbers, you can use ranges or sets.

A series of numbers indicates that each point in this range will be available, so if you use the following line, you run a command at 1 o'clock, 2 o'clock, 3 o'clock and 4 o'clock:

* 1-4 * * * /bin/command


A set is similar, consisting of a set of numbers separated by commas, each item in the list will be matched. The previous example looks like this using sets:

* 1,2,3,4 * * * /bin/command

Rate this article :

5/5 | 2 opinion

This article was useful to you ?

Article utileYes

Article non utileNo

Vous souhaitez nous laisser un commentaire concernant cet article ?

Si cela concerne une erreur dans la documentation ou un manque d'informations, n'hésitez pas à nous en faire part depuis le formulaire.

Pour toute question non liée à cette documentation ou problème technique sur l'un de vos services, contactez le support commercial ou le support technique

MerciMerci ! N'hésitez pas à poser des questions sur nos documentations si vous souhaitez plus d'informations et nous aider à les améliorer.


Vous avez noté 0 étoile(s)

Similar articles

2mn reading

How do I configure the firewall on a dedicated VPS server?

1mn reading

How can I connect as root on a dedicated VPS server with Putty?

0mn reading

Linux / Debian SSH commands

0mn reading

SSH - Apt-get command: Installing and uninstalling packages


Ask the LWS team and its community a question