Hi,

I am writing a script which will run a process in a while loop, i.e., user will execute
sh test.sh

and the script will run a set of processes,
I want to know how to kill the script which is having a infinite while loop in its contents.

Killing a process (say test.out) is done by
pkill -9 test.out

and a script can be killed using
pkill -9 test.sh

But this is possible only when the user has started the script using
./test.sh

But if the user runs like this
sh test.sh

Then it cannot be killed using
pkill -9 test.sh

Since both options to run the script should be given to the user, in what way can i kill the script in case the user starts the script as
sh test.sh

I am using fedora 16.
Below is a sample code of what i am trying to do. You can create a test.out executable with following content

#include <stdio.h>
#include <iostream>

int main ()
{
    int i=1;
    while (i)
    {
        printf ("%d  ",i);
        i++;
        sleep (1);
    }

return (0);
}

the contents of script file test.sh is as follows

#!/bin/sh
# Run using ./test.sh  start
# Stop using sh test.sh stop   or  ./test.sh stop

start () {
while [ 1 ]
do
   echo "Starting.."
   ./test.out
   #./test2.out  
   sleep 3
done
}
stop () {
   echo "Stopping run.sh script"
    pkill -9 run.sh
    #** The above line works only if user has run with ./test.sh start
    # but not if the user has used sh test.sh start **
   exit 
}
case "$1" in
   start)
           start
       fi
       ;;
   stop)
       stop
       ;;
   *)
       echo "Invalid argument"

esac

you should read some init scripts.

a trace of script's PID is kept in a file, so init scripts are killed using their PIDs.

The problem you are having with pkill is that, by default, it only checks the process name (sh in your case). You can request that it match the entire command line by providing the -f flag. Somthing like:

$ sh test.sh

# In another terminal...
$ pgrep test.sh
$ pgrep -f test.sh
2015
$

I use pgrep here (and you should, too, at first) since pkill matching with -f could catch other processes that happen to have that same string in the command line.

ok, atleast i am able to kill test.sh, only thing is since i had that file opened in gvim, that is also getting killed. Since i will be running test.sh in a dedicated system, some other processes with name test.sh shldn't come up and get killed with pkill.

Now for just testing purpose is der any other option with `pkill` which will not `kill gvim` which has `test.sh` opened .. ??

Anyway that -f option helps..tq

If you spawn th eprogram exactly as sh test.sh then you can combine -f with -x to provide an exact match. For example:

pkill -x -f 'sh test.sh'

Or, instead of asking for an exact match, just provide more of the command line to lead to more unique results.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.