如何在UBUNTU中将脚本作为服务运行 [英] How to run a script as a service in UBUNTU

查看:446
本文介绍了如何在UBUNTU中将脚本作为服务运行的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个脚本,通常我在Linux上使用./myscript.sh(包含Java运行命令)运行.现在我想将其作为服务,以便它在机器重启后自动运行,如果我想停止并重新开始,只需找到该进程并终止并从命令行重新启动即可.

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.

我通过Google快速搜索发现的是将脚本放置在/etc/init.d目录中,但使用其他证书文件却使该脚本内部的命令混乱,而我通常将其放置在与该脚本位于同一级别的其他证书文件中.我是否需要将所有其他文件与此脚本一起移动到/etc/init.d下,还是有什么更好的方法可以在某个文件中简单提及此脚本的路径?

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?

推荐答案

您需要最简单的脚本如下:

[Unit]
Description=Virtual Distributed Ethernet

[Service]
ExecStart=/usr/bin/YOUR_SCRIPT

[Install]
WantedBy=multi-user.target

另外,您需要:创建新服务后systemctl daemon-reload.

Also you need: systemctl daemon-reload after creating new service.

这篇关于如何在UBUNTU中将脚本作为服务运行的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