zoukankan      html  css  js  c++  java
  • Linux添加自启动daemon service

    需求

    项目要编写一个程序,要求随工控机上电启动,并能在崩溃后自动重启程序。

    实现

    涉及到daemon的概念,个人倾向于用service实现。
    脚本需要root权限,修改ExecStart=/bin/sleep 999为对应业务程序地址
    服务名称xxx可自定义

    #!/bin/bash
    file=/etc/systemd/system/xxx.service
    
    if [ $UID -gt 0 ];then
    echo "Please run with root!"
    exit 0
    fi
    
    # creat a system service
    rm -rf $file
    cat <<EOF >>$file
    [Unit]
    Description=xxx daemon service
    Wants=network-online.target
    After=network-online.target
    
    [Service]
    Type=simple
    ExecStart=/bin/sleep 999
    ExecReload=/bin/kill -HUP $MAINPID
    Restart=always
    
    [Install]
    WantedBy=multi-user.target
    EOF
    
    # launch
    chmod +x $file
    systemctl enable xxx
    systemctl start xxx
    

    附加

    可在service栏目中定义用户进行权限约束(默认为root)
    配置自定义重启间隔,启动目录和环境变量

    [Service]
    User=ubuntu
    RestartSec=5s
    WorkingDirectory=/home/ubuntu/xxx
    Environment=USER=ubuntu HOME=/home/ubuntu
    
  • 相关阅读:
    杭电1827
    hdu 3118
    poj 2060
    hdu 2236
    poj 2226
    poj 1719
    poj 1466
    poj 3160
    骑士飞行棋笔记
    基础测试学习笔记
  • 原文地址:https://www.cnblogs.com/azureology/p/14229912.html
Copyright © 2011-2022 走看看