Linux使用systemd自定义服务

vi /etc/systemd/system/kylin.service

[Unit]
Description=kylin4
After=network.target

[Service]
Type=forking
#EnvironmentFile=所需环境变量文件或参数文件
ExecStart=/opt/software/bigdata/kylin4/bin/kylin.sh start
ExecStop=/opt/software/bigdata/kylin4/bin/kylin.sh stop
Restart=always
RestartSec=5
User=root

[Install]
WantedBy=multi-user.target

环境变量不可用,需配置,创建环境文件,使用下面格式,无需在service文件中指定路径

mkdir /etc/systemd/system/kylin.service.d
touch /etc/systemd/system/kylin.service.d/env.conf

vi /etc/systemd/system/kylin.service.d/env.conf

[Service]
# 环境变量格式如下
Environment="JAVA_HOME=/usr/java/jdk1.8.0_181-cloudera"

重新加载启动,开机启动

systemctl daemon-reload
systemctl start kylin
systemctl enable kylin

启动失败,排查问题命令

# 大概日志
journalctl -xe
# 详细日志
journalctl -u kylin.service

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
Linux下启动界面程序需要使用桌面环境的支持,而自定义服务通常是在命令行下运行的程序,因此需要将命令行程序转化为可以在桌面环境下运行的界面程序。这可以通过以下步骤实现: 1. 安装桌面环境:如果你的Linux系统没有安装桌面环境,则需要安装一个。常见的桌面环境有Gnome、KDE、XFCE等,你可以根据自己的喜好选择一个。 2. 创建一个界面程序:使用你喜欢的编程语言创建一个可以在桌面环境下运行的程序。例如,使用Python和GTK+创建一个简单的界面程序: ```python #!/usr/bin/env python import gi gi.require_version('Gtk', '3.0') from gi.repository import Gtk class MyWindow(Gtk.Window): def __init__(self): Gtk.Window.__init__(self, title="Hello World") self.button = Gtk.Button(label="Click Here") self.button.connect("clicked", self.on_button_clicked) self.add(self.button) def on_button_clicked(self, widget): print("Hello World") win = MyWindow() win.connect("destroy", Gtk.main_quit) win.show_all() Gtk.main() ``` 3. 将界面程序与自定义服务关联:在自定义服务的脚本中添加启动界面程序的命令。例如,以下脚本在启动自定义服务之后,启动一个名为mygui的界面程序: ```bash #!/bin/bash # start my custom service ... # start GUI program export DISPLAY=:0.0 /usr/bin/python /path/to/mygui.py ``` 在这个脚本中,我们首先启动自定义服务,然后导出DISPLAY环境变量,这是Linux系统中用于指定图形界面显示的环境变量。接下来,我们启动Python解释器,并运行mygui.py脚本,这将启动一个名为mygui的界面程序。 4. 将自定义服务添加到系统服务:将脚本保存为一个可执行文件,并将它添加到系统服务中,这样它就可以在系统启动时自动运行了。在Ubuntu系统中,你可以使用systemd来管理系统服务。例如,以下是一个将自定义服务添加到systemd中的示例: ```ini [Unit] Description=My Custom Service [Service] ExecStart=/path/to/myservice.sh Restart=always User=root [Install] WantedBy=multi-user.target ``` 在这个文件中,我们定义了一个名为My Custom Service系统服务,它的启动命令为/path/to/myservice.sh,并且在失败时会自动重启。接下来,我们将这个文件保存为/etc/systemd/system/myservice.service,并使用以下命令启动服务: ```bash sudo systemctl daemon-reload sudo systemctl enable myservice sudo systemctl start myservice ``` 现在,当系统启动时,自定义服务和界面程序都将自动启动。

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值