3

I have a Java application that starts automatically when the headless machine (Ubuntu 14.04 Server) boots and runs as a user. When the application is finished I need it to execute a script to poweroff because an external breaker is about to be thrown which will cut the power.

I cannot allow to hang if it detects shutdown errors at the next boot.

My attempts to write a script all result in a request for the super-user's password. I can accept the password be present in text in a script as the machine is isolated from the internet.

I'm not proficient in bash or Linux.

script permissions:

$ ls -la shutdown
-rwsr-xr-x 1 root root 209 Mar 18 16:55 shutdown
7

There are various way to do this. The simplest, and worst, is to have the password in the script as you suggest. Since your computer is not connected to the internet, most security concerns are moot and you could just use sudo's -S option that allows passing the password from STDIN:

echo password | sudo -S shutdown -h now

A safer approach is to allow a specific user to run the shutdown command with no password. First, run visudo to edit the sudoers file and add this line:

nate  ALL=NOPASSWD:/sbin/shutdown

That will allow the user nate to run sudo /sbin/shutdown without a password being requested.

  • Thanks, I used your second suggestion and it works from the command line, Now to test it from Java. I clean forgot about sudoers ... – Nate Lockwood Mar 20 '15 at 18:34

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.