网上参考Ubuntu18.04 开机自启动脚本之后并没成功,各种搜索后发现博文内容都一样,说什么亲测可行,但根本没解决下面这个问题

~$ sudo systemctl status rc-local.service
● rc-local.service - /etc/rc.local Compatibility
   Loaded: loaded (/etc/systemd/system/rc-local.service; enabled; vendor preset:
  Drop-In: /lib/systemd/system/rc-local.service.d
           └─debian.conf
   Active: inactive (dead)
Condition: start condition failed at Wed 2019-08-14 12:21:45 CST; 5min ago
           └─ ConditionPathExists=/etc/rc.local  #脚本文件位置 was not met

然后发现这是Ubuntu20.04的方案。18.04不能这么干。(只能说CSDN都不亲测的,而且都是抄袭,千篇一律)

下面开始说解决方法。


编辑rc.local.service并添加[Install]内容:(注意与链接文的不同,一是文件位置,二是Install添加了两行内容

$ sudo vi /lib/systemd/system/rc.local.service

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

如果你之前是在/etc/systemd/system/rc-local.service下,请先rm掉

$ sudo rm /etc/systemd/system/rc-local.service

启用服务

$ sudo systemctl enable rc.local.service
$ sudo systemctl status rc-local.service

创建rc.local

$ sudo touch /etc/rc.local
$ sudo chmod 755 /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

我想开机自动挂载硬盘(以前写的Ubuntu18.04硬盘格式化、挂载、开机自动挂载在大多数ubuntu18.04上都可以,不知道什么原因在一台dell服务器上会有问题,所以采用开机自启脚本方法设置开机自动挂载)

所以在exit 0前添加如下内容

echo yourpassword | sudo mount /dev/yourdisk /path/to/mount

重启电脑,检查你的脚本是否执行了。

$ reboot
$ cat /usr/local/test.txt
$ sudo df -h

 

Ubuntu18.04设置开机自启动脚本
标签: