1

I have a question about syntax of bash regarding launching scripts from within bash script.

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?

  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. 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; how can I do that?

NOTE: The python file launched is writing its own log files.

Toby Speight
  • 27,591
  • 48
  • 66
  • 103
JavaSa
  • 5,813
  • 16
  • 71
  • 121
  • have a look at http://wiki.bash-hackers.org/howto/redirection_tutorial .. – Sundeep Aug 30 '16 at 13:32
  • For (2), running `: > 2>&1 &` generates ``-bash: syntax error near unexpected token `2'`` — it is deemed invalid because there is no filename for the `>` redirection. That wasn't what I expected; I thought the file name would be the `2>&1` hieroglyph. (The `:` is a standard shell (built-in) command that does nothing successfully, assuming its arguments and I/O redirections were successful.) – Jonathan Leffler Aug 30 '16 at 14:05

3 Answers3

1

1) stdout (Standard Output) is redirected to /dev/null and stderr (error messages) is redirected to standard output i.e console.

  • 1>filename : Redirect stdout to file "filename."
  • 1>>filename: Redirect and append stdout to file "filename."
  • 2>filename : Redirect stderr to file "filename."
  • 2>>filename: Redirect and append stderr to file "filename."
  • &>filename : Redirect both stdout and stderr to file "filename."

3) Using the ps auxww flags, you will see the full path to output in both your terminal window and from shell scripts. "ps manual":

-w Wide output. Use this option twice for unlimited width.

Vikas Jindal
  • 249
  • 3
  • 12
  • You've not yet explained the `2>&1` notation. You might include a URL reference such as `[I/O Redirection](https://www.gnu.org/software/bash/manual/bash.html#Redirections)` which displays as [I/O Redirection](https://www.gnu.org/software/bash/manual/bash.html#Redirections). – Jonathan Leffler Aug 30 '16 at 14:11
1

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. The /dev/null is kind of a black hole. Whatever you write to it disappers.

2>&1 redirects the output of stderr (which has fd as 2) to stdout (whose fd is 1).

Refer I/O redirection for more info about redirections.

Refer this link for more info about /dev/null

Fazlin
  • 2,285
  • 17
  • 29
1

Ok, diclaimer: I don't have access to a bash right now, so I might be wrong.

  1. Let's break your command: python do_something.py > /dev/null 2>&1 &

    python do_something.py will run your command
    > /dev/null will redirect stdout to /dev/null
    2>&1 will redirect stderr to 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]

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

    > 2 will redirect stdout to a file named 2
    >&1 will redirect stdout to stdout (yes stdout to 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)

    Note: the behavior of >&1 is probably unspecified.

  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.

[1]: What is the difference between &> and >& in bash?
[2]: In the shell, what does “ 2>&1 ” mean?
[3]: ls -l /proc/$PROCCESSID/cwd

Community
  • 1
  • 1
  • @user6775110: can you explain role of each & in the expression: `python do_something.py >& /dev/null &` – JavaSa Aug 30 '16 at 20:17