Hello,
I need to create a process that will kill a specific process if it's running. Let's just say the process is called win, actually called something else. It could be running multiple times on the machine and I would want to kill them all. Below is the code I have written so far, and it works but it picked up a processed called windat. I just want to kill the process if it's called win. Thanks.
for pid in `ps -elf | awk '{print $4"|"$15}' | grep win`
do
PIDID=`echo $pid | cut -d"|" -f1`
PROCESS=`echo $pid | cut -d"|" -f2`
log "Killing $PIDID $PROCESS"
done
#!/bin/sh
PROGRAM=$1
matches()
{
if test "$1" = "$PROGRAM"
then
return 0
fi
return 1;
}
ps -e | while read PID TTY TIME CMD
do
if matches $CMD
then
echo $PID
fi
done
save this as a script, then run with program name as argument
I've modified the script in a attempt to see if a process was killed or not, should be simple I thought. I set the flag to N at the beginning, and if in the if matches statement is true I set the flag to Y. After it's done with the while loop the match flag is N again. Why is that MATCH flag not global inside the while loop? something is eluding me here and I'm not sure what.
PROGRAM=$1
LOGFILE=/usr/local/logs/processkill.log
MATCH="N"
matches()
{
if test "$1" = "$PROGRAM"
then
return 0
fi
return 1;
}
ps -e | while read PID TTY TIME CMD
do
if matches $CMD
then
echo "Killing PID $PID for $CMD"
MATCH="Y"
fi
done
echo "$MATCH"