Machine A connects to a remote machine B using sshpass and that remote machine B doesn't give ssh access to root directly so I connect to it through user and use sudo and I also have disabled password for the user when he does sudo su. When the commands are simple like ls -lrt /some/dir I get correct output for the command but when I start a server/shell script on remote machine (server takes no time while the script takes 1 minute to run) the output is shown but the bash doesn't return back to normal(i.e. it looks like it is doing some work but I know that the server has started/script has finished).

Command used :

sshpass -p 'password' ssh -o StrictHostKeyChecking=no user@ "sudo ls /dev/ttyUSB*"

above works fine, I get the output and the bash is returned so I can write more commands and do stuff.

sshpass -p 'password' ssh -o StrictHostKeyChecking=no user@ "sudo /root/path/server &"

sshpass -p 'password' ssh -o StrictHostKeyChecking=no user@ "sudo /root/path/to/script/scriptTakes1Minute param1 param2"

But, the above doesn't work as expected. It looks like it is doing work but it isn't the bash is not returned and so I can't write commands further.

I can't share the script or the server program as it is company's code. But the server code uses UNIX datagram sockets to communicate.

/* Creating a UNIX datagram socket for Server Application */
if ((sock = socket(AF_UNIX, SOCK_DGRAM, 0)) < 0)
    perror("server: socket");

When I tried

sshpass -p 'password' ssh -o StrictHostKeyChecking=no user@ "sudo /root/path/server >/dev/null &2>1 </dev/null &"

the server program started correctly but the script didn't work as expected and didn't return the bash but as soon as I pressed ENTER the bash returned, I know it means something but don't know what.

I can't redirect output to a file as I don't know how much time will the command take to run in total it may take a minute or few.

I found something that will help a lot(I guess). The script that is kept on that machine B creates few processes in background and so the sshpass doesn't return until all those background processes are killed manually from Machine B. As soon as those programs are killed we get that output as well as the bash back.

The script creates 8 ppp sessions that run in background.

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.