How to schedule a recurring task on linux?

What command can I use to schedule a recurring task on linux? I would also like to send the return of the command by email automatically. What is the most suitable tool?

Author: bfavaretto, 2014-01-29

4 answers

There is a tool for linux called crontab. She's a native. To edit the file just give the command crontab -e.

Crontab has the following format:

[minutes] [hours] [days of the month] [month] [days of the week] [user] [command]

Each field is filled in as follows:

  • minutes: report numbers from 0 to 59;
  • hours: report numbers from 0 to 23;
  • days of the month: report numbers from 0 to 31;
  • month: report numbers from 1 to 12;
  • days of the Week: report numbers from 0 to 7;
  • user: is the user who will execute the command (it is not necessary to specify it if the user's own file is used);
  • command: the task to be performed.

  • crontab-e: is used to edit the current crontab file and create one, if not exist;
  • crontab-l: shows the current content of crontab;
  • crontab-r: removes the current crontab file.

To send email, just use this command

59 */6 * * * script.sh | mail -s “Subject of Mail” [email protected]

You can read more about at this link. http://en.wikipedia.org/wiki/Cron

 20
Author: Marco Paulo Ollivier, 2014-01-30 14:13:58

Use the Cron for scheduling. It is the default tool.

Redirect the output with a pipe ( / ) to the mail command to send the output by email.

Example of input in /etc/crontab:

00 09-18 * * * /home/exemplo/bin/check-db-status | mail -s "db status" [email protected]

Will run every hour from 9 to 18, sending the output to [email protected].

 7
Author: Vitor Py, 2016-05-11 20:44:58

To schedule a task on Linux, you must use CRON. Cron can be interpreted as a Linux service that is loaded during the system boot process. It is a tool that allows you to program the execution of commands and processes repeatedly or only once.

To perform the tasks, cron uses a kind of Table known as crontab. The crontab file is usually located in the /etc directory, but it can also be in a directory that creates a crontab for each system user (usually in /var/spool/cron/), it all depends on the linux distribution.

Hand in batter

The first step is to open crontab. For this you can use text editors such as vi, emacs or nano (I prefer pico, but check their distribution). You can also type the crontab-e command to edit your user's unique file.

Crontab has the following format:

[minutes] [hours] [days of the month] [month] [days of the week] [user] [command]

Each field is filled in as follows:

  • minutes : report numbers from 0 to 59;

  • hours : report numbers from 0 to 23;

  • days of the month : enter numbers from 0 to 31;

  • month : report numbers from 1 to 12;

  • days of Week : report numbers from 0 to 7;

  • user : it is the user who will execute the command (it is not necessary to specify it if the user's own file is used);

  • command : the task to be performed.

Obs.:

 - Você pode informar * (asterisco) para especificar uma execução constante. Por exemplo, se o campo dias do mês conter *, o comando relacionado será executado todos os dias;
 - Você também pode informar intervalos no preenchimento, separando os números de início e fim através de - (hífen). Por exemplo, se no campo horas for informando 2-5, o comando relacionado será executado às 2, 3, 4 e 5 horas. E se o comando tiver que ser executado às 2 horas, entre 15 e 18 horas e às 22 horas? Basta informar 2,15-18,22. Nestes casos, você separa os parâmetros por vírgula.

For example:

#tarefa infowester 
30 0,21-23 3,14 \* \* echo "Não entre em pânico" > /home/alecrim/infowester.txt

In this example, the phrase "Don't panic" is inserted into the infowester file.txt, within the directory / home / Rosemary/, at 30 minutes at 21, 22, 23 and 0 hours, on days 3 and 14, in all months and on all days of the week. Notice the line "# infowester task". This is a comment. Type # and everything that is typed in the line will not be considered by the cron. It is a useful feature for entering descriptions when you have multiple tasks to perform.

Crontab commands

crontab-e: as already reported, it serves to edit the current crontab file and create one, if not exists;

crontab-l : this command shows the current contents of crontab;

crontab-R : removes the current crontab file.

CRONTAB commands

Source: Infowester ( http://www.infowester.com/linuxcron.php )

ANOTHER SOLUTION

Maybe an easier way you also use the init file.d . This file is found in most massant Linux distributions, in the path /etc/init.d, where all the commands found in this file are executed every time you boot the system. To use this function, simply open this file with a text editor and place the desired commands.

Attention: all commands are executed only at system startup.

 3
Author: Pedro Reis, 2014-01-29 18:46:27

Remembering that when you use crontab -e you do not need to put user in the execution of the activity. With crontab -e it is already Native run by root. Already in /etc/crontab you need to put the user.

 2
Author: Giovani, 2014-06-27 20:48:55