How to get a systemd service running on boot with tmux

Viewed 181

I'm trying to have a systemd service run on boot on a raspberry pi. It is a bash script that checks a github repository and keeps a python script running and up to date. I have tried various configurations for the service file noticing that some network errors occurred but I can't seem to have it run as the tmux task doesn't start properly.

[Unit]
Description=Sensor API Service
Wants=network-online.target
After=network.target network-online.target

[Service]
Type=simple
Restart=always
ExecStart=/usr/bin/bash /home/pi/sensor/update.sh

[Install]
WantedBy=network-online.target

After boot tmux doesn't even run properly with the following error:

error connecting to /tmp//tmux-100/default (No such file or directory)

This is the bash script I'm trying to run.

#!/bin/bash

session="sensorInput"
workingDir="/home/pi/sensor"

if [ ! -d $workingDir ]; then
    mkdir $workingDir
    cd $workingDir
else
    cd $workingDir
fi

function UOI () {
    cd $workingDir
    git clone {githublink}
    shopt -s dotglob
    mv -u sensor/* ./
    rm -fr sensor
    git reset --hard
    git pull --force
    git checkout .
    pip3 install -r requirements.txt
}

function run () {
    git fetch
    if [ ! -f "app.py" ]; then
        echo "Python app not found, running update/install function."
        UOI
        echo "Finished installing the script."
        tmux new -d -s $session 'python3 app.py'
        echo "Script is now running in session \'sensorInput\'"
    elif git status --branch --porcelain -uno | grep behind; then
        echo "Differences from main branch found, updating script."
        echo "Terminating python script session."
        tmux kill-session -t $session
        UOI
        echo "Finished updating the script."
        tmux new -d -s $session 'python3 app.py'
        echo "Script is now running in session \'sensorInput\'."
    else
        echo "No differences found, no action taken."
        tmux has-session -t $session 2>/dev/null
        if [ $? != 0 ]; then
            echo "The script is not running, rebooting the script."
            tmux new -d -s $session 'python3 app.py'
            echo "Script is now running in session \'sensorInput\'."
        fi
    fi
}

while true; do run & sleep 60m; done

I have honestly ran out of ideas I searched in quite a bit of other questions and tried modifying the service file but I can't seem to figure it out.

I'm sorry if I somehow missed the question already being answered here before. Hope someone can help me.

0 Answers
Related