Reputation: 1085
I need to execute a shell script via sshj and disconnect without waiting for the script to complete. The script can take a long time to complete, so I will be creating additional Java code to periodically check for results from the script. How would do I disconnect the sshj session without waiting for the shell script to complete?
Upvotes: 3
Views: 1148
Reputation: 2043
End your command with &
. Example:
nohup /usr/bin/longRunningProcess &
Once that command is submitted (eg, submit the command and then call Command.join()
), you can disconnect, and longRunningProcess
will continue in the background. The other thing to worry about is the command's output. You will have to redirect stdout
and stderr
elsewhere, such as a file of /dev/null
. So, the command will look something like this:
nohup /usr/bin/longRunningProcess > /dev/null 2>&1 &
And the Java code would be something like:
SSHClient client = ...
Session session = ...
Command command = session.exec(yourCommand);
// Wait for command to be submitted
command.join();
// Do some error checking by checking command.getExitStatus()
...
// Disconnect
session.close();
client.close();
Upvotes: 2