0

I would like to execute the following command through ssh:

echo "trap '/tmp/test &' 2 9" >> /tmp/output.txt

I am trying to do as follows:

ssh 127.0.0.1 -T "echo "trap '/tmp/test &' 2 9" >> /tmp/output.txt"

I'm using python subprocess as follows:

command = "echo \"trap '/tmp/test &' 2 9\" >> /tmp/output.txt"
addtrap = 'ssh root@' + rhost + ' -T "' + command + '"'
subprocess.Popen(addtrap, shell=True)

But, obviously, I'm getting some errors regarding quotes and apostrophes. Any option to avoid this issue?

1

One reliable way is to do:

ssh 127.0.0.1 sh << "EOF"
echo "trap '/tmp/test &' INT" >> /tmp/output.txt
EOF

Then you know what shell you get, here sh as opposed to the login shell of the remote user whichever it is, and you can easily pass the code for that sh to interpret verbatim without having to worry about quoting.

(BTW, you harldy want to use -T ever. You may want to use -t, but only to run interactive applications like vi or an interactive shell; signal 9, usually SIGKILL cannot be trapped, it's better to use signal names rather than numbers).

  • Thank you! I will test your solution, hope it works – ebdecastro May 28 at 21:20
  • It works. Thank you so much :) – ebdecastro May 29 at 10:42

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.