0

I have an app running on a Tomcat Server. On a certain situation I want to shutdown my raspberry using the command shutdown -h now. So far, I've tried this:

try {
    Runtime.getRuntime().exec("sudo shutdown -h now");
} catch (IOException e) {
    logger.error("Error trying to shutdown raspberry : " + e.getMessage());
}

The app runs, no exception caught but raspberry keeps turned on. Do I need to do something else to make it work? Is it possible at all?

1

The app runs, no excpetion caught but raspberry keeps turned on.

Just because the exec call didn't throw an exception, that doesn't mean the command work. The exec call returns a Process instance, you should check the exit code to verify the command actually worked.

Process process = Runtime.getRuntime().exec("sudo shutdown -h now");
if (process.exitValue() != 0) {
    logger.error("command failed");
}

Do I need to do something else to make it work? Is it possible at all?

As what user is the Tomcat process running? If it's not root, then you need to configure /etc/sudoers to let that user execute shutdown without password.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.