Send ctrl-C signal using bash script.


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting Send ctrl-C signal using bash script.
# 1  
Old 01-28-2016
Send ctrl-C signal using bash script.

Code:
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[i]}
  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?

Last edited by Don Cragun; 01-28-2016 at 03:39 PM.. Reason: Add CODE and ICODE tags.
# 2  
Old 01-28-2016
Control/c generates a sigint signal so lets try this - assume your script is called perf.shl

Code:
  /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.
This User Gave Thanks to jim mcnamara For This Post:
# 3  
Old 01-29-2016
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?
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

Expect command to send the user input enter or ctrl+c

Hey All, I am writing one script using expect, that script which is used in spawn will accepts only 1. Enter 2. Ctrl+c Press Control-C to exit, Enter to proceed. Could some one share some thoughts to send the above user inputs in linux expect block ? Thanks, Sam (0 Replies)
Discussion started by: SCHITIMA
0 Replies

2. Shell Programming and Scripting

Vi : Is it possible to send ctrl + d signal from a file made with vi and executing it.

Hi Experts, Is it possible to send ctrl + d signal from a inside a file made with vi, using Ctrl V , Esc and 004 , escape sequence. Since : 004 should exit the script if executed. Is this something possible. I am trying with vi , I put this code ^ , and trying to execute it but... (4 Replies)
Discussion started by: rveri
4 Replies

3. AIX

Send ctrl+C in expect script

Hi, Am trying to transfer file via FTP using expect script from server to client i need to interrupt the file transfer between server and client Please help what should used in expect code.. I used send "ctrl+c\r" expect "Aborted" but that didnt work.. I need what should... (3 Replies)
Discussion started by: Priya Amaresh
3 Replies

4. Shell Programming and Scripting

How to handle CTRL+Z or CTRL+C in shells script?

Hi, while executing shell script, in the middle of the process, if we kill the shell script( ctrl+z or ctrl+c), script will be killed and the files which using for the script will be in the folder. How to handle those scenarios. Is there any possibilities, if user breaks the script, I need to... (3 Replies)
Discussion started by: ckchelladurai
3 Replies

5. Shell Programming and Scripting

ctrl-c in bash script - Programming

Hi All, I need to place a ctrl-c interrupt in a bash script, there is no other way, it has to be done :) can someone please advise how would I go about this? i want to use ctrl c in below code, after the code excution of just 1 min or 1sec java Cspsamp 111.19.5.172 7025 rd1... (6 Replies)
Discussion started by: aish11
6 Replies

6. Shell Programming and Scripting

ctrl c trapping signal

im trying to make a trap signal 2 (ctrl c) in a bash script if a user presses ctrl c while running the script it should display an error message but not quit the bash script just yet. User will have to press "enter" to quit This is what i have so far #trap trap_control 2 #while true #do... (6 Replies)
Discussion started by: gangsta
6 Replies

7. Shell Programming and Scripting

How to send Ctrl Break combination in Expect

Greetings, I am writing an Expect script to automate multiple processes on an HP-UX system. Everything has gone fine so far but I now have run into a problem. One of the processes that I'm trying to automate requires the key combination of ctrl break and I have so far been unable to figure out... (1 Reply)
Discussion started by: g_trueblood2000
1 Replies

8. Shell Programming and Scripting

How to send SIGNAL to the thread?

Hello, I have to send SIGSEGV to the thread. What is the simplest and efficient way to do that? (6 Replies)
Discussion started by: Rahulpict
6 Replies

9. Shell Programming and Scripting

Ctrl-C signal propagation

Hello I have a master startup script (let's call it myScript) that displays a menu from which the user can start/stop several instances of a server. When I issue the start command for one of the servers from the menu and then exit myScript through the provided mechanism (enter "q" in this case),... (2 Replies)
Discussion started by: EvilBoz
2 Replies

10. AIX

Disable ctrl-c,ctrl-d,ctrl-d in ksh script

I wrote a ksh script for Helpdesk. I need to know how to disable ctrl-c,ctrl-z,ctrl-d..... so that helpdesk would not be able to get to system prompt :confused: (6 Replies)
Discussion started by: wtofu
6 Replies
Login or Register to Ask a Question