public inbox for cygwin@cygwin.com
 help / color / mirror / Atom feed
From: Chuck Messenger <chuckm@rochester.rr.com>
Cc: cygwin mailing list <cygwin@cygwin.com>
Subject: Contrib: cygwin shutdown script (Re: 'shutdown', games)
Date: Thu, 14 Feb 2002 23:00:00 -0000	[thread overview]
Message-ID: <3C6CB37D.6070507@rochester.rr.com> (raw)
In-Reply-To: <000f01c1b5c0$71bbdcd0$928e1941@alleluja>

  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


-------------------------------------

#!/bin/bash

# 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 chuckm@rochester.rr.com


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

pid=$$

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

c="$1"
p=""

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

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

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

c=$p
done

# 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`"
else
log "kill $1 $a -- `procname $a`"
kill $1 $a 2>> $logf
fi
done

rm $tmpf
}

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

log "Start by killing nicely"
killem

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 
ought
# 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:      http://cygwin.com/ml/#unsubscribe-simple
Bug reporting:         http://cygwin.com/bugs.html
Documentation:         http://cygwin.com/docs.html
FAQ:                   http://cygwin.com/faq/

      parent reply	other threads:[~2002-02-15  7:00 UTC|newest]

Thread overview: 3+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2002-02-14 17:31 'shutdown', games hongxun lee
2002-02-14 17:59 ` Randall R Schulz
2002-02-14 23:00 ` Chuck Messenger [this message]

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

  Avoid top-posting and favor interleaved quoting:
  https://en.wikipedia.org/wiki/Posting_style#Interleaved_style

* Reply using the --to, --cc, and --in-reply-to
  switches of git-send-email(1):

  git send-email \
    --in-reply-to=3C6CB37D.6070507@rochester.rr.com \
    --to=chuckm@rochester.rr.com \
    --cc=cygwin@cygwin.com \
    /path/to/YOUR_REPLY

  https://kernel.org/pub/software/scm/git/docs/git-send-email.html

* If your mail client supports setting the In-Reply-To header
  via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line before the message body.
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for read-only IMAP folder(s) and NNTP newsgroup(s).