Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Start a remote process using java

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 [email protected] "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 [email protected] "sudo /root/path/server &"

sshpass -p 'password' ssh -o StrictHostKeyChecking=no [email protected] "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");
                exit(1);
        }

EDIT 1:

When I tried

sshpass -p 'password' ssh -o StrictHostKeyChecking=no [email protected] "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.

EDIT 2:

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.

EDIT 3:

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.

EDIT 4:

The script creates 8 ppp sessions that run in background.

like image 975
Shashank Singh Avatar asked Jan 02 '23 16:01

Shashank Singh


2 Answers

Simply need to use one more line of code process.waitFor(); which will fix your issue:-

 process = Runtime.getRuntime().exec(SHELL_COMMAND);
 process.waitFor();// add this line to complete execution of your shell command
 System.out.println("LLLLLLLOOOOOOOOOKKKKKKKK started process");
like image 92
Abhijit Pritam Dutta Avatar answered Jan 05 '23 14:01

Abhijit Pritam Dutta


If you want ssh to execute a command and return before the command completes, use the -f option, such as ssh -f user@host myServer. If it may produce output, you probably want to redirect stdout and/or stderr.

See the man page for details: https://man.openbsd.org/ssh#f

like image 45
Chris Avatar answered Jan 05 '23 14:01

Chris