關(guān)于不同系統(tǒng)(ubuntu)下自啟動(dòng)服務(wù)的方式

ubuntu 20.04
ubuntu16.04 后版本不支持update-rc.d方式添加開機(jī)自啟腳本,只能通過systemctl 命令添加,以下記錄三種方式添加開機(jī)自啟;
方式一:
使用rc-local.service
rc-local.service是系統(tǒng)自帶的一個(gè)開機(jī)自啟服務(wù),20.04默認(rèn)關(guān)閉

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.創(chuàng)建/etc/rc.local
ubuntu20.04默認(rèn)不存在/etc/rc.local,需要自己創(chuàng)建,添加啟動(dòng)內(nèi)容,自定義自己需要開機(jī)實(shí)現(xiàn)的功能

#!/bin/bash

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

注意:關(guān)于開機(jī)自啟中涉及到的文件,都需要給可執(zhí)行權(quán)限;
3.啟動(dòng)rc-local.service
systemctl enable rc-local.service(自啟動(dòng))
systemctl start rc-local.service(啟動(dòng))
systemctl status rc-local.service(狀態(tài))

(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自帶的命令,可以在用戶登陸時(shí)自動(dòng)執(zhí)行某個(gè)程序,在終端輸入gnome-session-properties

添加一個(gè)新的啟動(dòng)程序,選中自己寫好的腳本,添加禁區(qū)保存就可以
image.png

方式三:自定義開機(jī)自啟服務(wù)

除了使用系統(tǒng)自帶的rc-local.service,可以自定義創(chuàng)建一些服務(wù)

1.創(chuàng)建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表示服務(wù)的依賴關(guān)系
ExecStart 表示要啟動(dòng)的程序或腳本
WantedBy 表示該服務(wù)所屬targe,multi-user.targe表示多用戶命令行狀態(tài)
2.啟動(dòng)服務(wù)
使用systemctl enable / start /lib/systemd/system/daemon.service啟動(dòng)服務(wù)

最后編輯于
?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請(qǐng)聯(lián)系作者
【社區(qū)內(nèi)容提示】社區(qū)部分內(nèi)容疑似由AI輔助生成,瀏覽時(shí)請(qǐng)結(jié)合常識(shí)與多方信息審慎甄別。
平臺(tái)聲明:文章內(nèi)容(如有圖片或視頻亦包括在內(nèi))由作者上傳并發(fā)布,文章內(nèi)容僅代表作者本人觀點(diǎn),簡(jiǎn)書系信息發(fā)布平臺(tái),僅提供信息存儲(chǔ)服務(wù)。

友情鏈接更多精彩內(nèi)容