I run my script "switch.sh" repeatedly (within 30 seconds). Each time script is triggered, script itself should kill all previous process.
Here is my code:
for pid in $(ps -fe | grep 'switch.sh' | grep -v grep | awk '{if ($2<$$) print $2}'); do
sudo kill -9 $pid
done
sleep 30
I can't figure out, why this can't work. The problem is awk '{if ($2<$$) print $2}. If I use numbers instead of $$ then it works, but if I use variables, then if statement doesn't work. I also try with variable instead of $$ but with no luck
In addition, I would consider using $2!=pid , since at some point, the numbering of processes may start anew, so an earlier process is not always guaranteed to have a lower number..
I noticed that, thanks. I also try with !=, but since I would trigger script very fast, even before previous script finish kill process, then somehow previous triggered script can kill next triggered script.
OK, I see what you mean. I guess the best method would be to record the pids of the processes as suggested earlier in this thread and use those to kill the processes.
Do these processes need to govern themselves? Perhaps it would be better to have a parent process who regulates its children and that can be triggered to fire off the next child only after killing any previous child processes first?