Tag: systemd

  • Autostart icecast using systemd

    To autostart icecast using systemd, create a unit file

    vi /etc/systemd/system/icecast.service
    

    Add following content

    [Unit]
    Description=Icecast Network Audio Streaming Server
    After=network.target
    
    [Service]
    Type=simple
    ExecStart=/usr/local/bin/icecast -c /etc/icecast/icecast.xml
    ExecReload=/bin/kill -HUP $MAINPID
    User=icecast
    Group=icecast
    WorkingDirectory=/home/icecast/
    
    [Install]
    WantedBy=multi-user.target

    Enable icecast to start on boot with

    systemctl enable icecast

    To start/stop/restart icecast, use

    systemctl start icecast
    systemctl stop icecast
    systemctl restart icecast
  • Systemd Journal

    Systemd have its own loggin system called Systemd Journal. It keep track of logs for each service.

    To see log for a service, run

    journalctl -u SERVICE_NAME
    

    Example

    root@ocp-serverok-in:~# journalctl -u myapp
    -- Logs begin at Wed 2020-05-27 06:43:34 UTC, end at Fri 2020-06-26 09:33:19 UTC. --
    Jun 26 08:53:59 ocp-serverok-in systemd[1]: Started Sample web server.
    Jun 26 08:53:59 ocp-serverok-in cat[36147]: Starting web server
    root@ocp-serverok-in:~# 
    
  • Start an application using systemd

    systemd is used to start applications on linux systems.

    In this post, we will create an application and run start it on boot using systemd.

    Lets create our sample application.

    mkdir /root/myapp/
    vi /root/myapp/web.sh
    

    Add following content to the file and save.

    #!/bin/bash
    
    echo "Starting web server" | systemd-cat -p info
    
    cd /root/myapp
    python3 -m http.server 80
    

    You can start the application by running following command on terminal

    bash /root/myapp/web.sh
    

    this will run a simple web server on port 80. If you already have a web server running on port 80, change the port to another.

    To stop web server, type CTRL+C.

    Create Systemd service file

    To manage this application using systemd, we need to create a service file.

    vi /etc/systemd/system/myapp.service
    

    Add

    [Unit]
    Description=Sample web server
    
    [Service]
    Type=simple
    ExecStart=/bin/bash /root/myapp/web.sh
    
    [Install]
    WantedBy=multi-user.target
    

    systemd service file

    Managing Systemd service

    First you need to enable the service with

    systemctl enable myapp
    

    To start the service, run

    systemctl start myapp
    

    To stop the service, run

    systemctl stop myapp
    

    To see status of the service, run

    systemctl status myapp