Skip to content

Software 2. Running as a Service

David Albrecht edited this page Apr 14, 2023 · 11 revisions

This documentation is for software version 0.2.0 and earlier. Click here to see this page in the latest docs.

This page shows how to setup the software as a service so that it will start automatically on boot, and restart itself in case any errors come up.

Ideally, you should do this when you are completely finished with the setup and installation of your power monitor.

Service File Setup

  1. Create a .service file in /etc/systemd/system/:

    sudo su
    cd /etc/systemd/system
    touch power-monitor.service
    
  2. Simply use nano to edit the file, like so:

    nano power-monitor.service

    Copy and paste the contents for the file below. To save a file with nano, press Ctrl-o, then Enter, then Ctrl-x.

    NOTE: If your username is not pi, you'll need to replace the User= and the ExecStart= directory in the contents of this file.

    power-monitor.service contents:

    [Unit]
    Description=Raspberry Pi Power Monitor
    
    [Service]
    Restart=always
    RestartSec=1
    StartLimitInterval=120
    StartLimitBurst=5
    User=pi
    ExecStart=/usr/bin/python3 /home/pi/rpi-power-monitor/power-monitor.py
    
    [Install]
    WantedBy=multi-user.target
    
  3. Enable the service with:

    systemctl enable power-monitor

  4. Run systemctl daemon-reload to update systemd with the new service.

  5. Reboot the system with:

    reboot 0

The entire application suite should start automatically now on boot. When the Pi comes back up, check the status of the power monitor software with the following command:

sudo systemctl status power-monitor

You can also confirm that the docker containers are running by issuing the docker ps command. Example:

pi@raspberrypi:~ $ docker ps
CONTAINER ID        IMAGE               COMMAND                  CREATED             STATUS              PORTS                    NAMES
51a0a348c111        grafana/grafana     "/run.sh"                8 days ago          Up 7 minutes        0.0.0.0:3000->3000/tcp   grafana
013caaf13240        influxdb            "/entrypoint.sh infl…"   8 days ago          Up 7 minutes        0.0.0.0:8086->8086/tcp   influx


Working with services

  • Start service: sudo systemctl start power-monitor

  • Stop service: sudo systemctl stop power-monitor

  • View status: sudo systemctl status power-monitor

  • Restart service: sudo systemctl restart power-monitor

Troubleshooting systemd

You can view the logs from the power-monitor software by using journalctl. Issue the following command:

sudo journalctl -b -n 100 -u power-monitor

NOTE: The -b flag shows the logs for the current boot. The -n 100 flag shows the last 100 lines of the log file. You can remove or adjust either of these flags to change the timeframe of the output you want to see.

You can also "tail" the logs (view them in real time) by adding the -f flag to the command above