V2EX = way to explore
V2EX 是一个关于分享和探索的地方
Sign Up Now
For Existing Member  Sign In
jhytxy
V2EX  ›  问与答

为什么 debian 这么恶心不给用 rc.local 了

  •  
  •   jhytxy · Dec 3, 2018 · 9221 views
    This topic created in 2714 days ago, the information mentioned may be changed or developed.
    现在设置开机启动要怎么做?

    网上搜了几个启用 rc.local 的教程都失败了

    求问大佬现在 debian 设置开机启动的正确姿势是什么

    以及如何找回熟悉的 rc.local
    40 replies    2018-12-05 11:26:40 +08:00
    akiakiseofficial
        1
    akiakiseofficial  
       Dec 3, 2018 via iPhone
    这....楼下教吧
    trait
        2
    trait  
       Dec 3, 2018 via iPhone
    systemd ?
    ETiV
        3
    ETiV  
       Dec 3, 2018
    crontab @reboot

    了解一下
    jhytxy
        4
    jhytxy  
    OP
       Dec 3, 2018
    https://www.moerats.com/archives/623/

    这个做完以后 rc-local 服务是可以启动了
    但是重启一遍又没了
    hjc4869
        5
    hjc4869  
       Dec 3, 2018
    sudo apt install initscripts
    trys1
        6
    trys1  
       Dec 3, 2018 via Android   ❤️ 1
    请用 systemctl
    hjc4869
        7
    hjc4869  
       Dec 3, 2018
    Sorry 查错了。无视 #5

    Debian 的 systemd 有 rc-local.service,应该放一个 /etc/rc.local 之后就能用?
    jhytxy
        8
    jhytxy  
    OP
       Dec 3, 2018
    @hjc4869 systemctl status rc-local.service
    ● rc-local.service - /etc/rc.local
    Loaded: loaded (/etc/systemd/system/rc-local.service; enabled)
    Active: failed (Result: exit-code) since Mon 2018-12-03 06:30:51 EST; 16min ago
    Process: 223 ExecStart=/etc/rc.local start (code=exited, status=127)

    Dec 03 06:30:51 107-172-77-132-host systemd[1]: Starting /etc/rc.local...
    Dec 03 06:30:51 107-172-77-132-host systemd[1]: rc-local.service: control pr...7
    Dec 03 06:30:51 107-172-77-132-host systemd[1]: Failed to start /etc/rc.local.
    Dec 03 06:30:51 107-172-77-132-host systemd[1]: Unit rc-local.service entere....
    Hint: Some lines were ellipsized, use -l to show in full.
    ooh
        10
    ooh  
       Dec 3, 2018
    @ooh 补充说明:如果发现执行成功,操作却没有执行可以 服务内的脚本 需要 sleep 一会儿
    dot2017
        11
    dot2017  
       Dec 3, 2018
    做成 service
    WordTian
        12
    WordTian  
       Dec 3, 2018 via Android   ❤️ 2
    systemctl enable rc-local #设置 rc-local 开机启动
    systemctl start rc-local #启动 rc-local

    然后在 /etc/rc.local 文件中加命令就完了
    jhytxy
        13
    jhytxy  
    OP
       Dec 3, 2018
    笑话我的人有试过吗

    每次都是 rc.local 打不开
    我用 root 登录的
    rc.local 里就一条 node /c9sdk/server.js -w ~/c9sdk/ -l 0.0.0.0 -a
    ZE3kr
        14
    ZE3kr  
       Dec 3, 2018 via iPhone
    有执行权限么
    jhytxy
        15
    jhytxy  
    OP
       Dec 3, 2018
    @ooh cat /usr/lib/systemd/system/rc.local.service
    cat: /usr/lib/systemd/system/rc.local.service: No such file or directory
    jhytxy
        16
    jhytxy  
    OP
       Dec 3, 2018
    @ZE3kr 有 设置完第一遍查看状态可以正常启动

    重启完就没了
    jhytxy
        17
    jhytxy  
    OP
       Dec 3, 2018
    踩了个大坑

    国产教程里的 /usr/lib/systemd/system 是不存在于 debian 9 的

    debian 9 在 /etc/systemd/system 里
    ooh
        18
    ooh  
       Dec 3, 2018
    hulk@test:~$ cat /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

    hulk@test:~$ cat /etc/rc.local

    #!/bin/bash
    sleep 10 && mount -t vboxsf workspace /srv
    exit 0

    hulk@test:~$ sudo systemctl status rc-local

    ● rc-local.service - /etc/rc.local Compatibility
    Loaded: loaded (/lib/systemd/system/rc-local.service; enabled; vendor preset: enabled)
    Drop-In: /lib/systemd/system/rc-local.service.d
    └─debian.conf
    Active: active (exited) since Mon 2018-12-03 12:40:38 UTC; 16min ago
    Docs: man:systemd-rc-local-generator(8)
    Process: 1001 ExecStart=/etc/rc.local start (code=exited, status=0/SUCCESS)

    Dec 03 12:40:28 test systemd[1]: Starting /etc/rc.local Compatibility...
    Dec 03 12:40:38 test systemd[1]: Started /etc/rc.local Compatibility.
    jhytxy
        19
    jhytxy  
    OP
       Dec 3, 2018
    @ooh 重启一遍呢
    ooh
        20
    ooh  
       Dec 3, 2018
    @jhytxy 程序不会骗人的,我一年至少也要重启十几次,你 rc.local 代码有问题,贴出来看看
    lemonda
        21
    lemonda  
       Dec 3, 2018
    你想开机运行 Cloud9 吧
    我本来想看下自己是怎么设置开机启动的然后告诉你,结果发现一直是手动运行的😓
    Vanquish5419
        22
    Vanquish5419  
       Dec 3, 2018 via iPhone
    Centos7 一样,用 systemd 吧,rc.local 不推荐使用了
    benjix
        23
    benjix  
       Dec 3, 2018
    确保你的 rc.local 文件没问题

    vim /lib/systemd/system/rc.local.service
    在最后添加
    [Install]
    WantedBy=multi-user.target

    然后执行
    systemctl daemon-reload
    systemctl enable rc.local
    systemctl start rc.local

    重启试试
    hcymk2
        24
    hcymk2  
       Dec 3, 2018
    又不发开机日志,又不发代码。
    caola
        25
    caola  
       Dec 3, 2018
    systemd
    FindHao
        26
    FindHao  
       Dec 3, 2018 via Android
    systemd 很舒服,可以看看阮一峰的教程
    wwqgtxx
        27
    wwqgtxx  
       Dec 4, 2018 via iPhone   ❤️ 1
    这个 service 文件一分钟就搞定的事,非要瞎折腾
    msg7086
        28
    msg7086  
       Dec 4, 2018
    都 8012 年了还在用着上古的不知道淘汰了多少年的 rc.local …… ermmm
    0ZXYDDu796nVCFxq
        29
    0ZXYDDu796nVCFxq  
       Dec 4, 2018 via Android
    rc.local 早该淘汰了
    Systemd is No.1
    Vicer
        30
    Vicer  
       Dec 4, 2018 via Android
    不是加个执行权限就行了?
    一般用 @reboot
    sagaxu
        31
    sagaxu  
       Dec 4, 2018 via Android   ❤️ 1
    自带的 rc.local 缺了个 wantedby,所以即使做了 enable,重启后也是不会执行的,自己加一个就好了
    designer
        32
    designer  
       Dec 4, 2018 via iPhone
    不要轻易判断一个 OS 是不是恶心。就算不顺手可以选择不用
    trafficMGR
        33
    trafficMGR  
       Dec 4, 2018 via Android
    所以这个问题跟 Debian 有什么关系吗
    silvernoo
        34
    silvernoo  
       Dec 4, 2018 via Android
    我已经离不开 systemd 了
    1daydayde
        35
    1daydayde  
       Dec 4, 2018 via iPhone
    @silvernoo +10086
    Sharuru
        36
    Sharuru  
       Dec 4, 2018 via Android
    你 V 是这样的,越菜的越觉得这恶心,那恶心的,噗哧。
    fstab
        37
    fstab  
       Dec 4, 2018 via Android
    从学用 linux,vps 一直都用 debian,直到现在大概有几年了,没遇到过什么坑。
    scorn
        38
    scorn  
       Dec 4, 2018 via Android
    我自己建一个 rc.local 解决
    xuanaux
        39
    xuanaux  
       Dec 4, 2018
    我用的是 Ubuntu,当初直接创建 rc.local 文件并富裕执行权限就可以正常使用,你看看是不是日常.local 哪里写错了。
    最后问下 systemd 有类似于 rc.local 这种方便的功能么?总不能每个命令写一个 Server 文件吧。
    wwqgtxx
        40
    wwqgtxx  
       Dec 5, 2018   ❤️ 1
    @xuanaux systemd 的逻辑就是每个服务写一个 service 文件,这种东西写多了就是每次 cp 一份改一下名字,描述,命令,熟练了之后都能闭着眼睛敲出来了。
    好处是你可以很方便的管理你启动的每个命令,systemctl start\stop\status xxx.service 即可。
    至于想要实现 rc.local 的功能,你自己创建个 service 把 ExecStart 指向一个 shell 脚本不就得了,只不过注意 service 文件中的 type,具体的可以参见
    http://www.jinbuguo.com/systemd/systemd.service.html
    About   ·   Help   ·   Advertise   ·   Blog   ·   API   ·   FAQ   ·   Solana   ·   3051 Online   Highest 6679   ·     Select Language
    创意工作者们的社区
    World is powered by solitude
    VERSION: 3.9.8.5 · 85ms · UTC 07:53 · PVG 15:53 · LAX 00:53 · JFK 03:53
    ♥ Do have faith in what you're doing.