Restart Python3 scripts if they exit/fail

Not sure if this is correct, but after I did this command, enable now works: chown -R pi.user /home/pi/.config/systemd

However, I am getting an exit code one for the edgebridge server:

pi@raspberrypi:~/.config/systemd/user $ systemctl --user status edgebridge
● edgebridge.service - edgebridge
Loaded: loaded (/home/pi/.config/systemd/user/edgebridge.service; enabled; vendor preset: enabled)
Active: activating (auto-restart) (Result: exit-code) since Sun 2022-01-30 02:32:50 GMT; 18s ago
Process: 3180 ExecStart=/usr/bin/python3 /home/pi/edgebridge.py (code=exited, status=1/FAILURE)
Main PID: 3180 (code=exited, status=1/FAILURE)
 


No problem at all, you're helping me, so I'm glad this shed some light. As usual, user error :) I tried the command "chown -R pi.user /home/pi/.config/systemd" and this is what I got:

oot@raspberrypi:/# chown -R pi.user /home/pi/.config/systemd
chown: invalid user: ‘pi.user’
By pi.user I mean the user.usergoup that runs the python, so that would be pi.something, you can find out the right groupname, by using the groups command when logged in as the pi user, it will be the first group listed (there may be more than one).
 
By pi.user I mean the user.usergoup that runs the python, so that would be pi.something, you can find out the right groupname, by using the groups command when logged in as the pi user, it will be the first group listed (there may be more than one)

I think the failure for edgebridge happened because an occurrence was running through screen (I stopped that process) and no more error.

When I type "groups" while logged in under 'pi' it just lists 'pi' as the first one.

pi@raspberrypi:~ $ groups
pi adm dialout cdrom sudo audio video plugdev games users input netdev lpadmin gpio i2c spi
 
I think the failure for edgebridge happened because an occurrence was running through screen (I stopped that process) and no more error.

When I type "groups" while logged in under 'pi' it just lists 'pi' as the first one.

pi@raspberrypi:~ $ groups
pi adm dialout cdrom sudo audio video plugdev games users input netdev lpadmin gpio i2c spi
So the other running occurrence was likely tripping you up both when running the service and when running the bash script.

Ah ha, your version of Linux uses a group-per-user: the pi-user has a pi-group set aside for its sole use. The right chown command would therefore be:
Code:
# Changing the user to pi and to group named pi
chown -R pi.pi /home/pi/.config/systemd
 
So the other running occurrence was likely tripping you up both when running the service and when running the bash script.

Ah ha, your version of Linux uses a group-per-user: the pi-user has a pi-group set aside for its sole use. The right chown command would therefore be:
Code:
# Changing the user to pi and to group named pi
chown -R pi.pi /home/pi/.config/systemd
Ok - I did that. I don't see errors, but I need to wait a bit to see if this is working. I'm using journalctl -xef -u phonetrack and waiting to see if I can see the process checking the phone every once and a while...but I am not seeing anything (I should see it try to determine presence in the log.) Big improvements, because status is showing enabled now on both scripts..I think it's almost there :)
 
Ok - I did that. I don't see errors, but I need to wait a bit to see if this is working. I'm using journalctl -xef -u phonetrack and waiting to see if I can see the process checking the phone every once and a while...but I am not seeing anything (I should see it try to determine presence in the log.)
The group probably doesn't matter that much in this instance. It's a good idea to fix it up in case it causes future confusion/errors/security-issues.
Big improvements, because status is showing enabled now on both scripts..I think it's almost there :)
I think you might need
Code:
journalctl -xef --user-unit=phonetrack
Or perhaps
Code:
journalctl --user -xef -u=phonetrack
I don't think -u matches user-units by default.
 
Well, thank you very much, it's running for the first time using a service! I appreciate your patience on this. A couple things I still would love to be able to do. First, be able to watch the log live, the commands I tried above showed me some historic things, but I can see the scripts are now working properly based on presence status. A bit strange because when I get the status of phonetrack, it shows it's running, but shows only a few lines of it's activity from a while ago. Just curious with the way you help set this up for me, what would cause the service to restart either of the scripts? A crash?
 
