Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Trapping Ctrl-C in ssh inside bash script

Tags:

bash

ssh

I have a bash script that runs a SSH with some commands. If I Ctrl-C the bash script dies, but the ssh session (and its running commands) keep running. How can I effectively send the Ctrl-C to ssh session before dying my bash shell?

like image 903
Luís Guilherme Avatar asked Mar 02 '26 07:03

Luís Guilherme


1 Answers

This trap will send SIGTERM to all process in the process group when Ctrl-C is pressed:

trap 'kill 0' INT

It's worth noting that the ssh might leave jobs running on the other system, and that you can use a trap in the remote command to handle those as well.

like image 153
dan4thewin Avatar answered Mar 06 '26 14:03

dan4thewin



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!