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

What on earth is the word "2 Linux" in ampter1?

2025-03-31 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Servers >

Share

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

There is often such a need, write a python file, and then make the python into a scheduled task form, according to the requirements of regular crontab execution!

Scheduled tasks often see the following format, for example, the following one is executed every three minutes, and the output information of python is recorded in the log log every time! What is commonly used is

[root@nessus allpython2019] # crontab-root/allpython2019/1-FtpSwDownloadcfg20191101.py 3 * / usr/bin/python / root/allpython2019/1-FtpSwDownloadcfg20191101.py > > / root/allpython2019/run.log 2 > & 1 [root@nessus allpython2019] #

So what on earth is this 2 > & 1?

In fact, many blog posts on the Internet have been explained as follows:

Run.log 2 > & 1 means refer to https://blog.csdn.net/liupeifeng3514/article/details/79711694

Answer: the command command > a 2 > & 1 [command > > a 2 > & 1] can be understood to mean that standard input generated by command is redirected to file a, and standard error is also redirected to file a! (a can be a file run.log or / dev/null empty device file) [if you don't write this, only the standard output may be printed to the log file, but the stderr will not be redirected to the log, and the stderr will be printed to the screen]

Here are my tests:

(1) the test does not add 2 > & 1 stderr is not redirected to log, and stderr is printed on the screen.

(2) Test added 2 > & 1 stderr has also been redirected to log

(3) there is a difference between the two commands command > a 2 > & 1 and command > a 2 > & 1 when testing timing crontab execution, that is, an override! One is to add! I often use add!

Several basic symbols and their meanings: / dev/null for empty device file; 0 for stdin standard input; 1 for stdout standard output; 2 for stderr standard error. For example, I wrote the following test program [here we have two commands, in which the t instruction does not exist, the execution will report an error, and it will be output to stderr. Date can be executed normally, the execution will output the current time, and it will be output to stdout. ] [root@nessus allpython2019] # vim test.shroud raceme bins * 17C has written [root@nessus allpython2019] # chmod + x test.sh [root@nessus allpython2019] #. / test.sh > res1.log./test.sh: line 2: t: command not found #-you can see that not adding 2 > & 1 stderr is not redirected to log Stderr is printed to the screen [root@nessus allpython2019] # [root@nessus allpython2019] # cat res1.log Friday, November 1, 2019, 10:01:47 CST [root@nessus allpython2019] # [root@nessus allpython2019] # [root@nessus allpython2019]. / test.sh > res2.log 2 > & 1 #-you can see that adding 2 > & 1 stderr has also been redirected to log [root@nessus allpython2019] # [root@nessus Allpython2019] # [root@nessus allpython2019] # cat res2.log. / test.sh: line 2: t: no command found: Friday, November 01, 2019, 10:03:08 CST [root@nessus allpython2019] # [root@nessus allpython2019] # [root@nessus allpython2019] #. / test.sh > res2.log 2 > & 1 #-the following shows the difference between > and > It's an overlay! One is to add! [root@nessus allpython2019] # cat res2.log. / test.sh: line 2: t: command not found Friday 01 November 2019 10:03:24 CST [root@nessus allpython2019] # [root@nessus allpython2019] # [root@nessus allpython2019] #. / test.sh > > res2.log 2 > & 1 [root@nessus allpython2019] # cat res2.log. / test.sh: line 2: t: command not found Friday 01 November 2019 10: 03:24 CST./test.sh: line 2: t: no command found Friday 01 November 2019 10:03:40 CST [root@nessus allpython2019] #. / test.sh > > res2.log 2 > & 1 [root@nessus allpython2019] # cat res2.log. / test.sh: line 2: t: no command found Friday 01 November 2019 10:03:24 CST./test.sh: line 2: t: no command found Friday 01 November 1 10:03:40 CST./test.sh: line 2: t: no command found Friday 01 November 2019 10:03:44 CST [root@nessus allpython2019] # [root@nessus allpython2019] # [root@nessus allpython2019] #

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: 264

*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