[英]Run Linux service from java code causing service: command not found
I want to run Linux service from my Java code. 我想从我的Java代码运行Linux服务。 I tried to do it in many ways.
我尝试了许多方法。 Even writing .sh script that executes service start, but I keep getting following error: /opt/scripts/service-stop.sh: line 3: service: command not found
甚至编写执行服务启动的.sh脚本,但我一直收到以下错误:/opt/scripts/service-stop.sh:第3行:service:命令未找到
Here my .sh script 这是我的.sh脚本
echo "Service name: $1"
echo "Start args: $2"
service $1 start $2
Java code: Java代码:
private void executeCommandAndWait(String... args) {
ProcessBuilder pb= new ProcessBuilder().command(args);
Process proc = pb.start();
}
executeCommandAndWait("/bin/bash", "-c", "/opt/scripts/service-start.sh", srvName, "some-arg");
'service' is a system call, and needs to be run by a sudo user. “服务”是系统调用,需要由sudo用户运行。 I'm guessing that it isn't in the PATH for the Java process/user.
我猜它不在Java进程/用户的PATH中。 You might get away with using its full path:
您可能无法使用其完整路径:
/$PATH_TO_SERVICE/service $1 start $2
Try systemctl
in your bash script: 在您的bash脚本中尝试
systemctl
:
I'm not sure how you pass args to it (maybe this answer will help), but the basic is: 我不确定您如何将args传递给它(也许这个答案会有所帮助),但是基本是:
systemctl start <service>
or 要么
sudo systemctl start <service>
The Arch wiki has some very decent documentation :) Arch Wiki有一些非常不错的文档 :)
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.