02-好用的Linux技巧

若你发现有超好用技巧,欢迎推荐,随时更新!戳我发邮件📫

开机启动软件&执行命令

以下教程针对Ubuntu18.04

1、备份rc.local.service并添加配置

sudo cp /lib/systemd/system/rc.local.service /lib/systemd/system/rc.local.service.backup

sudo sh -c 'echo "[Install]\nWantedBy=multi-user.target\nAlias=rc-local.service\n" >> /lib/systemd/system/rc.local.service'

2、创建/etc/rc.local文件

sudo touch /etc/rc.local 

3、将要执行的命令添加到/etc/rc.local中即可

要执行的内容都写在exit 0之前

#!/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.
echo "看到这行字,说明添加自启动脚本成功。" > /usr/local/test.log

exit 0

4、给rc.local加上权限

sudo chmod +x /etc/rc.local

5、创建软连接

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

6、启动服务并检查状态

sudo systemctl enable rc.local
sudo systemctl start rc.local.service
sudo systemctl status rc.local.service