Ubuntu开机自启动

  1. 有桌面环境的Ubuntu,可以比较方便的在Startup Applications 中启动(以user的环境)

编写auto_run脚本

#!/bin/bash
while true
do
python3 xxx.py
done

其中 while true 为自重启控制

2. 使用systemctl的方法

# vim /etc/systemd/system/***.service 
# /usr/lib/systemd/system/mount_smb.service
[Unit]
Description=Mount SMB Service
After=network.target zerotier-one.service remote-fs.target

[Service]
User=root
#Restart=always
RestartSec=1
ExecStartPre=/bin/sleep 10
ExecStart=/home/xxxx/Script/auto_mount_nas.sh
ExecStop=/home/xxxx/Script/auto_mount_nas.sh umount
Type=oneshot
RemainAfterExit=yes


[Install]
WantedBy=multi-user.target

After 和 Before为关键依赖项目,使用这个挂载时之前未添加依赖remote-fs.target,导致关机时remote-fs.target 先关闭,无法正常umount 远程文件夹导致关机时间延长值90s,并且无法通过修改默认关闭时间来修改关机时间

# systemctl daemon-reload

后面可以使用systemctl 命令控制启动,停止等

# systemctl status ***.service

来查看服务状态