1. 編輯 rc-local.service 文件
sudo chmod 777 /lib/systemd/system/rc-local.service
sudo gedit /lib/systemd/system/rc-local.service
- 在
rc-local.service
文件尾部添加以下內(nèi)容:
[Install]
WantedBy=multi-user.target
Alias=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
Alias=rc-local.service
2、編輯 rc.local 文件
- 編輯
rc.local
文件并添加要開機(jī)執(zhí)行的腳本米间,第一行#!/bin/sh
,尾行exit 0
麸恍。(ubuntu18.04
版本之后默認(rèn)沒有/etc/rc.local
文件,需要自己創(chuàng)建) - 添加下列內(nèi)容:
sudo gedit /etc/rc.local
- 內(nèi)容
#!/bin/sh
echo "看到這行字欢伏,說明添加自啟動(dòng)腳本成功匿垄。" > /usr/local/test.log
# 中間這一段就是腳本的內(nèi)容讽营,例如:sudo ssr start
exit 0
- 自定義啟動(dòng)相關(guān)腳本 虎忌, 通過
su - xxxx
執(zhí)行指定不同用戶權(quán)限的腳本
#!/bin/sh
echo "看到這行字,說明添加自啟動(dòng)腳本成功橱鹏。" > /usr/local/test.log
su - xxxx -c "/home/xxxx/service/autostart.sh"
su - xxxx2 -c "/home/xxxx2/website/autostart.sh"
exit 0
- 給 rc.local文件加上權(quán)限膜蠢。
sudo chmod +x /etc/rc.local
3.、創(chuàng)建軟鏈接
在/etc/systemd/system/
目錄下創(chuàng)建軟鏈接莉兰。創(chuàng)建軟鏈接類似于windows
下創(chuàng)建快捷方式挑围。
4、創(chuàng)建方式:ln -s 原目錄 映射目錄
sudo ln -s /lib/systemd/system/rc-local.service /etc/systemd/system/
5糖荒、自定義用戶腳本
gedit autostart.sh
- 腳本內(nèi)容
#!/bin/bash
# source /home/xxxx/anaconda3/bin/activate # archlinux 激活方式一
source /home/xxxx/anaconda3/etc/profile.d/conda.sh # ubuntu 激活方式二
cd /home/xxxx/service
conda activate xxxx
nohup python main.py &