4

I am trying to do an operation in linux trying to burn cpu using openssl speed

this is my code from netflix simian army

#!/bin/bash
# Script for BurnCpu Chaos Monkey

cat << EOF > /tmp/infiniteburn.sh
#!/bin/bash
while true;
   do openssl speed;
done
EOF

# 32 parallel 100% CPU tasks should hit even the biggest EC2 instances
for i in {1..32}
do
  nohup /bin/bash /tmp/infiniteburn.sh &
done

so this is Netflix simian army code to do burn cpu, this executes properly but the issue is I cannot kill all 32 processes, I tried everything

pkill -f pid/process name
killall -9 pid/process name
etc.,

the only successful way I killed the process is through killing it via user

pkill -u username

How can I kill these process without using username?

any help is greatly appreciated

jww
  • 97,681
  • 90
  • 411
  • 885
Coding Ninja
  • 385
  • 1
  • 7
  • 25
  • Have you tried `kill -9 pid`? – Eli Sadoff Nov 08 '16 at 02:02
  • yes I tried that no luck – Coding Ninja Nov 08 '16 at 03:05
  • Possibly related/maybe a duplicate: [Best way to kill all child processes](http://stackoverflow.com/q/392022). Closely related is [How to kill all subprocesses of shell?](http://stackoverflow.com/q/2618403/) Someone with more Bash experience then me who understands the subtleties will have to decide. – jww Nov 08 '16 at 04:26

3 Answers3

4

finally, I found a solution to my own question,

kill -- -$(ps -o pgid= $PID | grep -o [0-9]*)

where PID is the process ID of any of the one processes running, this works fine but I am open to hear any other options available

source: http://fibrevillage.com/sysadmin/237-ways-to-kill-parent-and-child-processes-in-one-command

Coding Ninja
  • 385
  • 1
  • 7
  • 25
3

Killing a process doesn't automatically kill its children. Killing your bash script won't kill the openssl speed processes.

You can either cast a wider net with your kill call, which is what you're doing with pkill -u. Or you could use trap in your script and add an error handler.

cleanup() {
    # kill children
}

trap cleanup EXIT
John Kugelman
  • 349,597
  • 67
  • 533
  • 578
0

I had a similar problem and solution where I needed to kill a NodeJS server after some amount of time.

To do this, I enabled Job control, and killed async processes by group id with jobs:

set -m
./node_modules/.bin/node src/index.js &
sleep 2

kill -- -$(jobs -p)
bozdoz
  • 12,550
  • 7
  • 67
  • 96