Ubuntu18.04配置rc.local自启脚本方法记录

修改配置文件

打开位于/lib/systemd/system/目录下的rc-local.service文件,有默认内容如下:

#  SPDX-License-Identifier: LGPL-2.1+
#
#  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
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

创建软链接

Ubuntu18.04在启动时会读取/etc/systemd/system目录下的配置文件,因此在修改rc-local.service后,需要在/etc/systemd/system目录下创建软链接指向配置文件,执行的命令如下:

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

创建自启脚本

/etc/目录下新建文件rc.local同时修改权限,命令如下:

sudo touch /etc/rc.local
sudo chmod 755 /etc/rc.local

键入要执行的自启命令,系统在开机时会以root用户执行此脚本,记得在脚本的开头添加#!/bin/bash,示例文件如下:

#!/bin/bash
echo "hello world!" > /var/test.log

重启后可查看命令是否执行,以确认自启脚本配置成功。

  • 1
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 1
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值