Official | HULFT IoT EdgeStreaming First Step Guide Fourth Edition: July 1, 2021

Create a service from a script by using systemd

To create a service from a script by using systemd, you must describe a system unit file.

 

  1. Create the startup script "run.sh" and place it in a directory at the same level as "es-agent."

    The content of the startup script is as follows:

    #!/bin/sh
    
    BIN_DIR=/opt/edge
    COMMAND=${BIN_DIR}/es-agent
    
    cd $BIN_DIR
    exec $COMMAND run -c ${BIN_DIR}/es-agent.yaml
    
    exit 0
    

     

  2. Allow run.sh to be executed.

    $ chmod +x run.sh

     

  3. Modify the unit file "es-agent.service."

    The content of the file is as follows:

    [Unit]
    Description=es-agent
    ConditionPathExists=/opt/edge
    
    [Service]
    Type=simple
    ExecStart=/opt/edge/run.sh
    ExecStop=/bin/kill -TERM ${MAINPID}
    Restart=always
    
    [Install]
    WantedBy=multi-user.target
    

     

  4. Place the unit file "es-agent.service" in the "/etc/systemd/system" directory.

    # cp es-agent.service /etc/systemd/system
    

     

  5. Notify systemd that the unit file has been added or modified.

    # systemctl daemon-reload
    
    # systemctl status es-agent.service
    es-agent.service - es-agent
    Loaded: loaded (/etc/systemd/system/es-agent.service; disabled)
    Active: inactive (dead)
    

     

  6. Configure settings for automatic startup of es-agent.

    # systemctl enable es-agent.service
    ln -s '/etc/systemd/system/es-agent.service' '/etc/systemd/system/multi-user.target.wants/es-agent.service'
     
    # systemctl status es-agent.service
    es-agent.service - es-agent
    Loaded: loaded (/etc/systemd/system/es-agent.service; enabled)
    Active: inactive (dead)
    

    es-agent is now set to start up automatically when the IoT gateway OS starts. Confirm once more that the settings have been configured correctly.

    Start the service with "systemctl start <service name>" and confirm the status and content of standard output with "systemctl status <service name>".

    If an error is not output at this time, the service has been created from the script correctly, and has been started.

    # systemctl start es-agent.service
    
    # systemctl status es-agent.service
    ● es-agent.service - es-agent
       Loaded: loaded (/etc/systemd/system/es-agent.service; enabled)
       Active: active (running) since Tue 2020-02-25 06:00:39 CET; 7s ago
      Process: 1577 ExecStop=/bin/kill -TERM ${MAINPID} (code=exited, status=0/SUCCESS)
     Main PID: 1661 (es-agent)
       CGroup: /system.slice/es-agent.service
               └─1661 /opt/edge/es-agent run -c /opt/edge/es-agent.yaml
    
    Feb 25 06:00:39 debian run.sh[1661]: time="2020-02-25T05:00:39Z" level=info msg="no plugins to load" file=loade...script
    Feb 25 06:00:39 debian run.sh[1661]: time="2020-02-25T05:00:39Z" level=info msg="Starting the server on :15601"
    Feb 25 06:00:39 debian run.sh[1661]: {"cpu":99.99999999779732,"disk_avail":14.960063934326172}
    Feb 25 06:00:40 debian run.sh[1661]: {"cpu":0,"disk_avail":14.960063934326172}
    Feb 25 06:00:41 debian run.sh[1661]: {"cpu":0,"disk_avail":14.960063934326172}
    Feb 25 06:00:42 debian run.sh[1661]: {"cpu":0.9900990099009309,"disk_avail":14.960063934326172}
    Feb 25 06:00:43 debian run.sh[1661]: {"cpu":0,"disk_avail":14.960063934326172}
    Feb 25 06:00:44 debian run.sh[1661]: {"cpu":0,"disk_avail":14.960063934326172}
    Feb 25 06:00:45 debian run.sh[1661]: {"cpu":0,"disk_avail":14.960063934326172}
    Feb 25 06:00:46 debian run.sh[1661]: {"cpu":0,"disk_avail":14.960063934326172}
    

     

 

 

Official | HULFT IoT EdgeStreaming First Step Guide Fourth Edition: July 1, 2021