2

I have a service in linux called appSevice, when I start/stop with these cmd, it works :

sudo systemctl start appSevice.service;
sudo systemctl stop  appSevice.service;

but the problem when I tried to execute these from JAVA code, I tried form exemple :

Runtime.getRuntime().exec("sudo systemctl stop appService.service");

but it didnt work, I can that the service is always running, any suggestions please to resolve this problem ?

service :

[Service]
Type=simple
ExecStart=/opt/soft/v1/launchAppService.ksh start
User=Jms-User
Restart=on-abort
Java user
  • 261
  • 3
  • 18
  • you execute it with `sudo`. who is providing the password? it is it `nopasswd`? – Oleksandr Kravchuk Aug 24 '20 at 08:19
  • 1
    remove `sudo` from your command, what it'll be happen if it's asking you for root password ? instead, run your java program with user having appropriate rights. execute the command line with the correct function: `Process exec(String[] cmdarray) This method executes the specified command and arguments in a separate process.` – Halayem Anis Aug 24 '20 at 08:20
  • yes I think this is the problem (password is problem), is there a way to execute these command without providing password ? – Java user Aug 24 '20 at 08:32
  • @ Halayem Anis , can you give an exemple of your commande please ? – Java user Aug 24 '20 at 08:37

2 Answers2

0

Steps to help you execute a system command via java program:

  1. create user and give him rights to execute systemctl command, see this thread allowing user to run systemctl/systemd services without password

  2. execute your java program using this user

  3. your java code should be: Runtime.getRuntime().exec(new String[]{"systemctl", "stop", "appService.service"});

the first argument is the command to execute, others are the arguments

Halayem Anis
  • 7,654
  • 2
  • 25
  • 45
  • thanks for your answer, but I use alreadya user. to execute the jar ..I tries your commande it doesnt work..when I executed sudo systemctl status appService.service; I can see that the service is running – Java user Aug 26 '20 at 07:26
  • @Javauser never, never.... never execute a program with root privileges ! – Halayem Anis Aug 26 '20 at 07:27
  • I know..because usually I run and stop the services using linuw commande line with :sudo systemctl start appService.service;sudo systemctl stop appService.service; but now I want to do that with java programatically – Java user Aug 26 '20 at 07:31
  • what did you get from standard or error output ? this thread will help you to get the output of a command execution: https://stackoverflow.com/questions/5711084/java-runtime-getruntime-getting-output-from-executing-a-command-line-program – Halayem Anis Aug 26 '20 at 07:40
  • I got this error :Failed to stop appService.service: Interactive authentication required.See system logs and 'systemctl status appService.service' for details – Java user Aug 26 '20 at 07:56
  • look at the edit please ..i have already a user called Jms-User – Java user Aug 26 '20 at 08:16
0

There is one way also. Create new user with LimitedRoot capabilities and use VISUDO to allow the user to run systemctl/systemd services without entering a password.

Example:

%LimitedRootUser ALL=NOPASSWD: /bin/systemctl restart appService.service
Fatih Şennik
  • 1,295
  • 5
  • 12