I am trying to reboot remote server from my java code. I have password-less ssh setup in place and sudoers file has argument in place so that no password is required for executing the system commands. I run the following command from "bash shell" on my local machine and my remote system shutsdown and comes up just fine
ssh -t user1@192.168.2.125 'sudo reboot'
Above command restarts the remote server without checking for any passwords; and now I pass the above command thru the java code as follows (I surrounded 'sudo reboot' in special characters so that it goes as one command)
String cmd="ssh -t user1@192.168.2.125 \'sudo reboot\'";
log.info("Executing command.."+cmd);
p = Runtime.getRuntime().exec(cmd);
//open error stream and input stream and process
However I get the following error
[2014-10-30 19:07:55,593] INFO Executing command..ssh -t user1@192.168.2.125 'sudo reboot'
[2014-10-30 19:07:57,070] ERROR ERROR while executing command: ssh -t user1@192.168.2.125 'sudo reboot'
[2014-10-30 19:07:57,070] ERROR Pseudo-terminal will not be allocated because stdin is not a terminal.
bash: sudo reboot: command not found
What am I doing wrong? I am trying to avoid 3rd party libraries(ex:JSch). I am using centos6 and Java 7
SOLUTION
1)took out single quotes as suggested by @hcs. Then I get another error mesg about ".. tty.."
2)updated sudoers file as suggested in the link https://serverfault.com/questions/15667/remotely-reboot-tomcat