Mailing-List: contact cygwin-help AT cygwin DOT com; run by ezmlm List-Subscribe: List-Archive: List-Post: List-Help: , Sender: cygwin-owner AT cygwin DOT com Delivered-To: mailing list cygwin AT cygwin DOT com Message-ID: <3C6CB37D.6070507@rochester.rr.com> Date: Fri, 15 Feb 2002 02:06:37 -0500 From: Chuck Messenger Reply-To: chuckm AT rochester DOT rr DOT com, chuckm AT rochester DOT rr DOT com User-Agent: Mozilla/5.0 (Windows; U; Windows NT 5.0; en-US; rv:0.9.8) Gecko/20020204 X-Accept-Language: en-us MIME-Version: 1.0 CC: cygwin mailing list Subject: Contrib: cygwin shutdown script (Re: 'shutdown', games) References: <000f01c1b5c0$71bbdcd0$928e1941 AT alleluja> Content-Type: text/plain; charset=us-ascii; format=flowed Content-Transfer-Encoding: 7bit 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 AT rochester DOT rr DOT 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/