简体   繁体   English

从bash启动脚本并指导输出

[英]Launching scripts from bash and directing outputs

I have a question about syntax of bash regarding launching scripts from within bash script. 我有一个关于bash语法的问题,关于从bash脚本中启动脚本。

My questions are: 我的问题是:

  1. I've seen the following syntax: 我见过以下语法:

     #!/bin/bash python do_something.py > /dev/null 2>&1 & 

    Can you please explain what is directed to /dev/null , and what is the meaning of 2>&1 if before already mentioned /dev/null ? 你可以解释一下导向/dev/null ,如果之前已经提到过/dev/null那么2>&1是什么意思?

  2. In addition if I have a line defined like: 另外,如果我有一行定义如下:

     python do_something.py > 2>&1 & 

    how is that different? 那有什么不同?

  3. If I have the same python file in many paths, how can I differentiate between each process after launching ps -ef |grep python . 如果我在许多路径中有相同的python文件,我怎样才能在启动ps -ef |grep python之后区分每个进程。 When I'm doing so, I get a list of processes which are all called do_something.py , it would be nice if I could have the full execution path string of each pid; 当我这样做时,我得到一个名为do_something.py的进程列表,如果我可以拥有每个pid的完整执行路径字符串,那就太好了。 how can I do that? 我怎样才能做到这一点?

NOTE: The python file launched is writing its own log files. 注意:启动的python文件是编写自己的日志文件。

1) stdout (Standard Output) is redirected to /dev/null and stderr (error messages) is redirected to standard output ie console. 1)stdout(标准输出)被重定向到/ dev / null,stderr(错误消息)被重定向到标准输出,即控制台。

  • 1>filename : Redirect stdout to file "filename." 1> filename:将stdout重定向到文件“filename”。
  • 1>>filename: Redirect and append stdout to file "filename." 1 >> filename:重定向并将stdout附加到文件“filename”。
  • 2>filename : Redirect stderr to file "filename." 2> filename:将stderr重定向到文件“filename”。
  • 2>>filename: Redirect and append stderr to file "filename." 2 >> filename:重定向并将stderr附加到文件“filename”。
  • &>filename : Redirect both stdout and stderr to file "filename." &> filename:将stdout和stderr重定向到文件“filename”。

3) Using the ps auxww flags, you will see the full path to output in both your terminal window and from shell scripts. 3)使用ps auxww标志,您将在终端窗口和shell脚本中看到输出的完整路径。 "ps manual": “ps手册”:

-w Wide output. -w宽输出。 Use this option twice for unlimited width. 两次使用此选项可获得无限宽度。

Answers: 回答:

1, 2. > redirects whatever that is printed in stdout as result of executing the command (in your case python do_something.py ) to a file called /dev/null . 1,2。 >将执行命令(在您的情况下为python do_something.py )的结果重定向到stdout打印的任何内容到名为/dev/null的文件。 The /dev/null is kind of a black hole. /dev/null是一种黑洞。 Whatever you write to it disappers. 无论你写什么,它都会消失。

2>&1 redirects the output of stderr (which has fd as 2) to stdout (whose fd is 1). 2>&1stderr (其fd为2)的输出重定向到stdout (其fd为1)。

Refer I/O redirection for more info about redirections. 有关重定向的详细信息,请参阅I / O重定向。

Refer this link for more info about /dev/null 有关/dev/null更多信息,请参阅链接

Ok, diclaimer : I don't have access to a bash right now, so I might be wrong. 好的, 免责声明 :我现在无法访问bash,所以我可能错了。

  1. Let's break your command: python do_something.py > /dev/null 2>&1 & 让我们打破你的命令: python do_something.py > /dev/null 2>&1 &

    python do_something.py will run your command python do_something.py将运行您的命令
    > /dev/null will redirect stdout to /dev/null > /dev/null会将stdout重定向到/ dev / null
    2>&1 will redirect stderr to stdout 2>&1将stderr重定向到stdout
    & will fork your process and run in background &将分叉您的流程并在后台运行

    So your command will ignore stdout/stderr and be run in background which is equivalent to the command python do_something.py >& /dev/null & [1] [2] 所以你的命令将忽略stdout / stderr并在后台运行,这相当于命令python do_something.py >& /dev/null & [1] [2]

  2. python do_something.py > 2>&1 & : python do_something.py > 2>&1 &

    > 2 will redirect stdout to a file named 2 > 2会将stdout重定向到名为2的文件
    >&1 will redirect stdout to stdout (yes stdout to stdout) >&1会将stdout重定向到stdout(是stdout到stdout)
    & will fork your process and run in background &将分叉您的流程并在后台运行

    So this command is almost equivalent to python do_something.py >2 & , it will redirect the output to a file named 2 (eg: echo 'yes' > 2>&1 ) 所以这个命令几乎等同于python do_something.py >2 & ,它会将输出重定向到一个名为2的文件(例如: echo 'yes' > 2>&1

    Note : the behavior of >&1 is probably unspecified. 注意>&1的行为可能未指定。

  3. Since you have run your command using & , your command will be fork and run in background, therefore I'm not aware of any way to do it in that case. 由于您使用&运行命令,因此您的命令将在后台运行并运行,因此在这种情况下我不知道有任何方法可以执行此操作。 You can still lookup the /proc directory [3] to see from which directory your command have been run thought. 您仍然可以查找/proc目录[3]以查看您的命令从哪个目录运行。

[1] : What is the difference between &> and >& in bash? [1]bash中&>和>&之间有什么区别?
[2] : In the shell, what does “ 2>&1 ” mean? [2]在shell中,“2>&1”是什么意思?
[3] : ls -l /proc/$PROCCESSID/cwd [3]ls -l /proc/$PROCCESSID/cwd

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM