0

for example I have a python script:

#!/usr/local/bin/python3.7
import time

n=0
while True:
    print(n)
    n = n + 1
    time.sleep(1)

If I execute the $test.py in shell like so, I can hit Ctrl+C to cancel the script at any time. but if I execute the script like this: $test.py 2>err&, the only way I can stop the script is using kill command from another terminal.

I can't find any reference to 2>err&, what does it really mean?

user97662
  • 942
  • 1
  • 10
  • 29
  • 1
    It is not the redirection, it is the `&` that executes the command in the background. It just happens to be written here without a space after the redirection of stderr. i.e., if you run `test.py &` it will also run in the background. The `2>` redirects file descriptor number 2, which is stderr, to the destination named afterwards, here `err`. – Arkku Aug 24 '21 at 20:54
  • 1
    It would be better written as `2>err &` -- less confusion that way. – Charles Duffy Aug 24 '21 at 21:13

1 Answers1

2

2>err& is not one thing.

2>err means to redirect standard error to the file named err. You can do this for foreground and background commands, and it has nothing to do with whether you can kill the process with Ctl-C.

& at the end of a command means to run it in the background. Keyboard interrupts are only sent to the foreground process group, so commands in the background are not killed with Ctl-C (this assumes job control is in effect, which it almost always is for interactive shells). You can use the fg command to switch a background process to foreground.

Barmar
  • 741,623
  • 53
  • 500
  • 612