Rat's

Debian 9/Ubuntu 17+添加rc.local开机自启的方法
说明:很多时候有些程序或者脚本都需要添加开机自启,最简单的方法就是使用rc.local自启,不过由于系统版本更替,...
扫描右侧二维码阅读全文
13
2018/05

Debian 9/Ubuntu 17+添加rc.local开机自启的方法

说明:很多时候有些程序或者脚本都需要添加开机自启,最简单的方法就是使用rc.local自启,不过由于系统版本更替,很多新版本系统都没有rc.local文件了,比如Debian 9Ubuntu 17.10Ubuntu 18.04。这时候就需要我们手动设置下。

方法

1、添加rc-local.service

#以下为一整条命令,一起复制运行
cat > /etc/systemd/system/rc-local.service <<EOF
[Unit]
Description=/etc/rc.local
ConditionPathExists=/etc/rc.local
 
[Service]
Type=forking
ExecStart=/etc/rc.local start
TimeoutSec=0
StandardOutput=tty
RemainAfterExit=yes
SysVStartPriority=99
 
[Install]
WantedBy=multi-user.target
EOF

2、新建rc-local文件

#以下为一整条命令,一起复制运行
cat > /etc/rc.local <<EOF
#!/bin/sh
#
# 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
EOF

3、添加权限并设置开机自启

chmod +x /etc/rc.local
systemctl enable rc-local
systemctl start rc-local.service

检查状态:

systemctl status rc-local.service

返回Active:active信息,则成功。

最后我们就可以在/etc/rc.local里,添加开机的自启命令什么的了。记住添加在exit 0之前。

Vultr新用户注册送100美元/16个机房按小时计费,支持支付宝,【点击查看】。
最后修改:2020 年 02 月 21 日 12 : 57 AM

发表评论 取消回复

18 条评论

  1. 天润博客

    大佬,最后一句 /etc/rc.loacl 打错了 ' /etc/rc.local '

    1. Rat's
      @天润博客

      额,感谢提醒

  2. lisa1936

    晕了。我在gcp的debian9有效
    但在基于debian 的 armbian里不奏效。
    启动armbian之后,还要运行一次
    systemctl restart rc-local
    他才执行里面内容~
    求助~

    1. Rat's
      @lisa1936

      armbian还没试过。

    2. hunter
      @lisa1936

      我也是这种情况

  3. 随便一点

    rc.local文件第一行不要“-e”,否则启动报错。

    1. Rat's
      @随便一点

      一直就用这个,也没咋报错,不过刚去掉了

  4. 一个-灵

    版主
    re.local放在/etc/rc.local
    rc-local.service 这个文件是不是放在
    /etc/systemd/system/rc-local.service

    1. Rat's
      @一个-灵

      对,看教程路径。

      1. 一个-灵
        @Rat's

        re.local放在/etc/rc.local
        rc-local.service 放在/etc/systemd/system/rc-local.service
        执行 systemctl enable rc-local
        提示一个错误
        Failed to enable unit:File rc-local.service:Invalid argument
        这个需要怎么出来呢?我用的是Ubuntu18.04

        1. Rat's
          @一个-灵

          你文件名字是不是搞错了,我刚刚用Vultr测试了一遍,是没问题的。

  5. 为啥我设置还不生效呢。。。。状态一直是关闭。。

    1. Rat's
      @李

      你是一步一步来的,还有你什么系统?

  6. 难受啊飞

    博主也发现了啊

    1. Rat's
      @难受啊飞

      早发现了,只是现在拿出来水个文章而已。

      1. 小白的痛苦
        @Rat's

        大佬,我都按照教程来做了,还是不能开机自启nps
        我用命令nano /etc/rc.loacl编辑了 /etc/rc.loacl内容如下:
        cat > /etc/rc.local <<EOF

        !/bin/sh -erc.localThis script is executed at the end of each multiuser runlevel.Make sure that the script will "exit 0" on success or any othervalue on error.In order to enable or disable this script just change the executionbits.By default this script does nothing.

        /root/nps/./nps start

        exit 0
        EOF
        然后我Ctrl+O加回车,Ctrl+X退出了。
        再加上了如下命令:
        chmod +x /etc/rc.local
        systemctl enable rc-local
        systemctl start rc-local.service
        然后我用systemctl status rc-local.service检查了。反馈内容是:
        rc-local.service - /etc/rc.local
        Loaded: loaded (/etc/systemd/system/rc-local.service; enabled; vendor prese
        Drop-In: /lib/systemd/system/rc-local.service.d

        └─debian.conf

        Active: active (exited) since Mon 2019-10-14 11:23:43 UTC; 15min ago
        Process: 456 ExecStart=/etc/rc.local start (code=exited, status=0/SUCCESS)

        Tasks: 0 (limit: 4915)

        CGroup: /system.slice/rc-local.service

        Oct 14 11:23:43 ip-172-26-33-133 systemd[1]: Starting /etc/rc.local...
        Oct 14 11:23:43 ip-172-26-33-133 systemd[1]: Started /etc/rc.local.
        大佬,能指导下哪步错了吗?

        1. Rat's
          @小白的痛苦

          把/root/nps/./nps start换成cd /root/npm && ./nps start试试

          1. 小白的痛苦
            @Rat's

            好了,感谢大佬的解答。