Last edited:
Well, thank you very much, it's running for the first time using a service! I appreciate your patience on this. A couple things I still would love to be able to do. First, be able to watch the log live, the commands I tried above showed me some historic things, but I can see the scripts are now working properly. Also, with the way you help set this up for me, what would cause the service to restart either of the scripts? A crash?
No worries. I had some interest in seeing what was involved with a user service, this seemed like a good learning exercise.

Using jounalctl with the -f argument (as you have done) should result in journalctl sitting and waiting for more messages to arrive. You would normally have to interrupt the wait by using control-c.

If you're using a desktop GUI, I have recently written a Qt GUI journal browser in python (https://github.com/digitaltrails/jouno). It can sit in the system tray, filter for particular messages, and forward then as DBUS desktop-notifications (popup messages). The idea being that you need not watch the journal, filtered messages will just popup on the desktop, at which point you can use the GUI to investigate further.

Because the service file specifies Restart=always, if the ExecStart script/service stops for any reason, systemd will rerun after RestartSec. So both error and non-error exits from the script will result in a restart, as will a reboot. A principle idea behind systemd was to better define and control this kind of service behaviour.
 
I have recently written a Qt GUI journal browser in python (https://github.com/digitaltrails/jouno).

I'm a pretty big fan of KSystemLog but your app has some potential, especially with speed.

But, it crashes every time I press "Journal query"

Code:
ERROR: 
 Traceback (most recent call last):
  File "jouno.py", line 2815, in run
    values_list = [QueryFieldValue(field_name, v) for v in values_set]
  File "jouno.py", line 2815, in <listcomp>
    values_list = [QueryFieldValue(field_name, v) for v in values_set]
  File "jouno.py", line 2839, in __init__
    description = pwd.getpwuid(value).pw_name
KeyError: 'getpwuid(): uid not found: 62803'

Segmentation fault (core dumped)
 
I'm a pretty big fan of KSystemLog but your app has some potential, especially with speed.

But, it crashes every time I press "Journal query"

Code:
ERROR:
 Traceback (most recent call last):
  File "jouno.py", line 2815, in run
    values_list = [QueryFieldValue(field_name, v) for v in values_set]
  File "jouno.py", line 2815, in <listcomp>
    values_list = [QueryFieldValue(field_name, v) for v in values_set]
  File "jouno.py", line 2839, in __init__
    description = pwd.getpwuid(value).pw_name
KeyError: 'getpwuid(): uid not found: 62803'

Segmentation fault (core dumped)
Well that's interesting, it looks like you might have a UID 62803 that doesn't have a passwd entry. Perhaps this a deleted user that has old journal entries. That's not something I had considered. Should be an easy fix, but I will also look at whether lookups for other fields could cause similar issues. Hopefully everything except queries is OK.

The main idea with jouno is to filter and forward to DBUS notifications in order to avoid watching the journal. I use the notifications to keep an eye on backups and any other background events I'd like to be aware of (core dumps, smartd, ...). The log browser and query stuff is just grew out of that basic requirement. It's all very journald focused, I figure the trend is for journald/systemd to take over everything. The browser does show more journald details (double-click an item) than other browsers I've seen, I find that useful at times.

I have posted to kde forums suggesting they could pick up a few ideas and fold them into their own efforts (but I'm over coding in C++/C, so I stick to Python/Qt, but I might take a look at QML since they're doing a lot with that now).
 
According to 'cat /etc/passwd | grep 62803', there's no such entry. So, there's that.

I just figured I'd share the error that it threw.
 
According to 'cat /etc/passwd | grep 62803', there's no such entry. So, there's that.

I just figured I'd share the error that it threw.
I pushed a change to github, the query metadata now uses the numeric UID or GID if the names no longer exist. Thanks for helping get this sorted.
 
I'm almost always happy to help. It's FOSS. As a general rule, I figure I'm obligated to report bugs when I find them.
 

Members online


Latest posts

Top