4

I run a remote script using ssh:

local script:

local_command
ssh -p 222 user@server 'sh -s' < ./remote_run.sh

remote script:

some_command & 
sleep 10000
trap 'echo exiting' SIGINT SIGTERM SIGABRT SIGKILL EXIT

I need a trap on remote script for cleaning up (kill running processes and remove files). But used approach does not work.

Any suggestions?

  • 2
    Maybe SIGHUP when ssh is involved? – Matt Jun 25 '14 at 15:17
  • Are you asking what to put in place of echo exiting to perform this cleanup? – Barmar Jun 25 '14 at 20:48
  • @Barmar, No, the problem is that 'echo exiting' is never invoked. – Dmitry Eskin Jun 25 '14 at 20:50
  • Then @mtm is probably right, you need to add more signals to the trap. – Barmar Jun 25 '14 at 20:51
  • Do you want the cleanup to happen when some_command exits? Or after 10000 seconds? – Gilles Jun 25 '14 at 23:46
0

Tried and worked like charm.

ssh -p 222 user@server "nohup ./remote_run.sh"

and in remote_run.sh

some_command &
sleep 10000
ps -aux | grep "[r]emote_run.sh" | awk '{print $2}' | xargs kill -9
#killed

obviously, the script is transferred to remote host first. :)

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.