How to schedule a recurring task in linux?

18

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

    
asked by anonymous 29.01.2014 / 18:31

4 answers

18

There is a tool for linux called crontab. She is native. For you to edit the file just give the command crontab -e .

The crontab has the following format:

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

The completion of each field is done 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 that will execute the command (it is not necessary to specify it if the user's own file is used);
  • Command: the task that should be performed.
  • crontab -e: is to edit the current crontab file and create one if it does not exist;
  • crontab -l: shows the current contents 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 this link. link

    
29.01.2014 / 18:38
7

Use the cron for the schedule. This is the default tool.

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

Example input to /etc/crontab :

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

It will run from one at a time from 9 to 18, sending the output to [email protected]

    
29.01.2014 / 18:38
3

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 in a repetitive way or only once.

To execute 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 at / var / spool / cron /), everything depends on linux distribution.

Hand in hand

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

The crontab has the following format:

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

The completion of each field is done 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 that will execute the command (it is not necessary to specify it if the user's own file is used);

  • Command : The task that should be performed.

Note:

 - 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 "Do not Panic" is inserted into the infowester.txt file, within the / home / alecrim / directory, at 30 minutes from 9:22 p.m., 2:00 p.m., 0:00 p.m. on days 3 and 14, in every month and every day of the week. Notice the line "#tarefa infowester". This is a comment. Type # and anything typed on the line will not be considered by cron. This is a handy feature for entering descriptions when you have multiple tasks to perform.

crontab commands

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

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

crontab -r : Removes the current crontab file.

Source:Infowester( link )

OTHER SOLUTION

Perhaps an easier way than you too to use the init.d file. This file is found in most Linux distributions in the /etc/init.d path, where all commands found in this file run every time you boot the system. To use this function, simply open this file with a text editor and put the desired commands.

WARNING: All commands run only at system startup.

    
29.01.2014 / 19:46
2

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

    
27.06.2014 / 22:09