How to send control + c from bash script?

I run several screens in a bash script, and then run the django runserver in each of them. I would also like to programmatically stop them all, which requires me to send Control+c to runserver .

How can I send these keystrokes from my bash script?

+45
bash gnu-screen
Apr 26 '11 at 11:28
source share
4 answers

Ctrl + C sends a SIGINT signal.

kill -INT <pid> also sends a SIGINT signal:

 # Terminates the program (like Ctrl+C) kill -INT 888 # Force kill kill -9 888 

Assuming 888 is your process id.




Note that kill 888 sends a SIGTERM signal, which is slightly different, but will also ask you to stop the program. Therefore, if you know what you are doing (there is no SIGINT handler attached to the program), a simple kill enough.

To get the PID of the last command running in your script, use $! :

 # Launch script in background ./my_script.sh & # Get its PID PID=$! # Wait for 2 seconds sleep 2 # Kill it kill $PID 
+57
Apr 26 '11 at 11:32
source share
— -

CTRL-C usually sends a SIGINT signal to a process, so you can simply:

 kill -INT <processID> 

from the command line (or script) to affect a specific processID .

I say “generally” because, as with most UNIX, this is almost endlessly configurable. If you execute stty -a , you will see which key sequence is bound to the intr signal. It will probably be CTRL-C , but this sequence of keys can be mapped to something else entirely.




The following script shows this in action (albeit with TERM , not INT , because sleep does not respond to INT in my environment):

 #!/usr/bin/env bash sleep 3600 & pid=$! sleep 5 echo === echo PID is $pid, before kill: ps -ef | grep -E "PPID|$pid" | sed 's/^/ /' echo === ( kill -TERM $pid ) 2>&1 sleep 5 echo === echo PID is $pid, after kill: ps -ef | grep -E "PPID|$pid" | sed 's/^/ /' echo === 

It basically starts the sleep log watch process and captures its process id. He then displays the relevant details of the process before killing the process.

After a short wait, he then checks the process table to see if the process has passed. As you can see from the script output, it really disappeared:

 === PID is 28380, before kill: UID PID PPID TTY STIME COMMAND pax 28380 24652 tty42 09:26:49 /bin/sleep === ./qq.sh: line 12: 28380 Terminated sleep 3600 === PID is 28380, after kill: UID PID PPID TTY STIME COMMAND === 
+11
Apr 26 '11 at 11:33
source share

You can get the PID of a specific process, such as MySQL, using the following commands: ps -e | pgrep mysql

This command will provide you with the PID for MySQL processing. for example, 13954 Now enter the following command into the terminal. kill -9 13954 This will kill the MySQL process.

0
Mar 17 '17 at 11:35
source share
  pgrep -f process_name > any_file_name sed -i 's/^/kill /' any_file_name chmod 777 any_file_name ./any_file_name 

for example, "pgrep -f firefox" will grep the PID to launch "firefox" and save that PID in a file named "any_file_name". The sed command will add kill at the beginning of the PID in the any_file_name file. The third line will make the executable file "any_file_name". Now the next line will destroy the PID available in the file "any_file_name". Writing four lines in a file and executing this file can perform -C control. The work for me is absolutely wonderful.

0
Apr 17 '17 at 6:48
source share



All Articles