3

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.

1 Answer 1

2

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
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.