Link to home
Start Free TrialLog in
Avatar of Zolf
ZolfFlag for United Arab Emirates

asked on

How to restart Service as root without a password prompt from application

Hello there,

I want to restart my java application which is running on Ubuntu 16.04 as service. But the problem I have is on Ubuntu when I want to restart my java application service, I do as follows
sudo service myapp restart

Open in new window

and then Ubuntu asks for password. Now how can I tell Ubuntu to not ask password when I send this cmd from my application.

//Restart application server
		router.get("/restartAppService").handler(ctx -> {
			/* Build command: sudo service AooService stop */
			  final ArrayList<String> command = new ArrayList<String>();
			  command.add("sudo service TutisService restart");
			  System.out.println("CMD ----"+command.toString());
			  final ProcessBuilder builder = new ProcessBuilder(command);
			  	try {
			  		builder.start();
				} catch (IOException e) {
					System.out.println("EXCEPTION WHILE RESTARTING SERVICE----"+e.getMessage());
					e.printStackTrace();
				}
			  //System.exit(0);
		});

Open in new window


Any help will be appreciated!!

cheers
Zolf
ASKER CERTIFIED SOLUTION
Avatar of Duncan Roe
Duncan Roe
Flag of Australia image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Put the user under which this script runs in /etc/sudoers. That should stop the password prompt.

That's not enough. You need to use NOPASSWD in sudoers as well
If you're going to be running your Java app on many machines + wish to restart from a central location, create an empty passphrase ssh key + deploy the public key part across all machines.
Avatar of Zolf

ASKER

Thanks for your comments.

I tried that but when I test in the terminal by running the restart service without sudo it still asks for password
without sudo
? You're meant to be running it with sudo