Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Can't kill process started by another ssh session in bash script

Tags:

linux

bash

ssh

I'm writing a bash script where I need to login in a remote machine via ssh, start a process, end the ssh session, do some other things, and then login again via ssh to kill the process. But the process doesn't get killed. I have tried a lot of ways. Here is the part of the script that won't work:

ssh [email protected] &>/dev/null << EOF
tshark -i ens160 -w /home/localadmin/dns_traffic_61.pcap &>/dev/null &
EOF


ssh [email protected] &>/dev/null << EOF
kill $(pidof tshark)
EOF

I have also tried putting the tshark command in a script so I would kill the script like this:

ssh [email protected] &>/dev/null << EOF
sh tshark.sh &>/dev/null &
EOF

ssh [email protected] &>/dev/null << EOF
pid=$(ps -ef | grep tshark.sh | grep -v grep | awk '{print $2}')
kill $pid  
EOF

and this:

ps -ef | grep tshark.sh | grep -v grep | awk '{print $2}'|xargs kill

Nothing seems to work.

Note: When I connect via ssh manually I can kill the process, only the bash script can't.

like image 765
Giannis Pappas Avatar asked Apr 10 '26 10:04

Giannis Pappas


1 Answers

use single quotes around end marker so that expansion doesn't occur in current shell but in remote

ssh [email protected] &>/dev/null << 'EOF'
kill $(pidof tshark)
EOF

compare

cat << EOF
$$
EOF

and

cat << 'EOF'
$$
EOF
like image 195
Nahuel Fouilleul Avatar answered Apr 12 '26 13:04

Nahuel Fouilleul



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!