This is the mail archive of the mailing list for the Cygwin project.

Index Nav: [Date Index] [Subject Index] [Author Index] [Thread Index]
Message Nav: [Date Prev] [Date Next] [Thread Prev] [Thread Next]
Other format: [Raw text]

Contrib: cygwin shutdown script (Re: 'shutdown', games)

  hongxun lee wrote:

>Sorry for the attachment as i know some hate to see them in emails..This 
>is my first try of the command 'shutdown', and it did close the windows 
>applications right away, but you can see that it can't close Cygwin..Is 
>it supposed to be so ?

Yeah, that's what I've found, too -- it's a pain to do shutdowns with 
Cygwin programs running.

You prompted me to solve my problem. I've written a script which shuts 
down all Cygwin programs, as nicely as possible. Polite but firm. I've 
included it below. To make it operate properly, invoke it the way it 
says in the comment block at the top:

- Chuck



# The purpose of this script is to shut down all running Cygwin programs,
# prior to shutting down the computer. This is a workaround for the problem
# that Windows is unable to shut down Cygwin shells nicely.
# The script first gives each Cygwin program a chance -- kill with no flags.
# If that fails, it kills everything that's left with "kill -9".
# Unfortunately, I was unable to get Cygwin's "shutdown" to work correctly
# (see note at bottom of script). It would be nice if that worked -- this
# you could simply invoke this script to shut down your computer.
# You should link to this program from a desktop shortcut like so (adjust
# as needed):
# c:\cygwin\bin\bash.exe --login /usr/local/shut
# (assuming you store this in /usr/local/shut).

# Written by Chuck Messenger, 2002

tmpf=/tmp/`basename $0`.tmp
logf=/tmp/`basename $0`.log


log() {
echo $1 >> $logf
echo $1

is_descendant_of() {
# Returns 0 if $1 is a descendant of $2
# In the code, c is child, $2 is ultimate parent, i is index
local c p


if [ "$1" == "$2" ]; then
return 0

while [ "$p" != "$2" ]; do
p=`grep "^ *$c " $tmpf | awk '{print $2}'`

if [ -z "$p" ]; then
# No parent for $c
return 1


# Success -- $1 is a descendant of $2
return 0

procname() {
grep "^ *$1 " $tmpf | awk '{print $8}'

killem() {
# Don't kill any processes which are part of the parent of this shell script

ps agux | grep -v "^ *PID" > $tmpf

for a in `awk '{print $1}' $tmpf`; do
if is_descendant_of $a $pid ; then
log "Don't kill descendant $a -- `procname $a`"
log "kill $1 $a -- `procname $a`"
kill $1 $a 2>> $logf

rm $tmpf

log "Shutting down at `date`"
log "PID is $pid"

log "Start by killing nicely"

sleep 2

log "No more Mr Nice Guy..."
killem -9

# This ought to be run from a root bash (e.g. from a desktop shortcut).
# In this case, there's no point in trying to kill the parent ($PPID).
# And, I've found that in the case where you call this script from an
# interactive bash shell, that trying to kill the parent process (which 
# to be the interactive bash shell) causes a hung condition.
# So, just call it quits...

log "Bye, now..."

# Unfortunately, on Win2k, "shutdown now" causes a reboot rather than a
# shutdown...
# And on Win98, "shutdown now" fails, because it complains that the shell
# in which this script is being run must itself first be killed.
# So leave out "shutdown now" for now...

#shutdown now

Unsubscribe info:
Bug reporting:

Index Nav: [Date Index] [Subject Index] [Author Index] [Thread Index]
Message Nav: [Date Prev] [Date Next] [Thread Prev] [Thread Next]