0

My script looks like:

#!/bin/bash

myProgram1&
pidMyProgram1=$!

ssh myUserName@pluto
myProgram2&
pidMyProgram2=$!

function cleanup
{
   kill -9 $pidMyProgram1 $pidMyProgram2
   exit 0
}

trap cleanup SIGINT SIGTERM

while :
do
   sleep 1
done

It does not work as I need it to. I am trying to launch two processes, one on another box called pluto. I want these two processes to run indefinitely until I hit control+c, then I want both processes to stop. When I run this I end up ssh'ed into pluto, but my processes aren't even running. Any ideas as to how I can handle my requirement of starting the process on a seperate box? I am pretty new to BASH...

smuggledPancakes
  • 9,881
  • 20
  • 74
  • 113
  • possible duplicate of [Getting ssh to execute a command in the background on target machine](http://stackoverflow.com/questions/29142/getting-ssh-to-execute-a-command-in-the-background-on-target-machine) – Dan S Oct 21 '14 at 20:09
  • I don't agree with this being a duplicate question. There are other factors, like using trap to cleanup the processes on those seperate machines. – smuggledPancakes Oct 21 '14 at 21:38

2 Answers2

1

ssh myUserName@pluto starts a shell on the remote machine and does not exit until that shell exits. It does not cause the remainder of your script to be executed on the remote machine. You just need to pass myProgram2 as an argument to ssh; that program must exist on the remote machine, not just on the local box.

myProgram1 & pidMyProgram1=$!

ssh myUserName@pluto myProgram2 & pidMyProgram2=$!

function cleanup
{
   kill $pidMyProgram1 $pidMyProgram2
   exit 0
}

trap cleanup SIGINT SIGTERM

while :
do
   sleep 1
done

Assuming myProgram1 will run forever until interrupted, you can replace the busy while loop with

wait $pidMyProgram1
chepner
  • 497,756
  • 71
  • 530
  • 681
  • When I run my script I get a no such process message from Bash for the process that I created after using ssh. Is there another way to obtain the correct PID? – smuggledPancakes Oct 21 '14 at 20:40
  • They should be correct. It's possible that the programs are exiting naturally before `cleanup` is called, though. – chepner Oct 21 '14 at 20:57
  • When I ssh into the other machine the PID is definitely different. In fact, shouldn't I have to ssh into that machine to close the PID? – smuggledPancakes Oct 21 '14 at 21:36
  • If you kill `ssh`, the process running on the remote hosts should receive `SIGHUP` and exit as well. – chepner Oct 21 '14 at 21:43
  • Could you update your answer to address the concern of getting the correct PID and making sure it is closed on exit? I am not getting it still. – smuggledPancakes Oct 21 '14 at 21:49
  • When you kill `ssh`, the connection to the remote machine is closed, and the program that was running receives `SIGHUP` and exits as a result. You don't need the process ID of that program, because you only need the local process ID of the `ssh` in order to kill it. – chepner Oct 21 '14 at 21:52
0

What I needed was ssh -l username hostname 'pkill processName'

It finally works!

smuggledPancakes
  • 9,881
  • 20
  • 74
  • 113