服务器搭建(09)设置开机启动脚本(ubuntu18.04后)

1 ubuntu18.04后启动的原理变更

ubuntu-16.10 开始不再使用initd管理系统,改用systemd,systemd 就是用 systemctl 命令来替换了 service 和 chkconfig 的功能。

以前启动 XXX服务用命令:

sudo service XXX start

现在改用:

sudo systemctl start XXX.service

开机启动比以前复杂一些。systemd 默认是读取 /etc/systemd/system 下的配置文件,该目录下的文件会链接/lib/systemd/system/下的文件。执行 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

该配置文件主要分为3个部分:

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

可以看到,默认的配置文件中少了 Install 段,也就没有定义如何做到开机启动,所以显然这样配置是无效的。 因此我们的思路就是在后面帮他加上 [Install] 段。之后在rc.local中添加启动脚本相关信息,调试后重启 测试即可。

2 设置开机启动脚本流程

@1 添加install字段

默认配置文件为:

#  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

添加install字段,添加后,配置文件为:

[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 之后编辑文件/etc/rc.local (没有则创建)

把你需要启动脚本写入 /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.
echo "log test" > /usr/local/test.log
#添加需要开机启动的脚本命令集合
exit 0

之后 添加权限,如下所示:

sudo chmod +x /etc/rc.local

@3 创建软链接

 systemd 默认读取 /etc/systemd/system 下的配置文件, 所以还需要在 /etc/systemd/system 目录下创建软链接,如下所示:

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

@4 重启系统测试开机脚本是否生效

可以通过打印日志的方式,比如:

echo "test log..." > /usr/local/test.log

也可以直接看执行结果。

 

 

 

 

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

图王大胜

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值