• [Linux]开机自启


    1 方式1:systemctl

    • 开启开机自启服务
      • systemctl enable <服务名>
    • 关闭开机自启服务
      • systemctl disable <服务名>
    • 查看开机自启服务状态
      • systemctl list-unit-files | grep <服务名>
    #[demos]
    systemctl enable firewall
    systemctl disable firewall.service
    
    systemctl disable sshd
    systemctl enable sshd
    systemctl list-unit-files | grep sshd
    

    2 方式2:开机自启脚本

    2.1 开机自启脚本编写与配置

    以消息队列RabbitMQ为例

    • 编写start_rabbitmq.sh
    vim /usr/local/rabbitmq/sbin/start_rabbitmq.sh
    
    [编写自启内容]
    #!/bin/bash
    
    #erlang
    export PATH=$PATH:/usr/rabbitmq/erlang/bin
    
    #rabbitmq
    export HOME=/usr/rabbitmq/rabbitmq/
    
    /usr/rabbitmq/rabbitmq/sbin/./rabbitmq-server -detached
    

    -detached:以后台守护进程方式运行

    • 给脚本增加可执行权限
    chmod +x /usr/local/rabbitmq/sbin/start_rabbitmq.sh
    
    • 编辑开机自启的系统文件
    vim /etc/rc.d/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
    #↓我们编写的启动脚本
    /usr/local/rabbitmq/sbin/start_rabbitmq.sh
    
    • 给系统脚本添加执行权限
    chmod +x /etc/rc.d/rc.local
    
    • 重启,并查看是否生效
    shutdown -r now
    

    2.2 开机自启失效的失效/故障解决方法

    • 失效错误1:"/bin/bash^M: 坏的解释器: 没有那个文件或目录"
      在Linux下编译shell运行脚本的时候出现”/bin/bash^M: 坏的解释器: 没有那个文件或目录“这样的错误如下图。
    [解决办法]
    使用在终端输入: sed -i 's/
    $//' my-johnnyzen-shell.sh即可
    
    [原因分析]
    此shell脚本文件在Windows 下编辑过,在Windows下每一行结尾是
    
    ,而Linux下则是
    
    sed -i 's/
    $//' make-all-linux-project.sh 会把make-all-linux-project.sh中的行尾的
    替换为空白
    

    3 参考文档

  • 相关阅读:
    常用公式 距离、波形、力
    代码字体
    关于flash缩放的详细解释
    色调
    工程项目1
    使用double无法得到数学上的精确结果的原因及为何不能用double来初始化BigDecimal
    第一次测验感受
    原码,补码,反码的概念及Java中使用那种存储方式
    static的含义
    第一次测试代码
  • 原文地址:https://www.cnblogs.com/johnnyzen/p/13121071.html
Copyright © 2020-2023  润新知