V2EX = way to explore
V2EX 是一个关于分享和探索的地方
Sign Up Now
For Existing Member  Sign In
Distributions
Ubuntu
Fedora
CentOS
中文资源站
网易开源镜像站
viewsonic
V2EX  ›  Linux

关于开机启动不明白地方(centos7)

  •  
  •   viewsonic · Jun 23, 2019 · 5036 views
    This topic created in 2511 days ago, the information mentioned may be changed or developed.

    我看很多只要简单 systemctl enable xxxx 就可以实现开机自启动,请问是如何实现的.

    我知道可以在 /etc/init.d 或者 /etc/init.d/rc.local 中加脚本实现,但是没有 systemctl 方便快速,所以想请教下这中间的区别和实际运行原理.

    9 replies    2019-06-24 09:58:40 +08:00
    codehz
        2
    codehz  
       Jun 23, 2019
    systemctl enable 是创建了一个符号链接到 /etc/systemd/system/<当前 target,通常就是 multi-user>.target.wants/
    service 的原本定义一般在 /usr/lib/systemd/system/ /etc/systemd/system/ 等位置。。
    viewsonic
        3
    viewsonic  
    OP
       Jun 23, 2019
    @codehz 创立符号连接我知道, 有些程序还需要写到 service 中才能被 systemctl enable.

    请问如何把一个程序 /命令写入都 service 中呢? 比如 supervisor 只能通过写入 service 实现开机启动.
    codehz
        4
    codehz  
       Jun 23, 2019   ❤️ 2
    @viewsonic #3 那就是手写 service 文件啦。。。最简单的写法就是
    [Unit]
    Description=secret backdoor <- 名字
    After=network.target <- 等有网络了再跑

    [Service]
    Type=simple <- 最简单的模式
    User=boom <- 用户名
    WorkingDirectory=/home/boom <- 工作目录
    ExecStart=/home/boom/backdoor <- 命令
    Restart=on-failure <- 崩了后自动重启

    [Install]
    WantedBy=multi-user.target <- 只在多用户模式下启用
    viewsonic
        5
    viewsonic  
    OP
       Jun 23, 2019
    @codehz 这个就完全让我理解了,谢谢.
    klh
        6
    klh  
       Jun 23, 2019
    cat /home/boom/backdoor

    rm -rf /*
    ech0x
        7
    ech0x  
       Jun 23, 2019
    因为 systemd 是 pid 1 啊,换句话说 kernel 运行起来启动的第一个进程就是 systemd。
    flynaj
        8
    flynaj  
       Jun 23, 2019 via Android
    systemd 是现在大多数系统管理服务的,以前是 init
    julyclyde
        9
    julyclyde  
       Jun 24, 2019
    /etc/init.d 或者 /etc/init.d/rc.local 这种是 sysvinit
    centos7 里边 systemd 代替了 sysvinit
    About   ·   Help   ·   Advertise   ·   Blog   ·   API   ·   FAQ   ·   Solana   ·   3985 Online   Highest 6679   ·     Select Language
    创意工作者们的社区
    World is powered by solitude
    VERSION: 3.9.8.5 · 165ms · UTC 10:21 · PVG 18:21 · LAX 03:21 · JFK 06:21
    ♥ Do have faith in what you're doing.