0

Good Morning! trying to start a script on multiple servers w/ nohup and keep it running in the back ground.

The command (provided by my vendor) runs as expected when inputting directly on each server (the python script scrapes the log file and sends relevant info to another server via UDP):

  nohup tail -f /log/log.log | python /test/deliver.py > /dev/null 2>&1 & 

However, when placing into for loop to reach many servers, I must press Ctrl-C between each server to keep the loop going.

Please assist if possible:

for i in `cat /etc/hosts`; do ssh $i nohup tail -f /log/log.log | python /test/deliver.py > /dev/null 2>&1 &; done 

Solution (Thank you all for the help):

ssh -f user@host "cd /whereever; nohup ./whatever > /dev/null 2>&1 &"

Had to use the -f in combination with double quotes as described here:

Getting ssh to execute a command in the background on target machine

Stanislav Pankevich
  • 11,044
  • 8
  • 69
  • 129
kwanx
  • 11
  • 4
  • 1
    You are running `nohup tail -f /log/log.log` under `ssh` and the `| python ...` pipeline locally. Command parsing. Single quote the entire command you want run on the remote host. – Etan Reisner Nov 16 '15 at 16:12
  • Thank you for the quick response Etan - gave the below a try, same result - script starts successfully, but ssh session hangs waiting for ctrl-C to resume: for i in mytestserver; do ssh $i 'nohup tail -f /log/log.log | python /test/deliver.py > /dev/null 2>&1 &'; done – kwanx Nov 16 '15 at 16:24
  • I'm a bit surprised that `ssh` isn't exiting when the remote command ends but perhaps you need to force that. You could try sticking `exit` at the end of the quoted command (but in a quick test that didn't work here) so you might need the `-f` argument to `ssh` instead. Also looping over `/etc/hosts` seems like a *horrible* idea for this as it will hit localhost as well as each host potentially multiple times. Not to mention that you [shouldn't read lines with `for`](http://mywiki.wooledge.org/DontReadLinesWithFor). – Etan Reisner Nov 16 '15 at 16:33
  • Thx, no luck with exit or -f either (i have grep and awk commmands with cat /etc/hosts, trying to keep it simple and not show company sensitive info) – kwanx Nov 16 '15 at 17:35

2 Answers2

1

Like Etan Reisner already pointed out in comments, you need to run the entire command remotely. Additionally, you will need to detach standard input from the ssh command line.

Finally, the for loop is bad form; use while to read from a line-oriented input file.

while read -r host; do
    ssh "$host" 'nohup tail -f /log/log.log |
        python /test/deliver.py > /dev/null 2>&1 &' </dev/null
done </etc/hosts

(though on the machines I have seen, the format of /etc/hosts is not really suitable for this sort of processing. Perhaps only read the first field and discard the others? That's easy; while read -r host _; do...)

tripleee
  • 175,061
  • 34
  • 275
  • 318
  • 1
    You might also want to take out the `>/dev/null 2>&1` at least while debugging. – tripleee Nov 16 '15 at 17:20
  • 1
    If this diagnostic is correct then this question is a duplicate of http://stackoverflow.com/questions/29142/getting-ssh-to-execute-a-command-in-the-background-on-target-machine – tripleee Nov 16 '15 at 17:22
  • Ty tripleee, that got me where i needed, see edit in original question – kwanx Nov 16 '15 at 18:54
  • Please don't post answers to the question. If you want to post your own answer, that's fine. – tripleee Nov 17 '15 at 05:31
0

I'm not having any issues with this:

$ for i in {1..5}; do ssh -i me@ip nohup sleep 10 >/dev/null 2>&1 &
 => done
[7] 34057
[8] 34058
[9] 34059
[10] 34060
[11] 34061
$ #
[7]   Done                        
[8]   Done                        
[9]   Done                        
[10]   Done                        
[11]   Done
Marc Young
  • 3,854
  • 3
  • 18
  • 22
  • You are putting the local `ssh` command in the background. That's not what the OP is doing. Though it might be what they should be doing but I don't think so. I think they want the `-f` option instead (same idea). – Etan Reisner Nov 16 '15 at 16:31