0

I am working on client and want to run a script on server which includes a trap.

The following minimal example works fine if I connect to server and run it in an interactive SSH session:

user@client:~$ ssh user@server
user@server:~$ sh -c 'trap "echo exiting..." EXIT'
exiting...
user@server:~$ 

However, if I don't connect to server interactively, but add the command to the ssh command, it doesn't work anymore:

user@client:~$ ssh user@server -- sh -c 'trap "echo exiting..." EXIT'
user@client:~$ 
finefoot
  • 3,060

1 Answers1

0

Maybe you can try something like this:

On the client machine, save the command in a file named (e.g.) .stackexchange:

user@client:~$ echo 'trap "echo exiting…" EXIT' > .stackexchange

Then, run a regular SSH command to connect to the server:

user@client:~$ ssh user@server

Once connected, run the command from the file:

user@server:~$ sh .stackexchange
exiting…
user@server:~$ 
Mix tutor
  • 110