How To Wiki
(punctuation)
Tag: Visual edit
 
(19 intermediate revisions by 7 users not shown)
Line 1: Line 1:
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 also disable crash recovery dialogs, like in [[Firefox]]. This can be true of graphically programs as well as [[console]] programs. The term for closing programs is [[Linux]]/[[Unix]] is [[kill (Unix)|kill]]
+
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 also disable crash recovery dialogs, like in [[Firefox]]. This can be true of graphically programs as well as [[console]] programs. The term for closing programs in [[Linux]]/[[Unix]] is [[kill (Unix)|kill]].
   
 
==Graphical programs==
 
==Graphical programs==
Line 72: Line 72:
 
**kill -2 `pidof rtorrent`
 
**kill -2 `pidof rtorrent`
 
*[[Firefox]]
 
*[[Firefox]]
  +
**To close a single window of Firefox
**wmctrl -c firefox
+
**:wmctrl -c Firefox
**:You may need to execute the close command twice
 
  +
**To quit Firefox, entirely (closing all windows)
**wmctrl -c firefox;wmctrl -c firefox
 
  +
<pre>
  +
if [[ -n `pidof firefox` ]];then
  +
WID=`xdotool search "Mozilla Firefox" | head -1`
  +
xdotool windowactivate --sync $WID
  +
xdotool key --clearmodifiers ctrl+q
  +
fi
  +
</pre>
 
*[[OpenOffice.org]]
 
*[[OpenOffice.org]]
 
**wmctrl -c openoffice
 
**wmctrl -c openoffice
  +
**:If you have multiple instances of OpenOffice, you need to execute the close command more than once
  +
*[[LibreOffice]]
 
**wmctrl -c libreoffice
 
**:If you have multiple instances of OpenOffice, you need to execute the close command more than once
 
**:If you have multiple instances of OpenOffice, you need to execute the close command more than once
 
*[[Galeon]]
 
*[[Galeon]]
Line 86: Line 96:
 
*[[Psi]]
 
*[[Psi]]
 
**wmctrl -c psi
 
**wmctrl -c psi
  +
*[[Rhythmbox]]
  +
**rhythmbox-client—quit
  +
* [[qBittorrent]]
 
** wmctrl -c qBittorrent
  +
* [[Transmission]]
  +
** wmctrl -c Transmission
 
*[[Pidgin]]
 
*[[Pidgin]]
** purple-remote "quit"
+
**purple-remote "quit"
  +
*[[VirtualBox]]
  +
<pre>
  +
if [[ -n `pidof VirtualBox` ]];then
  +
WID=`VBoxManage list runningvms|sed 's/.*{//'|sed 's/}//'`
  +
VBoxManage controlvm $WID savestate
  +
fi
  +
</pre>
  +
;Some untested commands
  +
*[[Gnome]]
  +
**gnome-session-save—kill
  +
*[[Openbox]]
  +
**openbox—exit
  +
* [[Fluxbox]]
  +
** fluxbox-remote "Exit"
  +
*[[XFCE]]
  +
** xfce4-session-logout
   
 
==Script to close many programs gracefully==
 
==Script to close many programs gracefully==
Line 93: Line 125:
   
 
<pre>
 
<pre>
  +
 
#!/bin/bash
 
#!/bin/bash
   
Line 98: Line 131:
 
if [[ -n `pidof rtorrent` ]];then
 
if [[ -n `pidof rtorrent` ]];then
 
echo "rtorrent is open, time to die (gracefully)...."
 
echo "rtorrent is open, time to die (gracefully)...."
kill -2 `pidof rtorrent`
+
(kill -2 `pidof rtorrent` &)
 
fi
 
fi
   
 
# mozilla firefox...
 
# mozilla firefox...
  +
if [[ -n `pidof firefox` ]];then
echo "closing mozilla (if its open)..."
 
 
echo "closing firefox..."
wmctrl -c mozilla
 
  +
WID=`xdotool search "Mozilla Firefox" | head -1`
# sometimes it takes a second shot (see below)
 
  +
xdotool windowactivate --sync $WID
  +
xdotool key --clearmodifiers ctrl+q
  +
fi
   
 
# galeon...
 
# galeon...
echo "closing galeon (if its open)..."
 
 
if [[ -n `pidof galeon` ]];then
 
if [[ -n `pidof galeon` ]];then
  +
echo "closing galeon..."
galeon -q
+
(galeon -q &)
 
fi
 
fi
   
 
# openoffice.org
 
# openoffice.org
  +
if [[ -n `pidof soffice.bin` ]];then
echo "closing openOffice (if its open)..."
 
 
echo "closing openOffice (if its open)..."
wmctrl -c OpenOffice
 
  +
(wmctrl -c OpenOffice &)
  +
fi
  +
  +
# LibreOffice
  +
if [[ -n `pidof soffice.bin` ]];then
 
echo "closing LibreOffice..."
  +
(wmctrl -c LibreOffice &)
  +
fi
   
 
# Inkscape
 
# Inkscape
  +
if [[ -n `pidof inkscape` ]];then
echo "closing Inkscape (if its open)..."
 
