2

I want to close a program through the command line (say Firefox or Thunderbird). The program is working just fine and in theory I could just go FILE > CLOSE. However, I want to do this through the command line so that is not an option. I could kill the process (e.g. pkill firefox), but from the sound of it, that is quite a brutal way to close a program. In fact, I am used to using this as a last resort, especially when a program hangs. In all honesty, I don't know if this is a proper way of quitting a program. Is it? Or are there better ways of closing a program?

user32421
  • 337
  • 5
  • 10
  • kill processname is the first thing to try. See man kill. Quote: "The default signal for kill is TERM.". Otherwise kill -9 will work. This is pretty standard, probably a FAQ. Oh, and note kill -15 is the same as kill, because SIGTERM == 15. See https://en.wikipedia.org/wiki/Unix_signal – Faheem Mitha Apr 14 '16 at 10:42

3 Answers3

4

kill sends signals to processes, it defaults to sending the TERM signal. The TERM signal can be 'caught' by processes, i.e. they can watch for it, and when it's received they can take action.

In many cases, Linux processes will behave properly when sent the TERM signal - i.e. they will tidy themselves up and then close down cleanly. So kill is a perfectly valid way of shutting many processes down, assuming the developers have properly handled the situation.

Whether it works for any given process depends on the developer.

Only some signals like KILL can not be caught, you send a KILL using kill by running kill -9, which is far more disruptive to the process because they get no chance to clean up.

EightBitTony
  • 21,373
  • 1
    To be explicit about editors - it's unlikely that you get a "unsaved document - save?". And as you say, if the developer is careless, it might cause problems. But well-coded editors will save the draft, and then tell you about it when you re-open the file. – sourcejedi Apr 14 '16 at 11:52
0

Use kill -15 [pid/name] as 'normal' kill that allows the process to perform cleanup

Use kill -9 [pid/name] as 'override' kill that kills the process immediately.

0

It's possible to simulate window close. I'm not sure how to identify windows reliably though. (I.e. kill firefox without killing gedit firefox-bug.txt). As commented, the fundamental tradeoff is whether you want to encourage user interaction (close confirmation dialogue) or not.

https://unix.stackexchange.com/a/247163/29483

sourcejedi
  • 50,249