ubuntu18.04开机自启脚本的实现

转载链接:https://www.cnblogs.com/defifind/p/9285456.html

正文:

操作总结:如下
vi /lib/systemd/system/rc-local.service
# 文件结尾添加如下内容: --------------------------------------------------------
[Install]  
WantedBy=multi-user.target  
Alias=rc-local.service
# 添加完毕 --------------------------------------------------------------
sudo touch /etc/rc.local 
sudo chmod +x  /etc/rc.local
vi /etc/rc.local
# 添加如下内容 ----------------------------------------------------------
#!/bin/sh -e
# rc.local
# 注: 此处为需要添加的命令,在 exit 0 之前 
# 若添加了文件形式执行,需执行的文件必须有哈。不然会启动失败。
# eg:
# echo "hello world" >> /root/123abc.log
# /bin/sh /root/123abc.sh
exit 0
# 添加完毕 ------------------------------------------------------------
# 软连接
ln -s /lib/systemd/system/rc-local.service /etc/systemd/system/
# 验证
sudo systemctl start rc-local.service
sudo systemctl status rc-local.service
操作详解:如下

ubuntu-16.10 开始不再使用initd管理系统,改用systemd,现在系统 默认去 /etc/systemd/system 读取开机启动的脚本。
systemd 也包括用 systemctl 命令来替换 service 和 chkconfig 功能

sudo service mysql start     # 以前启动服务
### 升级后
sudo systemctl start mysqld.service  # 现在启动服务

systemd 默认读取 /etc/systemd/system 下的配置文件,而该目录下的文件会链接 /lib/systemd/system/ 下的文件。

故我们需要做的是 修改 /lib/systemd/system/ 下的启动脚本,并链接到 /etc/systemd/system 目录下
1.1 执行 ls /lib/systemd/system 你可以看到有很多启动脚本,其中就有我们需要的 rc.local.service

打开脚本内容

#  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
ConditionFileIsExecutable=/etc/rc.local
After=network.target

[Service]
Type=forking
ExecStart=/etc/rc.local start
TimeoutSec=0
RemainAfterExit=yes

一般正常的启动文件主要分成三部分

[Unit] 段: 启动顺序与依赖关系
[Service] 段: 启动行为,如何启动,启动类型
[Install] 段: 定义如何安装这个配置文件,即怎样做到开机启动

可以看出 re.local 文件没有install 段落,故需要手动加上。

[Install]  
WantedBy=multi-user.target  
Alias=rc-local.service
1.2 然后手动创建 rc.local 文件
sudo touch /etc/rc.local 
sudo chmod +x  /etc/rc.local

写入内容

#!/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.

# 注: 此处为需要添加的命令,在 exit 0 之前 
# 若添加了文件形式执行,需执行的文件必须有哈。不然会启动失败。
# eg:
# echo "hello world" >> /root/123abc.log
# /bin/sh /root/123abc.sh
exit 0
2 最后,因为 systemd 默认读取 /etc/systemd/system 下的配置文件, 所以还需要在 /etc/systemd/system 目录下创建软链接。
ln -s /lib/systemd/system/rc.local.service /etc/systemd/system/ 

#### 也可以尝试下创建超链接的语句:sudo systemctl enable rc-local (未验证)
3 验证

到这里添加开机启动已完成了,如果想不重启,直接查看效果执行如下命令即可;

启动服务并检查状态

sudo systemctl start rc-local.service
sudo systemctl status rc-local.service
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值