Just a guess (this is bash
specific): the documentation for the huponexit
shell option says
If set, Bash will send SIGHUP to all jobs when an interactive login shell exits
On my system, it does not seem to be set by default. You can check with
shopt -p huponexit
If the output includes -u
, it is unset.
In general this should be fine to do it this way.
When you click the "X" to close the terminal window, that is sending a "signal" from your desktop (GNOME, KDE, etc.) to the terminal application, telling it to shut itself down. Since you're running MATLAB in this shell it's considered a child process to the terminal application.
So part of the responsibilities of being a parent process, is that you in turn send this same close "signal" to your children.
Now if you understand conceptually what I just explained then let's substitute in a bit more of the real terminology.
signals
First with the "signal", there are actually a whole family of different signals that you can send to Unix processes. To keep it simple there are 4 that you'll often see, SIGHUP
, SIGTERM
, SIGINT
, and SIGKILL
.
SIGHUP
The SIGHUP signal is sent to a process when its controlling terminal
is closed. It was originally designed to notify the process of a
serial line drop. In modern systems, this signal usually means that
controlling pseudo or virtual terminal has been closed.
SIGTERM
The SIGTERM signal is a generic signal used to cause program
termination. Unlike SIGKILL, this signal can be blocked, handled, and
ignored. It is the normal way to politely ask a program to terminate.
SIGINT
The SIGINT (“program interrupt”) signal is sent when the user types
the INTR character (normally C-c).
SIGKILL
The SIGKILL signal is used to cause immediate program termination. It
cannot be handled or ignored, and is therefore always fatal. It is
also not possible to block this signal.
NOTE: SIGINT
is what gets sent when you use Ctrl+C to "break" a program from the command line while it's in the middle of running.
which one is getting used?
Most likely the SIGTERM
is being called by your windowing environment and being passed down to your terminal. Your terminal is then most likely then sending SIGHUP
down to MATLAB. This signal gives all the processes the opportunity to do any local clean-up (closing files, ending processes, etc.) themselves.
kill command
You can send signals yourself using the poorly named command, kill
. So to send the SIGTERM
signal to your terminal or the SIGHUP to MATLAB, you could determine their PID using
ps` and then run this command to send them the signal:
$ kill -SIGTERM <PID>
or this:
$ kill -SIGHUP <PID>
You can get a complete list of the signals using this command:
$ kill -l
1) SIGHUP 2) SIGINT 3) SIGQUIT 4) SIGILL 5) SIGTRAP
6) SIGABRT 7) SIGBUS 8) SIGFPE 9) SIGKILL 10) SIGUSR1
11) SIGSEGV 12) SIGUSR2 13) SIGPIPE 14) SIGALRM 15) SIGTERM
...
...
Notice that the signals have numbers? You'll often times see them used like that instead of by their names:
$ kill -15 <PID>
Or the infamous -9
, which can kill pretty much any process.
Best Answer
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.
disown
command after backgrounding your process. This will make the shell forget about it.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.logout
(or Ctrl+d) instead of closing the terminal window. When you uselogout
, 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).