I am using `&`: why isn't the process running in the background?

bernie2436

I know that I can append & to a command to run the process in the background.

I'm SSH'ing into an Ubuntu 12.04 box and running a python program with $python program.py & -- but when I go to close the terminal window I get a message saying that closing the terminal will kill the running process.

Why is this? I am using the ampersand to run the process in the background. How can I get it to run regardless of if I am SSH'ed in?

phemmer

When you close a terminal window, the terminal emulator sends a SIGHUP to the process it is running, your shell. Your shell then forwards that SIGHUP to everything it's running. On your local system, this is the ssh. The ssh then forwards the SIGHUP to what it's running, the remote shell. So your remote shell then sends a SIGHUP to all its processes, your backgrounded program.

There are 2 ways around this.

  1. Disassociate the backgrounded program from your shell.
    1. Use the disown command after backgrounding your process. This will make the shell forget about it.
    2. Prefix your command with nohup (nohup $python program.py &). This accomplishes the same thing, but by using an intermediate process. Basically it ignores the SIGHUP signal, and then forks & executes your program which inherits the setting, and then exits. Because it forked, the program being launched is not a child of the shell, and the shell doesn't know about it. And unless it installs a signal handler for SIGHUP, it keeps the ignore action anyway.
  2. Use logout instead of closing the terminal window. When you use logout, this isn't a SIGHUP, and so the shell won't send a SIGHUP to any of its children.

Additionally you must make sure that your program doesn't write to the terminal through STDOUT or STDERR, as both of those will no longer exist once the terminal exits. If you don't redirect them to something like /dev/null, the program will still run, but if it tries to write to them, it'll get a SIGPIPE, and the default action of SIGPIPE is to kill the process).

Collected from the Internet

Please contact [email protected] to delete if infringement.

edited at
0

Comments

0 comments
Login to comment

Related

From Dev

Why the process is not running on background?

From Dev

Running command in background isn't working as expected

From Dev

Why am I not getting a password prompt when running a command as sudo, even though my user's group isn't even in the sudoers file?

From Dev

Hiding the process window, why isn't it working?

From Dev

Python: why isn't my if statement running

From Dev

Why this code isn't running successfully?

From Dev

Why isn't this crontab job running

From Dev

Why isn't the threadpooltest running correctly?

From Dev

I am trying to indent data when displaying using \t in swift but it isn't working

From Dev

Why isn't my code printing anything while I am attempting to reverse the words in a string

From Dev

I am new to Regular Expressions . Why isn't this match working in Perl?

From Dev

Why isn't the file lock released on close() if I launch a process while the file is locked?

From Dev

Am I using as.numeric incorrectly? (Still says the argument isn't numeric)

From Dev

why isn't the tkinter root background set to a BMP image when using Label and .place()?

From Dev

why isn't the tkinter root background set to a BMP image when using Label and .place()?

From Dev

Promise isn't running within process.exit

From Dev

Promise isn't running within process.exit

From Dev

I'm using foundation, and I don't know why my header isn't centered correctly

From Dev

Why am i running outta disk space?

From Dev

How do I check if a running process is a background process?

From Dev

How do I check if a running process is a background process?

From Dev

I am using DOS and I am trying to make the background white

From Dev

Why isn't this working If I add spaces to it?

From Java

Why isn't CFStream using TLS 1.3?

From Dev

Why isn't Postgres using the index?

From Dev

Why isn't Postgres using the index?

From Dev

Why isn't my background image showing up?

From Dev

Background image isn't showing up. Why?

From Dev

Why isn't my background for the button changing color?

Related Related

  1. 1

    Why the process is not running on background?

  2. 2

    Running command in background isn't working as expected

  3. 3

    Why am I not getting a password prompt when running a command as sudo, even though my user's group isn't even in the sudoers file?

  4. 4

    Hiding the process window, why isn't it working?

  5. 5

    Python: why isn't my if statement running

  6. 6

    Why this code isn't running successfully?

  7. 7

    Why isn't this crontab job running

  8. 8

    Why isn't the threadpooltest running correctly?

  9. 9

    I am trying to indent data when displaying using \t in swift but it isn't working

  10. 10

    Why isn't my code printing anything while I am attempting to reverse the words in a string

  11. 11

    I am new to Regular Expressions . Why isn't this match working in Perl?

  12. 12

    Why isn't the file lock released on close() if I launch a process while the file is locked?

  13. 13

    Am I using as.numeric incorrectly? (Still says the argument isn't numeric)

  14. 14

    why isn't the tkinter root background set to a BMP image when using Label and .place()?

  15. 15

    why isn't the tkinter root background set to a BMP image when using Label and .place()?

  16. 16

    Promise isn't running within process.exit

  17. 17

    Promise isn't running within process.exit

  18. 18

    I'm using foundation, and I don't know why my header isn't centered correctly

  19. 19

    Why am i running outta disk space?

  20. 20

    How do I check if a running process is a background process?

  21. 21

    How do I check if a running process is a background process?

  22. 22

    I am using DOS and I am trying to make the background white

  23. 23

    Why isn't this working If I add spaces to it?

  24. 24

    Why isn't CFStream using TLS 1.3?

  25. 25

    Why isn't Postgres using the index?

  26. 26

    Why isn't Postgres using the index?

  27. 27

    Why isn't my background image showing up?

  28. 28

    Background image isn't showing up. Why?

  29. 29

    Why isn't my background for the button changing color?

HotTag

Archive