Wednesday 28 November 2018

linux - ssh + here document – does Ctrl+C get to remote side?

As part of a series of tasks I have to do, I have to build a bash script that accesses a remote computer, executes 3 commands, waits for a process to end through a SIGINT (or simply end) and then do some cleanup afterwards.


Currently, I'm using this code:


#! /bin/bash

# local preparations
# ...

ssh -t $USER@remote.far <<-'COMMANDS'

echo "Preparing execution"

java -jar execute.jar &
executePID=$!

echo "Ready."
echo "CTRL+C to clean and close"

trap "kill $executePID" INT HUP
wait $executePID

#cleanup code here

echo "done. Logging out"
sleep 2
logout
COMMANDS

# Final local cleanup

This code seem to work just find until wait (builtin) command. wait seems to be consuming all commands that come after it and so, when I try to send a SIGINT (Ctrl+C), it seems to fail to execute the trap content and all the cleanup code.


How can I fix this so it works the way I expect?


I'm not allowed to split this bash file into multiple ones and I'm not allowed to create any scripts on the remote computer even if temporary.
Both computers are running the same flavor of Linux.

No comments:

Post a Comment

Where does Skype save my contact&#39;s avatars in Linux?

I'm using Skype on Linux. Where can I find images cached by skype of my contact's avatars? Answer I wanted to get those Skype avat...