日本免费高清视频-国产福利视频导航-黄色在线播放国产-天天操天天操天天操天天操|www.shdianci.com

學無先后,達者為師

網站首頁 編程語言 正文

systemd開機啟動和關機回調腳本

作者:小堅學Linux 更新時間: 2022-07-09 編程語言

一、開機執行一次的腳本
我們通過可以創建一個/etc/rc.local文件:
/etc/rc.local文件內容如下:

#!/bin/sh -e
#
# rc.local
#
# This script is executed at the end of each multiuser runlevel.
# Make sure that the script will "exit 0" on success or any other
# value on error.
#
# In order to enable or disable this script just change the execution
# bits.
#
# By default this script does nothing.

bash /opt/reboot.sh &

exit 0

這樣子我們的linux系統每次開機都會運行一次/opt/reboot.sh腳本。

一、開機自動運行的systemd服務
touch.service:

[Unit]
Description=touch service

[Service]
Type=oneshot

ExecStart=touch /jian
#ExecStop=rm /jian
PrivateTmp=true

[Install]
WantedBy=multi-user.target
Alias=touch.service

把這個文件放到/lib/systemd/system/目錄,然后執行命令使能它,讓他開機自動運行一次:

sudo systemctl enable touch.service

同時我們可以手動運行這個服務:

sudo systemctl start touch.service

二、關機執行systemd服務
rtc_load.service:

[Unit]
Description=set the RTC from the system time
Before=systemd-poweroff.service systemd-reboot.service systemd-halt.service
DefaultDependencies=no
 
[Service]
ExecStart=hwclock -w
Type=forking
 
[Install]
WantedBy=poweroff.target
WantedBy=reboot.target
WantedBy=halt.target

把這個文件放到/lib/systemd/system/目錄,然后使用以下命令創建幾個軟連接:

ln -s /lib/systemd/system/rtc_load.service /usr/lib/systemd/system/halt.target.wants/
ln -s /lib/systemd/system/rtc_load.service /usr/lib/systemd/system/poweroff.target.wants/
ln -s /lib/systemd/system/rtc_load.service /usr/lib/systemd/system/reboot.target.wants/

原文鏈接:https://blog.csdn.net/sinat_22338935/article/details/125596100

欄目分類
最近更新