Network Security Internet Technology Development Database Servers Mobile Phone Android Software Apple Software Computer Software News IT Information

In addition to Weibo, there is also WeChat

Please pay attention

WeChat public account

Shulou

Detailed explanation and summary of Linux scheduled task Crontab command

2025-01-16 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Servers >

Share

Shulou(Shulou.com)06/02 Report--

The crontab command is an instruction used by Unix and Linux to set up periodic execution. It is a very common technology on the Internet. Many tasks are set to be executed in the crontab loop.

If you do not use crontab, then the task is a resident program, which requires a relatively high requirement for your program. One requires that your program is 24X7-free, and the other is that your scheduler is more reliable. In practice, 90% of the programs do not need to spend so much time and energy to solve the above two problems. You just need to write your own business logic and schedule it through the industrial-grade program Crontab. There should be no doubt about the reliability and robustness of Crontab.

Easy getting started with crontab Command

Suppose I want to set up a task and do a data synchronization every minute, and the path of this synchronization script is / home/blue/do/rsyncfile.sh, then I can configure it like this, use the blue user, and enter it on the terminal.

Crontab-e # will now enter the editing screen of vi to allow you to edit work! Notice that each piece of work is a line. # hours, day, month and week | & 1 &

In this example, 2 > & 1 means that all standard output and error output will be redirected to a file called out.file.

Meaning of Crontab Command 2 > & 1

Let's first look at an example:

0 2 * / u01/test.sh > / dev/null 2 > & 1 &

This means to execute this command in the background and redirect error output 2 to standard output 1, and then put all standard output 1 in the / dev/null file, that is, empty.

There are several numbers here:

0 indicates keyboard input

1 represents standard output

2 indicates error output

We can also write:

0 2 * / u01/test.sh 1 > / u01/out.file & 0 2 * / u01/test.sh 2 > / u01/out.file & 02 * / u01/test.sh 2 > / u01/out.file 2 > & 1 &

Redirect the output of the tesh.sh command to out.file, that is, the output is not printed to the screen, but to an out.file file.

2 > & 1 redirects the error output to standard output. Then redirect the standard input to the file out.file.

& 1 represents file description 1 and standard output. If the missing & becomes the number 1, it means redirecting to file 1.

&: execution in the background

Test:

Ls 2 > 1: no error without 2 files will be reported, but an empty file 1 will be output

Ls xxx 2 > 1: no xxx file error output to 1

Ls xxx 2 > & 1: the file 1 will not be generated, but the error went to standard output

Ls xxx > out.txt 2 > & 1 = = ls xxx 1 > out.txt 2 > & 1: because the redirect symbol > defaults to 1, this sentence sends both error output and standard output to the out.txt file.

The reason why the Crontab command 2 > & 1 is written later

Format: command > file 2 > & 1 = = command 1 > file 2 > & 1

First, command > file redirects the standard output to the file. 2 > & 1 is the standard error that copies the standard output, which is also redirected to the file. The end result is that both the standard output and the error are redirected to the file.

If you change it to: command 2 > & 1 > file

2 > & 1 standard error copies the behavior of standard output, but at this time the standard output is still on the terminal. The output is redirected to file after file, but the standard error remains on the terminal.

Lessons learned:

You plan to execute the Python script on the server at 23:00 every night to back up the MySql database, with the following command:

* 23 * python/ var/www/html/crontab_python/back_db.py > / dev/null 2 > & 1

As a result, 60 backup files are produced for each backup. Looking carefully at the scheduled task command, it is found that a "0" has been added to the "score" location, because "*" represents any value in that location, which is modified as follows:

0 23 * python/ var/www/html/crontab_python/back_db.py > / dev/null 2 > & 1

Then there is the scheduled execution of PHP scripts.

0 4 * / usr/local/php/bin/php / usr/local/nginx/www/backup-db/backup_db.php 172.16.8.26 > / dev/null 2 > & 10 4 * / usr/local/php/bin/php / usr/local/nginx/www/backup-db/backup_db.php 172.16.10.151 > / dev/null 2 > & 1

This article gives you a detailed introduction of the Linux scheduled task Crontab command and summarizes some skills of using the Crontab command and some solutions to the problems encountered in the work. I hope it will be helpful to you.

Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.

Views: 0

*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.

Share To

Servers

Wechat

© 2024 shulou.com SLNews company. All rights reserved.

12
Report