wmctrl -c Inkscape
+
echo "closing Inkscape..."
  +
(wmctrl -c Inkscape &)
  +
fi
   
 
# Gimp
 
# Gimp
 
echo "closing Gimp (if its open)..."
 
echo "closing Gimp (if its open)..."
wmctrl -c gimp
+
(wmctrl -c gimp &)
 
# Azureus
 
#echo "closing azureus (if its open)..."
 
# ???????????
 
   
 
# Pidgin...
 
# Pidgin...
 
echo "closing pidgin (if its open)..."
 
echo "closing pidgin (if its open)..."
purple-remote "quit"
+
(purple-remote "quit" &)
   
# mozilla firefox...
+
# Transmission...
echo "closing mozilla again (if its open)..."
+
echo "closing Transmission (if its open)..."
wmctrl -c mozilla
+
(wmctrl -c Transmission &)
  +
  +
# qBittorrent
 
echo "Closing qBittorrent (if its open)..."
  +
(wmctrl -c qBittorrent &)
  +
  +
if [[ -n `pidof VirtualBox` ]];then
  +
WID=`VBoxManage list runningvms|sed 's/.*{//'|sed 's/}//'`
  +
VBoxManage controlvm $WID savestate
  +
fi
 
</pre>
 
</pre>
   
==See Also==
+
==See also==
 
*[[kill]]
 
*[[kill]]
   
==External Links==
+
==External links==
 
* [http://linux.die.net/man/7/signal kill signals man]
 
* [http://linux.die.net/man/7/signal kill signals man]
 
 
 
[[Category:Howto]]
 
[[Category:Howto]]

Latest revision as of 14:46, 1 April 2018

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 also disable crash recovery dialogs, like in Firefox. This can be true of graphically programs as well as console programs. The term for closing programs in Linux/Unix is kill.

Graphical programs

Using a program called wmctrl, you can close programs the way they are meant 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 (never graceful)
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 likely 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`
fi

Close command for specific programs

  • rtorrent
    • kill -2 `pidof rtorrent`
  • Firefox
    • To close a single window of Firefox
      wmctrl -c Firefox
    • To quit Firefox, entirely (closing all windows)
if [[ -n `pidof firefox` ]];then
  WID=`xdotool search "Mozilla Firefox" | head -1`
  xdotool windowactivate --sync $WID
  xdotool key --clearmodifiers ctrl+q
fi
  • OpenOffice.org
    • wmctrl -c openoffice
      If you have multiple instances of OpenOffice, you need to execute the close command more than once
  • LibreOffice
    • wmctrl -c libreoffice
      If you have multiple instances of OpenOffice, you need to execute the close command more than once
  • Galeon
    • galeon -q
  • Gimp
    • wmctrl -c gimp
  • Inkscape
    • wmctrl -c inkscape
  • Psi
    • wmctrl -c psi
  • Rhythmbox
    • rhythmbox-client—quit
  • qBittorrent
    • wmctrl -c qBittorrent
  • Transmission
    • wmctrl -c Transmission
  • Pidgin
    • purple-remote "quit"
  • VirtualBox
if [[ -n `pidof VirtualBox` ]];then
  WID=`VBoxManage list runningvms|sed 's/.*{//'|sed 's/}//'`
  VBoxManage controlvm $WID savestate
fi
Some untested commands
  • Gnome
    • gnome-session-save—kill
  • Openbox
    • openbox—exit
  • Fluxbox
    • fluxbox-remote "Exit"
  • XFCE
    • xfce4-session-logout

Script to close many programs gracefully

Say you want to make a shutdown script, you can add this to close programs gracefully.


#!/bin/bash

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

# mozilla firefox...
if [[ -n `pidof firefox` ]];then
   echo "closing firefox..."
   WID=`xdotool search "Mozilla Firefox" | head -1`
   xdotool windowactivate --sync $WID
   xdotool key --clearmodifiers ctrl+q
fi

# galeon...
if [[ -n `pidof galeon` ]];then
	echo "closing galeon..."
	(galeon -q &)
fi

# openoffice.org
if [[ -n `pidof soffice.bin` ]];then
  echo "closing openOffice (if its open)..."
  (wmctrl -c OpenOffice &)
fi

# LibreOffice
if [[ -n `pidof soffice.bin` ]];then
	echo "closing LibreOffice..."
	(wmctrl -c LibreOffice &)
fi

# Inkscape
if [[ -n `pidof inkscape` ]];then
	echo "closing Inkscape..."
	(wmctrl -c Inkscape &)
fi

# Gimp
echo "closing Gimp (if its open)..."
(wmctrl -c gimp &)

# Pidgin...
echo "closing pidgin (if its open)..."
(purple-remote "quit"  &)

# Transmission...
echo "closing Transmission (if its open)..."
(wmctrl -c Transmission &)

# qBittorrent
echo "Closing qBittorrent (if its open)..."
(wmctrl -c qBittorrent &)

if [[ -n `pidof VirtualBox` ]];then
    WID=`VBoxManage list runningvms|sed 's/.*{//'|sed 's/}//'`
    VBoxManage controlvm $WID savestate 
fi

See also

External links