5

I'm working on Ubuntu and I want to create a bash file that do these things:
Launch one program (prog0) on core 1.
Wait 3 seconds.
Then log the information of CPU and Memory usage for prog0 (I use two instances of pidstat on core 0 for logging that information).
Then start another program (prog1) on core 0.
When prog1 has finished (I think that prog1 exits automatically), I would like to exit from all the previous process (prog0 and two pidstat).

taskset -c 1 prog0 -option0 &
sleep 3
taskset -c 0 pidstat 1 -C prog0 -u > log2 &
taskset -c 0 pidstat 1 -C prog0 -r > log3 &
taskset -c 0 prog1 -option1 > log1 

I don't know how to exit or kill all the processes started when prog1 has finished.

user1382278
  • 471
  • 1
  • 6
  • 21
  • 1
    use the $! environment variable to keep track of the PID's as you execute the commands. then you can kill -9 them at the end – CoderDake Feb 28 '13 at 15:29

2 Answers2

7

Add:

trap 'kill $(jobs -p)' EXIT

to the beginning of your script. This will kill all background jobs when your script exits.


To create a script open a new file and paste the following into it:

#!/bin/bash
trap 'kill $(jobs -p)' EXIT
taskset -c 1 prog0 -option0 &
sleep 3
taskset -c 0 pidstat 1 -C prog0 -u > log2 &
taskset -c 0 pidstat 1 -C prog0 -r > log3 &
taskset -c 0 prog1 -option1 > log1

Save the file as runme.sh.

Make it executable: chmod +x runme.sh

Run it by executing: ./runme.sh or to run it in the background: ./runme.sh &

Now, when the last command taskset -c 0 prog1 -option1 > log1 has finished, the script will exit and it will kill all the background processes that it started.

dogbane
  • 266,786
  • 75
  • 396
  • 414
  • This command kills all process when I exit manually (ctr+c). I would like to have the possibility to exit and kill all process automatically when the prog1 ends. – user1382278 Feb 28 '13 at 20:40
  • you should put your commands in a script with the `trap` before all the other commands and then run the script. – dogbane Mar 01 '13 at 08:40
  • Your command is at the top of the script, but pidstat is a loop, does not stop. I need to stop automatically when prog1 ends or to exit when prog1 ends. I do not know if it is clear. – user1382278 Mar 01 '13 at 09:11
  • I have updated my answer to show you how to put this in a script. – dogbane Mar 01 '13 at 09:18
  • 2
    +1 @dogbane That's the easiest solution I have ever seen for doing this. Great info. – Khushal Dave Aug 01 '14 at 17:51
0

How about using xargs which is a basic linux program in most systems

xargs -P 2 -I {} sh -c 'eval "$1"' - {} <<EOF
LD_LIBRARY_PATH=$HOME/vendor/usr/lib $HOME/vendor/usr/sbin/pgbouncer -v $HOME/vendor/etc/pgbouncer/pgbouncer.ini ; kill -15 $PPID
nameko run --config config.yaml $@ ; kill -15 $PPID
EOF

with this, both commands are ran in parrallel and not exiting till one of them dies. If one of it dies, it will kill the parent PPID and there by kill the other sister jobs/process job as well

I use this to get around a problem in CloudFoundry to start 2 process at the same time and not flout the rules that if a app processs dies, it should crash the container

Paying tribute to answers here