zoukankan      html  css  js  c++  java
  • Centos 7 下自启动服务配置

    在服务器部署服务后,往往需要将服务设置成开机自启的状态 ,以防设备出现宕机或断电重启,服务无法访问的情况。

    对于常见的服务(httpd,mysqld,nginx)来说,可通过系统 systemctl enable 来完成该工作。但对于自己开发的 service,比如通过 docker,可不可以通过 systemctl enable 来运行呢,下面就是实现方案:

    Step1 - 创建启动文件

    vim auto_start_script.sh
    
    #!/bin/bash
    /usr/bin/docker-compose -f /home/user/docker-compose/docker-compose.yml  up -d
    

    Step2 - 配置权限

    # get file info
    ls -lrt auto_start_script.sh
    
    # add executable permissions
    chmod 744 auto_start_script.sh
    

    Step3 - 自定义系统服务

    # Add the service startup file
    vim /etc/systemd/system/ctg_docker.service
    
    [Unit]
    Description=running docker-compose at the system boot
    After=docker.service
    
    [Service]
    ExecStart=/home/user/docker-compose/config/auto_start_script.sh
    
    [Install]
    # 多用户登录或者带有 desktop 登录
    WantedBy=default.target
    
    # change permission
    chmod 644 /etc/systemd/system/ctg_docker.service
    

    Step4 - 测试服务

    # 加载服务
    systemctl daemon-reload
    
    # 启动服务,查看是否正常
    systemctl start ctg_docker.service
    
    # 添加开机启动
    systemctl enable ctg_docker.service
    
    # 重启系统
    reboot
    
    # 获取系统启动时间
    cat /proc/uptime| awk -F. '{run_days=$1 / 86400;run_hour=($1 % 86400)/3600;run_minute=($1 % 3600)/60;run_second=$1 % 60;printf("系统已运行:%d天%d时%d分%d秒",run_days,run_hour,run_minute,run_second)}'
    
    date -d "$(awk -F. '{print $1}' /proc/uptime) second ago" +"%Y-%m-%d %H:%M:%S"
    
    
  • 相关阅读:
    第十七讲:解释器模式
    第十六讲:适配器模式
    第十五讲:桥接模式
    第十四讲:组合模式
    第十三讲:外观模式
    第十二讲:代理模式
    第十一讲:享元模式
    第九讲:策略模式
    工厂模式
    观察者设计模式
  • 原文地址:https://www.cnblogs.com/michael9/p/12786693.html
Copyright © 2011-2022 走看看