Send ctrl-C signal using bash script.

declare -a array=( "LLC-load-misses" "LLC-loads" "LLC-store-misses" "LLC-stores" "branch-load-misses" "branch-loads" "dTLB-load-misses" "dTLB-loads" "dTLB-store-misses" "dTLB-stores" "iTLB-load-misses" "iTLB-loads" "branch-instructions" "branch-misses" "bus-cycles" "cache-misses" "cache-references" "cpu-cycles" "instructions" "ref-cycles" "alignment-faults" "context-switches" "cpu-clock" "cpu-migrations" "emulation-faults" "major-faults" "minor-faults" "page-faults" "task-clock" )


arraylength=${#array[@]}
#echo $arraylength
# use for loop to read all values and indexes
for ((i=0; i<${arraylength}-25; i++))
do  
  var1="perf stat --log-fd 3 --repeat 10 -e "
  var2=${array}
  var3=' filebench 3>resultsfilebench' 
  var4=$i
  var5='.log'
  varfinal=$var1$var2$var3$var4$var5
  eval $varfinal
done

When I try to run this script, it opens filebench application and waits for the user to press ctrl-C to iterate through the loop. I want this to be done automatically. perf command first enters the applications waits for the user to press ctrl-C and then print out the results. Is there any way to automate this by iterating through the loop without pressing ctrl-C?

Control/c generates a sigint signal so lets try this - assume your script is called perf.shl

  /path/to/perf.shl  > perf.log &   # create a child process
  export pid=$!
  while [ kill -0 $pid 2>/dev/null -eq 0 ] # check the child process exists.
  do      
       sleep 10   # wait 10 seconds -- you change this to what you need
       kill -2  $pid 2>/dev/null   # sigint to child process --ctrl/c
  done 

perf is based on Brandon Gregg's dtrace which starts when you execute it, then runs until ctrl/c is received then dumps data.
So. You may not really want a 10 second sleep.

1 Like

You are really awesome. Saved my day. Implemented this perfectly.

---------- Post updated at 03:47 PM ---------- Previous update was at 02:59 PM ----------

I have an other problem. I'm trying to record metrics of an application called memcached which is server client based application. So I started the server using 'memcached -t 4 -m 4096 -n 550' and in other window started the client. So the connection is established. Now I'm trying to get the pid of server and passing that as an input argument to perf using this command:

perf stat --log-fd 3 -e cycles -p $pid 3>result.log &

But according to your program the data is not being forwarded to the result file. At the end of the execution the result file is empty. Can you please help me with this?