grininmonkey
Posts: 65
Joined: Mon Jul 30, 2018 3:44 pm

Using python file as service issues

Sat Aug 11, 2018 4:50 am

I have the following tiny script that I am trying to setup as a systemd service. All though I had no issue doing so, it constantly fails and exits randomly and frequently. I tried looking in syslog for some ideas... but that appears to be generic failure messages. I run the file from terminal and walk away and it seems to never crash, so whatever is making it crash seems to only be within the context of being executed via systemctl or systemd (not sure yet what the proper linux terminology is yet)

Here is the script/code:

Code: Select all

import os
import glob
import pymysql
import time
from datetime import datetime

__version__ = 1.0

os.system('modprobe w1-gpio')
os.system('modprobe w1-therm')
 
base_dir = '/sys/bus/w1/devices/'
device_filename = '/w1_slave' 
device_folder = [d for d in glob.glob(base_dir + '28*')]

sql_proc = 'proc_updateDeviceValue'

db = pymysql.connect("localhost","##########","##########","thermostat")
cursor = db.cursor()

def read_temp_raw(device_file):
    lines = ""
    try:
        f = open(device_file, 'r')
        lines = f.readlines()
        f.close()
    except:
        print("error opening device file")
    return lines
 
def read_temp(device_file):
    temp_f = -99.9 #FM
    lines = read_temp_raw(device_file)
    try:
        while lines[0].strip()[-3:] != 'YES':
            time.sleep(0.2)
            lines = read_temp_raw()
        equals_pos = lines[1].find('t=')
        if equals_pos != -1:
            temp_string = lines[1][equals_pos+2:]
            temp_c = float(temp_string) / 1000.0
            temp_f = temp_c * 9.0 / 5.0 + 32.0
    except:
        print("error in parsing device file lines")
    return temp_f

def log_reading(device, dvalue):
    args = [device, dvalue]
    try:
        cursor.callproc(sql_proc, args)
    except pymysql.InternalError as error:
        db.rollback()
        code, message = error.args
        print(code, message)
    return "Success"

while True:
    for f in device_folder:
        log_reading(f.replace(base_dir,""),read_temp(f + device_filename))
    time.sleep(5)

db.close()
Here is the .service unit

Code: Select all

[Unit]
Description=Thermostat-ds18b20-daemon
After=network.target mysqld.service

[Service]
WorkingDirectory=/home/pi/Projects/Thermostat
ExecStart=/usr/bin/python3 /home/pi/Projects/Thermostat/thermostat_ds18b20_daemon.py
StandardOutput=null
User=root

[Install]
WantedBy=multi-user.target
Alias=thermtemp.service
I setup it up to run via @Startup in crontab.... and it didn't fail for hours.... I then killed it, and restarted the service and within 5 minutes.. a message like the following from syslog

Code: Select all

Aug 10 23:56:13 raspberrypi systemd[1]: thermostat_ds18b20_daemon.service: Main process exited, code=exited, status=1/FAILURE
Aug 10 23:56:13 raspberrypi systemd[1]: thermostat_ds18b20_daemon.service: Unit entered failed state.
Aug 10 23:56:13 raspberrypi systemd[1]: thermostat_ds18b20_daemon.service: Failed with result 'exit-code'.
I guess I can just fall back to starting up from crontab, but I really would like to figure out what is going on. The longest stretch of time it has run as service has only been 10 minutes. So any suggestions on what I can change to capture more or where I can currently look for more debugging info would be greatly appreciated... thanks!

Heater
Posts: 9842
Joined: Tue Jul 17, 2012 3:02 pm

Re: Using python file as service issues

Sat Aug 11, 2018 6:07 am

First fix up your service file so that it does not throw away output and does actually log something to syslog. Then you might be able to see what is failing.

Of course ones program should not be failing but for unexpected occasions when it does systemd can restart it automatically

Here is an example service file of mine that does that:

Code: Select all

[Service]
WorkingDirectory=/home/pi/propanel
ExecStart=/usr/local/bin/node --expose-gc /home/pi/propanel/assets/server-bundle.js
Restart=always
StandardOutput=syslog
StandardError=syslog
SyslogIdentifier=propanel
User=root
Group=root
Environment=NODE_ENV=production

[Install]
WantedBy=multi-user.target
I'm not sure why you are messing around with crontab to use a systemd service. Instead se the systemctl command to get the thing running:

Code: Select all

# start a service
$sudo  systemctl start myService

# Check the service status 
$ sudo systemctl status myService

# Stop the service
$ sudo systemctl stop myService

# Enable the service to run at boot time
$ sudo systemctl enable myService
 

grininmonkey
Posts: 65
Joined: Mon Jul 30, 2018 3:44 pm

Re: Using python file as service issues

Sat Aug 11, 2018 4:25 pm

Heater wrote:
Sat Aug 11, 2018 6:07 am
First fix up your service file so that it does not throw away output and does actually log something to syslog. Then you might be able to see what is failing.

Of course ones program should not be failing but for unexpected occasions when it does systemd can restart it automatically

I'm not sure why you are messing around with crontab to use a systemd service. Instead se the systemctl command to get the thing running:
Thanks for service file tag suggestions, it helped me get an idea, which leads me to another question but first let me report (in short) I believe its stable now while being executed via systemd.

So as I was tweaking the py file and the database and manipulating the service file and testing (etc..) while doing the following process each time I had modifying the py file and finished testing via running it from cli... stop/disable the service.....then enable/start the service.

After adding the syslog tags in the service file, I was seeing that as I was updating the procedure in the db it must have been bad timing and the py file (while running as service and not via cli) appeared to not be following my try except blocks that I added later after the first time I starting messing with setting it up as service.

This had me boggled for a bit, as I would stop the service, run the py file via cli and do things in the db to cause errors and the py file would not exit/fail and just keep clucking along.... so when I stop the cli instance and restart it as service... and do the same things again... it would exit/fail and I could see the expected errors in syslog.

Anyway, long story short, I pulled the "windows" go-to and simply rebooted the pi, and since I did this... the py file running as service hasn't exited/failed once and I have been hacking away at the db again and (on purpose) causing the proc to have an error... and the py file is properly not stopping now.... as it was doing when I ran the py file from cli as I previously described.

So I really don't know what the heck was happening, but sure seemed like... "somehow"... systemd was executing some form of cached version of the file before I added the try blocks ..... or something.... could it be a sign of SD card issue?

Heater
Posts: 9842
Joined: Tue Jul 17, 2012 3:02 pm

Re: Using python file as service issues

Sat Aug 11, 2018 4:54 pm

I'm glad to hear it's working better for you.

Your story is so convoluted it's impossible to guess what any problem may be.

I don't believe systemd has any notion of cached old versions of the things it runs. It runs what you have at the time.

I would not jump to suspecting SD card or file system problems unless there were other weird things going on.

grininmonkey
Posts: 65
Joined: Mon Jul 30, 2018 3:44 pm

Re: Using python file as service issues

Sat Aug 11, 2018 5:49 pm

Heater wrote:
Sat Aug 11, 2018 4:54 pm
I'm glad to hear it's working better for you.

Your story is so convoluted it's impossible to guess what any problem may be.

I don't believe systemd has any notion of cached old versions of the things it runs. It runs what you have at the time.

I would not jump to suspecting SD card or file system problems unless there were other weird things going on.
This is the first.... weird thing to happen so far, so who knows... But I am really enjoying playing around in Linux again after nearly 2 decades and especially this Pi .... little thing has my brain gears just a turning.

Return to “General discussion”

Who is online

Users browsing this forum: CharlyDelta, gkreidl and 34 guests