[NTLUG:Discuss] More shell scripting madness

Eric Schnoebelen eric at cirr.com
Thu Jul 5 11:38:06 CDT 2007


. Daniel wrote:
- Here's what I'd like to add to it.  I'd like to add some code
- that will detect if a previous instance of this script is
- currently running and if it is, kill it and exit.  If it's not
- running, just go into the loop.  So the result would be, if I
- click the icon once, it runs the loop.  If I click the icon
- again, it will stop the loop.

Here's some code I use in various scripts..

	pgm=${0##*/}
	if [ -f $HOME/.${pgm}.pid ] ; then
	    if kill -0 $(cat $HOME/.${pgm}.pid 2>/dev/null) ; then 
		echo "${pgm}: already running"
		exit 0;
	    fi
	fi


Change the block with the echo to do what you wish it to do if
the program is already running.  I just want it to bail, and
complain, most of the time..

--
Eric Schnoebelen		eric at cirr.com		http://www.cirr.com
    I sometimes wonder why I bother waking up in the morning. It would
    save so much time if I just admitted defeat and stayed asleep, and
    the results could hardly be much different.... -- Brian Pickering



More information about the Discuss mailing list