Linux shell to execute keystroke (CTRL+C)

Posted on

Problem :

I’ve been encountering a problem while trying to simulate “Ctrl + C” within a bash script. Basically, I am trying to simulate keystrokes after another command has been run. While I seem to have no problem terminating the script all together, I am only trying to interrupt it without actually killing it off or closing it, or if possible continue on with the rest of the script.
This is the trouble line here:

    netstat -a -c & sleep 60 && kill SIGHUP 26777   

I just want to scan for a minute, and interupt without closing/terminating. I have tried every combination of commands I can think of. Some of the solutions I have seen seem incredibly complicated and have only resulted in error for me. Is there an easier way to go about this? -Thanks in advance

Solution :

How about this:

[user1@centos6 ~]$ netstat -a -c > /tmp/tmp.netstat-out & (nsPID=$! ; sleep 10 ; kill -STOP $nsPID)
[1] 25658
[user1@centos6 ~]$ 

[1]+  Stopped                 netstat -a -c > /tmp/tmp.netstat-out
[user1@centos6 ~]$ ps -p 25658
  PID TTY          TIME CMD
25658 pts/1    00:00:00 netstat
[user1@centos6 ~]$ jobs
[1]+  Stopped                 netstat -a -c > /tmp/tmp.netstat-out
[user1@centos6 ~]$ kill -CONT %1
[user1@centos6 ~]$ jobs
[1]+  Running                 netstat -a -c > /tmp/tmp.netstat-out &
[user1@centos6 ~]$ 

I stop the process with the SIGSTOP signal after 10 seconds (I’m impatient ;-)) and I can restart it any time I want with the SIGCONT signal (kill -CONTPID)

Leave a Reply

Your email address will not be published. Required fields are marked *