0

I'm trying to set up a script that performs commands on multiple ssh hosts (Ubuntu 16/18, CentOS, SuSE) at once.

It is working fine with different commands, unless I have to execute a process on the remote host that is forking in background. For instance:

for ip in $(cat iplist); do ssh root@"$ip" "/usr/local/bin/pivx-cli stop && cp -r /root/.pivx /root/.pivxbck && cp /root/deploy/pivx* /usr/local/bin/ && /usr/local/bin/pivxd"; done

With this code, the script will freeze when pivxd is launched after display the SERVER STARTING statement, and the ssh connection will stay open (even if the process is forked correctly). If I kill the pid of the ssh process in another console, the pivxd on remote host will stay open, though.

So I tried to put an & at the end:

for ip in $(cat iplist); do ssh root@"$ip" "/usr/local/bin/pivx-cli stop && cp -r /root/.pivx /root/.pivxbck && cp /root/deploy/pivx* /usr/local/bin/ && /usr/local/bin/pivxd" & done

This will work and apparently produce the desidered output, but it will keep N ssh processes active. And again, if killed, remotely pivxd will stay up, and this sounds strange for me.

So after research I tried with screen, but in this case pivxd will not start:

for ip in $(cat iplist); do ssh root@"$ip" "/usr/local/bin/pivx-cli stop && cp -r /root/.pivx /root/.pivxbck && cp /root/deploy/pivx* /usr/local/bin/ && screen -d -m /usr/local/bin/pivxd"; done

I have mainly two goals:

  • cleanly close the connection after the pivxd process correctly forks.

  • Execute these commands in true parallel way, actually each will not run until the one before has finished, even with trailing &.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Browse other questions tagged or ask your own question.