18

Using Ubuntu 12.04 LTS, my question is that if I started an application in a terminal window, then is there anything bad about just closing the terminal window without exiting the application properly first. For example, I use MATLAB. I open up a terminal and type

matlab -nodisplay -nodesktop -nosplash

and then run a bunch of scripts. Then I can either

exit

to end MATLAB and then close the terminal window or just close the terminal window. What really is the difference between these two methods? Does the second method somehow "harm" anything? Is the first method preferred? Why?

22

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 usingps` 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.

  • I always thought those numbers were negative, but they are actually command-line arguments. I learnt something new :D – Thomas Jul 14 '13 at 2:22
  • 1
    HUP - HangUP - is the signal that is sent if you "loose the line" for a terminal, e.g. you were connected to a Unix-server by modem. It's probably the most "gentle" signal that terminate a process. Most daemons will not exit but re-read their config-files on HUP. The command nohup allows you to let a background-process continue after the shell closes. Commands like screen survive the hangup and insulates the commands in it from HUP. – Baard Kopperud Jul 14 '13 at 19:46
  • @BaardKopperud - thanks, I intentionally left HUP out, didn't want to confuse the discussion. – slm Jul 15 '13 at 1:30
  • @BaardKopperud - I think what you're pointing out is this: "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" I admit I lied a bit in my description but was trying to keep it simple. – slm Jul 15 '13 at 1:59
  • @BaardKopperud - I felt guilty so I've modified my answer to reflect more accurately what's going on. Thanks! – slm Jul 15 '13 at 2:03
2

It is most likely ok but you will lose data depending on what applications you have open. For instance, if you have a file editor open, you may lose some of your changes if you don't save and exit properly. If you're worried, just logout/exit the terminal properly. If you don't like typing exit or logout just try Ctrl-D.

  • Why would that be better? The application is going to get SIGHUP instead of SIGTERM, but it's still getting killed by a signal, so I wouldn't exactly call it "save and exit properly" – Michael Mrozek Jul 14 '13 at 9:15
  • As far as Ctrl-D, that really only applies at the command prompt. – Mark Jul 14 '13 at 12:24

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.