bash 如何在 UBUNTU 中将脚本作为服务运行
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/38739198/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
How to run a script as a service in UBUNTU
提问by agilob
I have a script which normally i run using ./myscript.sh(contain java run command) on linux. Now i want to make it as a service so it run automatically after machine restart and if i want to stop and start again simply find the process and kill and start it again from command line.
我有一个脚本,通常我在 linux 上使用 ./myscript.sh(包含 java 运行命令)运行。现在我想将它作为一项服务,以便它在机器重启后自动运行,如果我想停止并重新启动,只需找到该进程并杀死并从命令行再次启动它。
What i find with quick google search is to place the script in /etc/init.d directory but confusing with one thing that command inside this script using other certificate files which i normally place on same level where this script is place. Do i need to move all others file along with this script under /etc/init.d or is there any better way that i simply mention the path of this script in some file?
我通过快速谷歌搜索找到的是将脚本放在 /etc/init.d 目录中,但使用其他证书文件在这个脚本中的命令混淆了一件事,我通常将这些文件放在这个脚本所在的同一级别。我是否需要将所有其他文件与 /etc/init.d 下的这个脚本一起移动,或者有没有更好的方法我只是在某个文件中提到这个脚本的路径?
回答by agilob
You need to write systemd service file.
您需要编写 systemd 服务文件。
Simplest script looks like this:
最简单的脚本如下所示:
[Unit]
Description=Virtual Distributed Ethernet
[Service]
ExecStart=/usr/bin/YOUR_SCRIPT
[Install]
WantedBy=multi-user.target
Also you need: systemctl daemon-reload
after creating new service.
您还需要:systemctl daemon-reload
在创建新服务之后。