Linux收藏

Ubuntu 设置开机脚本

2022-03-12  本文已影响0人  承诺一时的华丽

1. 编辑 rc-local.service 文件

sudo chmod 777 /lib/systemd/system/rc-local.service
sudo gedit /lib/systemd/system/rc-local.service

[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 文件

sudo gedit /etc/rc.local
#!/bin/sh
echo "看到这行字,说明添加自启动脚本成功。" > /usr/local/test.log
# 中间这一段就是脚本的内容,例如:sudo ssr start
exit 0
#!/bin/sh
echo "看到这行字,说明添加自启动脚本成功。" > /usr/local/test.log
su - xxxx -c "/home/xxxx/service/autostart.sh"
su - xxxx2 -c "/home/xxxx2/website/autostart.sh"
exit 0
sudo chmod +x /etc/rc.local

3.、创建软链接

/etc/systemd/system/目录下创建软链接。创建软链接类似于windows下创建快捷方式。

4、创建方式:ln -s 原目录 映射目录

sudo ln -s /lib/systemd/system/rc-local.service /etc/systemd/system/ 

5、自定义用户脚本

gedit autostart.sh
#!/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 &

上一篇 下一篇

猜你喜欢

热点阅读