I’m writing a script to automate some tasks, one of the steps is to wait for a process started on a remote machine to finish execution.
My idea is to show a spinner while the process keeps executing, with inspiration from this question.
The relevant part of the code is this –
spinner_wait_remote() {
local pid=$1
local msg=${2-Process $pid still running}
local spin='-|/'
local i=0
while ssh "$remote_machine" "kill -0 $pid" 2>/dev/null
do
local i=$(( (i+1) %4 ))
printf "r${spin:$i:1} $msg"
sleep .1
done
printf "ne[Ae[K"
}
The problem is, it never enters the while loop, although the $pid
exists in the remote machine.
I have a similar method that waits for commands launched in the local machine and it works fine. The only difference is the ssh
command in the while
condition.
I also tried running the entire while loop on the remote machine, but it still doesn’t seem to enter the while loop.
spinner_wait_remote() {
pid=$1
msg=${2-Process $pid still running}
ssh "$remote_server" "
spin='-|/'
i=0
while kill -0 $pid 2>/dev/null
do
i=$(( (i+1) %4 ))
printf "r${spin:$i:1} $msg"
sleep .1
done
printf "ne[Ae[K"
"
}
Trilok Bhattacharya is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
Check out our Code of Conduct.