neil on the web

computing - xdm shutdown button

The Itch! Why I Wanted A Shutdown Button

I have machines that boot straight into an xdm login screen. I wanted a shutdown button on the xdm login so that I didn't have to su to shutdown.

I don't have much knowledge about xdm but it seemed to me that xdm didn't have that facility. Quite extensive Google searches found some information but it was nothing that I wanted to use as it was, or even understood for that matter.

The Scratch! Adding A Shutdown Button To An xdm Login Screen

I've taken a bit from what I found and modified it to fit my needs and along the way I found out more about xdm and what wish is.

The solution is a short Tcl/Tk script that runs only while the xdm login screen runs. The script displays a button that when clicked shuts down the machine. I've used it on two machines - both running Debian 3.0 on laptops.

I suggest that this approach is suitable for single user or home environments but that it may not be appropriate for multi-user or professional environments.

You need Tcl/Tk Installed on your machine to use this method.

You should be able to copy and paste the code from this page.

My thanks to Rainer for prompting me to do a little more work on the script by adding a reboot button and to improve these instructions.

Add Lines to /etc/X11/xdm/Xsetup

/usr/local/sbin/xdmshutdown -geometry +350+400 &

echo $! > /var/run/xdmshutdown.pid

The first line runs the xdmshutdown script and positions it on the screen.

The second line writes the xdmshutdown PID to the file /var/run/xdmshutdown.pid.

Add Lines to /etc/X11/xdm/Xstartup

If we don't remove the button after a login then it will stay visible on our desktop. This code removes the button after a login.

pid=$(cat /var/run/xdmshutdown.pid 2>/dev/null)

test "$pid" && kill -9 $pid 2>/dev/null

The first line stores the PID of xdmshutdown in the variable $pid (if it exists in the file /var/run/xdmshutdown.pid). If it's not there then the error message goes to null (parallel universe?).

The second line tests if the variable $pid contains a value. If it does then the kill command kills the process number $pid (our shutdown button script). Any errors go to /dev/null and our shutdown button disappears.

The lines above should go near the top of the file otherwise xdmshutdown may continue running after login. I put mine immediately after the comments:


#!/bin/sh
#
# $Id: Xstartup 189 2005-06-11 00:04:27Z branden $
#
# This script is run as root after the user logs in.  If this script exits with
# a return code other than 0, the user's session will not be started.

# Added NT 2010-05-06
# for xdmshutdown
#
pid=$(cat /var/run/xdmshutdown.pid 2>/dev/null)
test "$pid" && kill -9 $pid 2>/var/log/xdmshutdown.log
# Default Xstartup scrip continues below

The basic xdmshutdown script

Put this script in /usr/local/sbin. Make sure that root owns xdmshutdown (chown root:root xdmshutdown). Make xdmshutdown executable (chmod +x xdmshutdown).

#!/usr/bin/wish
# NT 20 October 2002 - adapted/copied from various similar
# scripts dragged kicking and screaming from the bowels of the web:)
#
proc shutdownf {} { exec /sbin/shutdown -h now 2>/dev/console >/dev/console }

button .shutdown -text "Shutdown this computer" -background yellow \
-activebackground red -command shutdownf
pack .shutdown -side right

The first line defines an command to run and associates it with the process name shutdownf.

The second line creates a button containing a text label, defines some colours for the button. If someone clicks on the button then the command shutdownf runs. The effect is a yellow button (caution) that changes colour to red (really, really caution) when the mouse pointer passes over it.

The third line displays the button.

Extended xdmshutdown script

This script adds a reboot button as well as a a shutdown button.

Put this script in /usr/local/sbin. Make sure that root owns xdmshutdown (chown root:root xdmshutdown). Make xdmshutdown executable (chmod +x xdmshutdown).

#!/usr/bin/wish
# NT 20 October 2002 - adapted/copied from various similar
# scripts dragged kicking and screaming from the bowels of the web:)
# NT 27 January 2003 - added a reboot button
#

proc shutdownf {} { exec /sbin/shutdown -h now 2>/dev/console >/dev/console }

proc rebootf {} { exec /sbin/reboot 2>/dev/console >/dev/console }

button .shutdown -text "Shutdown this computer" -background yellow \
 -activebackground red -width 25 -command shutdownf

button .reboot -text "Reboot this computer" -background yellow \
 -activebackground orange -width 25 -command rebootf

pack .reboot -side top
pack .shutdown -side top

See the basic script for an explanation about what this script does.

The changes are:

I suggest that you also alter the line in Xsetup that calls xdmshutdown to read:

/usr/local/sbin/xdmshutdown -geometry +330+400 &

This will place the buttons on login screen so that they fit in with the other items (or not depending on your taste).

If anyone knows of a better way to do this or can suggest improvements then please let me know.