美文网首页
关于不同系统(ubuntu)下自启动服务的方式

关于不同系统(ubuntu)下自启动服务的方式

作者: 酸奶泡奥利奥 | 来源:发表于2023-02-19 11:05 被阅读0次

ubuntu 20.04
ubuntu16.04 后版本不支持update-rc.d方式添加开机自启脚本,只能通过systemctl 命令添加,以下记录三种方式添加开机自启;
方式一:
使用rc-local.service
rc-local.service是系统自带的一个开机自启服务,20.04默认关闭

1.在终端下打开 rc-local.service文件

(base) root@zxm-29:/mnt/daemon# vim /lib/systemd/system/rc-local.service
#  SPDX-License-Identifier: LGPL-2.1+
#
#  This file is part of systemd.
#
#  systemd is free software; you can redistribute it and/or modify it
#  under the terms of the GNU Lesser General Public License as published by
#  the Free Software Foundation; either version 2.1 of the License, or
#  (at your option) any later version.

# This unit gets pulled automatically into multi-user.target by
# systemd-rc-local-generator if /etc/rc.local is executable.
[Unit]
Description=/etc/rc.local Compatibility
Documentation=man:systemd-rc-local-generator(8)
ConditionFileIsExecutable=/etc/rc.local
After=network.target

[Service]
Type=forking
ExecStart=/etc/rc.local start
TimeoutSec=0
RemainAfterExit=yes
GuessMainPID=no

[Install]
WantedBy=multi-user.target
(base) root@zxm-29:/mnt/daemon#

在最后添加Install段

2.创建/etc/rc.local
ubuntu20.04默认不存在/etc/rc.local,需要自己创建,添加启动内容,自定义自己需要开机实现的功能

#!/bin/bash

# start jar by zhouxiaomin
nohup /mnt/XHLD/daemon.sh > /daemon.log 2>&1 &

注意:关于开机自启中涉及到的文件,都需要给可执行权限;
3.启动rc-local.service
systemctl enable rc-local.service(自启动)
systemctl start rc-local.service(启动)
systemctl status rc-local.service(状态)

(base) root@zxm-29:/mnt/daemon# systemctl status rc-local.service
● rc-local.service - /etc/rc.local Compatibility
     Loaded: loaded (/lib/systemd/system/rc-local.service; enabled; vendor preset: enabled)
    Drop-In: /usr/lib/systemd/system/rc-local.service.d
             └─debian.conf
     Active: active (running) since Mon 2023-02-20 09:53:38 CST; 43min ago
       Docs: man:systemd-rc-local-generator(8)
    Process: 802435 ExecStart=/etc/rc.local start (code=exited, status=0/SUCCESS)
      Tasks: 85 (limit: 231629)
     Memory: 2.3G
     CGroup: /system.slice/rc-local.service
             ├─802436 /bin/sh /mnt/XHLD/daemon.sh
             ├─802441 java -jar -Duser.timezone=GMT+08 -Dfile.encoding=UTF-8 /mnt/XHLD/xinhuo-
             └─804504 sleep 15

2月 20 09:53:38 xhkj-29 systemd[1]: Starting /etc/rc.local Compatibility...
2月 20 09:53:38 xhkj-29 systemd[1]: Started /etc/rc.local Compatibility.
(base) root@zxm-29:/mnt/daemon# 

方式二:使用gnome-session-properties
这是Ubuntu自带的命令,可以在用户登陆时自动执行某个程序,在终端输入gnome-session-properties

添加一个新的启动程序,选中自己写好的脚本,添加禁区保存就可以 image.png

方式三:自定义开机自启服务

除了使用系统自带的rc-local.service,可以自定义创建一些服务

1.创建service文件,可以放到/lib/systemd/system/目录下,如果自定义目录,最好做好说明

daemon.service

[Unit]
Description=ZXM
After=network.target
[Service]
ExecStart=/mnt/daemon/daemon.sh
[Install]
WantedBy=multi-user.target

Description是service文件的描述,随便填写
After表示服务的依赖关系
ExecStart 表示要启动的程序或脚本
WantedBy 表示该服务所属targe,multi-user.targe表示多用户命令行状态
2.启动服务
使用systemctl enable / start /lib/systemd/system/daemon.service启动服务

相关文章

网友评论

      本文标题:关于不同系统(ubuntu)下自启动服务的方式

      本文链接:https://www.haomeiwen.com/subject/pgsikdtx.html