6

I use

myscript > my.log 2>&1 &

to run a script and collect it's output - so that when I logout - the script will still be running. It I were to start it with myscript & - it will be terminated right after logout.

But that's a strange effect: all that > my.log 2>&1 & does is redirect stderr to stdout...

Why > my.log 2>&1 & causes the job to sustain log out?

5
  • remove the trailing & to remove the immortality
    – jippie
    May 27, 2012 at 9:17
  • Well - yes - but if I were to do script & it's not immortal, is it?
    – Adobe
    May 27, 2012 at 9:28
  • 1
    it used to be killed when closing the shell, but most recent distributions leave the script running until it exits by itself
    – jippie
    May 27, 2012 at 9:32
  • 2>&1 redirects stderr to stdout, in fact. Then (right to left), > my.log redirects this stdout to a file.
    – choroba
    May 27, 2012 at 11:39
  • @choroba: You right. I must had been far away when I was writing that.
    – Adobe
    May 27, 2012 at 11:59

3 Answers 3

8
+50

Normally if you put something in the background it will continue to run even after its parent shell exits. In fact, I can create a test case like this:

/bin/sh -c 'while true; do echo hi; sleep 5; done' &

and then exit the shell and using ps I can see it's still running, forever, until I kill it.

There is one difference between redirecting the output and not: if you don't redirect the output and you exit your shell, then the stdout/stderr file descriptors are closed and the next time you try to write to them the write operation will fail. If your script is checking for that, or running with the -e (exit on error) option set, then your script will stop. Compare the behavior of the above with this version:

/bin/sh -c 'while true; do echo hi || exit 1; sleep 5; done' &

or

/bin/sh -ec 'while true; do echo hi; sleep 5; done' &

If you leave this running, exit the shell, and use ps you'll see that when it tries to run the echo it will fail, and exit.

If you were to redirect the output to a file then obviously the write will not fail anymore and the script will continue to run.

1

The > my.log 2>&1 has nothing to do with it. The final & instead, has the role to run a process in the background. The line returned after entering the command should show a number which is the process id (pid.) You can use this number afterwards, with the kill command, in order to terminate the process, if needed.

Even though, for your case I would run everything in a screen. You can start a screen using

screen -S <name>

and afterwards execute your command. To detach from a screen (return to your initial terminal), you need to press CTRL and in order press A and D. To reattach a screen you type:

screen -r <name>

Like this (running the task in a screen), it will run even if the user logs out/disconnects and the best part is that you get to see the results with the reattach command.

0

Let's compare an immortal process to a daemon. A daemon does not interract with any user or terminal, and it dies when init dies (unless it is stopped on purpose).

you can "create a daemon" with nohup and by redirecting to /dev/null the std.out & std.err

nohup ping google.com 2> /dev/null > /dev/null - THIS IS IMMORTAL.

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .