tags:

views:

36

answers:

1

Hi,

I would like to have a script with an infinite loop, that kills all processes created by this script when it gets executed again.

So basically you start the script via terminal:

bash poll.sh

and it's running infinitely, you open another terminal and again

bash poll.sh

and all poll.sh processes will get killed.

What would be the best way to do that? My first thought was to set environment variables, something like export POLLRUNNING=true and poll.sh would implement something like if [ $POLLRUNNING = true ]; exit 1

My second idea was to work with touch .isrunning and statement to ask for that file.

How would you do that?

A: 

pidof(8) supports an -x flag to specify scripts; it also supports an -o omitpid flag to leave out specific pids. So:

#!/bin/bash

PIDS=`pidof -o $$ -x poll.sh`

if [ x$PIDS != x ]
    then kill -s SIGTERM $PIDS
fi

sleep 10
sarnold
That is exactly what I needed. Thanks :)
mherwig