ubuntu20.04设置开机自启动

参考:

https://blog.csdn.net/qq_43685040/article/details/111574332

推荐方法2

一. Ubuntu20.04 设置开机自启

方法1:

具体步骤:{

查看系统中的自动启动脚本

修改 rc-local.service 文件的权限

修改 rc-local.service 文件

修改 /etc/rc.local 文件

创建软链接

重启Ubuntu后,去 /usr/local 下看看有没有生成test.log这个文件以及这个文件的内容。

其他说明

参考

1. 查看系统中的自动启动脚本

ls /lib/systemd/system  可以看到有 rc-local.service 这个文件
ls /lib/systemd/system/rc-local.service
2. 修改 rc-local.service 文件的权限

sudo chmod 777 /lib/systemd/system/rc-local.service

 文件: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

保存,退出。

2.    修改 /etc/rc.local 文件

       查看系统中有无 /etc/rc.local 这个文件,没有则自己创建一个。

       写入以下内容(清空文件中原有所有内容):

#!/bin/sh
echo "看到这行字,说明添加自启动脚本成功。" > /usr/local/test.log
exit 0

赋予权限

sudo chmod +x /etc/rc.local

5. 创建软链接

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

shutdown -r now 重启linux

查看这个文件的内容。

cat /usr/local/test.log

看到这行字,说明添加自启动脚本成功

 此时开机自启动成功!

参考centos7配置:

/etc/rc.local

#!/bin/bash
# THIS FILE IS ADDED FOR COMPATIBILITY PURPOSES
#
# It is highly advisable to create own systemd services or udev rules
# to run scripts during boot instead of using this file.
#
# In contrast to previous versions due to parallel execution during boot
# this script will NOT be run after all other services.
#
# Please note that you must run 'chmod +x /etc/rc.d/rc.local' to ensure
# that this script will be executed during boot.

touch /var/lock/subsys/local


方法2

建立托管文件采用systemctl的方式启动

1. 列出所有启动项:

sudo systemctl list-unit-files

vendor preset:表示服务默认的启动状态
STATE:表示服务当前的启动状态,有以下几种情况:

启动状态	含义
inactive	服务关闭
disable	服务开机不启动
enabled	服务开机启动
static	服务开机启动项被管理
failed	服务配置错误

使用grep过滤一下开启的grep enabled

sudo systemctl list-unit-files | grep enabled


查看.service文件的路径,及内容

sudo systemctl status openresty.service

禁用开机启动:

sudo systemctl disable nginx.service

停止已经开启的服务:

sudo systemctl stop nginx.service

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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值