How to schedule a recurring task on linux?

Asked

Viewed 8,186 times

20

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?

  • For this you use a Linux feature called Cron. You can learn more about cron at this link: http://www.infowester.com/linuxcron.php. And to send the email I never had to, so I don’t have much experience, but maybe this link can help you: http://www.nixtutor.com/linux/sending-email-Alerts-through-cron/ I hope I’ve helped!

4 answers

20


There is a tool for linux called crontab. It is 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]

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;
  • Weekdays: report numbers from 0 to 7;
  • User: is the user who will execute the command (no need to specify it if the user’s own file is used);
  • Commando: the task to be performed.

  • crontab -and: serves to edit the current crontab file and create one if there is no;
  • 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 this link. http://en.wikipedia.org/wiki/Cron

7

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

Redirect output with a pipe (|) to the command mail 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]

It will run from one in an hour from 9 to 18, sending the output to [email protected].

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 programming the execution of commands and processes in a repetitive way 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 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 the cookie jar

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 your 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]

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;

  • Weekdays: report numbers from 0 to 7;

  • User: is the user who will execute the command (no need to specify it if the user’s own file is used);

  • Commando: 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 "Do not panic" is inserted in the infowester.txt file, inside the directory /home/rosemary/, at 30 minutes of 21, 22, 23 and 0 hours, on days 3 and 14, in all months and on all days of the week. Note the line "#task infowester". This is a comment. Type # and anything typed on the line will not be considered by cron. Useful feature to insert descriptions when you have several tasks to perform.

Crontab commands

crontab -and: as already informed, serves to edit the current crontab file and create one, if there is no;

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

crontab -r: removes the current crontab file.

Comandos CRONTAB

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

ANOTHER SOLUTION

Maybe an easier way that you also use the file init. d. This file is found in most Linux distributions on the way /etc/init. d, where all the commands found in that file run every time you boot the system. To use this function, just open this file with a text editor and put the commands you want.

WARNING: All commands are only executed at system startup.

2

Remembering that when you use the crontab -e no need to put user in the execution of the activity. With the crontab -e it is already native to run root. Already in /etc/crontab it is necessary to put the user.

Browser other questions tagged

You are not signed in. Login or sign up in order to post.