How To Wiki
(New page: Some programs allow you to close them gracefully, asking you if you want to save your data, or log off before the programs is closed. This can be true of graphically programs as well as [...)
 
(No difference)

Revision as of 13:01, 4 March 2008

Some programs allow you to close them gracefully, asking you if you want to save your data, or log off before the programs is closed. This can be true of graphically programs as well as console programs. The term for closing programs is Linux/Unix is kill

Graphical programs

Using a program called 'wmctrl, you can close programs the way they are ment to be shutdown. wmctrl has many other uses besides this, but for this howto, the close functions is all we care about.

Usage

  • -c <WIN>
    • Close the window <WIN> gracefully.
  • <WIN> is a string, piece of text, in the tile bar. It is NOT case sensitive.
    • Example:
      • For window title: theory of evolution - OpenOffice.org Writer
      • <WIN> could be: openoffice

Example

  • You want to close OpenOffice.org
    • The title of the window is theory of evolution - OpenOffice.org Writer
    • Execute: wmctrl -c openoffice
    • If the file is unsaved, it will pop-up, Do you want to save.
      • Note it will only close one instance of OpenOffice, so if you have two files open, you would need to run it twice.

Console programs

To close console programs you use the kill command. Kill has a bunch of different signals that can be sent to a programs to initial a close.

kill signals

  • Common kill signal
SIGHUP 1 Hangup
SIGINT 2 Interrupt from keyboard
SIGKILL 9 Kill signal
SIGTERM 15 Termination signal
SIGSTOP 17,19,23 Stop the process

It is not always obvious what kill signal will close the program gracefully so it is likly you will have to test out different signals, and figure out which signal closes the programs better. If you are lucky the programs user man will give you a hint.

Example

  • You want to close, rtorrent, allow it to first close all connections
    • The user guide states: SIGINT: Normal shutdown with 5 seconds to send the stopped request to trackers. [1]
    • SIGINT is #2
  • Execute: kill -2 `pidof rtorrent`
  • To make a nice script for it
#!/bin/bash

# rtorrent
if [[ -n `pidof rtorrent` ]];then
	echo "rtorrent is open, time to die (gracefully)...."
	kill -2 `pidof rtorrent`
else
	echo "rtorrent not running..."
fi

See Also

External Links