Search code examples
javaexecsystemd

Systemd service not work with Runtime Exec Command


When I run our program with wrapper from systemd service. The program catch the java.io.IOException: Cannot run program "netstat": error=2, No such file or directory but when I start directly from the wrapper, it won't catch this error and work fine.

We use Runtime Excec command in java 8

String command = "netstat -tulpn | grep :" + port;
Process process = Runtime.getRuntime().exec(command);
  • We have changed all the path of java to absolute path.
  • The systemd service is running as root
  • We can run netstat from centos7 on root
  • Shebang has been added

Cannot run program "netstat"


Solution

  • It's really inconvenient but I had to change netstat's path to absolute then the problem seems to be solved. But on different servers, netstat is in a different path.