Sohil.Mehta
Posts: 35
Joined: Thu Feb 01, 2018 6:45 am

[SOLVED] Autostarting a python script at boot not working in /etc/rc.local and systemd

Mon Aug 13, 2018 7:16 am

I am working on a data acquisition project where I am sending a bunch of sensor data to the cloud. Here is a small snippet from my code:

Code: Select all

#!/usr/bin/python3
# coding: utf-8

import ctypes
from systemd import journal
.
.
. #Rest of the code
.
journal.send(
            channel = 'sensor',
            priority = journal.Priority.INFO,
            messageid = "%d" % (seq_num),
            timestamp = "%s" % (CurrentTimestamp),
            tdiff = "%s" % (dt),     
            acc_x="%f" % (cax),
            acc_y="%f" % (cay),
            acc_z="%f" % (caz),
            gyr_x="%f" % (cgx),
            gyr_y="%f" % (cgy),
            gyr_z="%f" % (cgz),
            horn = "%d" % (GPIO.input(hornpin)),
            brake = "%d" % (GPIO.input(brakepin)),
            rpm = "%d" % (GPIO.input(rpmpin)),
	    lux = "%f" % (lux),
            temperature_batterypack = "%d" % (temperature_batterypack),
            temperature_ambientair = "%d" % (temperature_ambientair),
            temperature_scooterbody = "%d" % (temperature_scooterbody),
            )
I have designed my own board using the public schematics of Raspberry Pi Compute module 3. The OS that I am using is the latest version of Raspbian Stretch Lite (June 2018 release). I am trying to autostart the code at boot by adding the following line in /etc/rc.local before exit 0:

Code: Select all

sudo python3 /home/pi/test_scripts/cloud_test.py &
and I get the following error:

Code: Select all

AttributeError: module 'systemd.journal' has no attribute 'Priority'
I tried autostarting using systemd (so I created a service file) and when I check the status of the service file, it gives me the same exact error. Whereas, if I don't do auto start and manually run the code on the command prompt as:

Code: Select all

$ python3 cloud_test.py
the code runs correctly and sends data to the cloud and I can see all the data on a website. This means all modules and python packages are correctly installed and I do not get any error. I also made by python script executable by using sudo chmod +x cloud_test.py and have also included #!/usr/bin/python3 at the top of my code. Then why do I see the error AttributeError: module 'systemd.journal' has no attribute 'Priority' only on autostart but not on manual start?
Last edited by Sohil.Mehta on Mon Aug 13, 2018 6:57 pm, edited 1 time in total.

User avatar
RaTTuS
Posts: 10061
Joined: Tue Nov 29, 2011 11:12 am
Location: North West UK

Re: Autostarting a python script at boot not working in /etc/rc.local and systemd

Mon Aug 13, 2018 7:45 am

use systemd
show your .service file
but probably you need to add the working directory or whatever defines
journal.Priority.INFO,

and you don't need sudo in rc.lcoal [but don't do it that way anyway]
you probably don't need to run it as root anyway
How To ask Questions :- http://www.catb.org/esr/faqs/smart-questions.html
WARNING - some parts of this post may be erroneous YMMV

1QC43qbL5FySu2Pi51vGqKqxy3UiJgukSX
Covfefe

PhilE
Raspberry Pi Engineer & Forum Moderator
Raspberry Pi Engineer & Forum Moderator
Posts: 1826
Joined: Mon Sep 29, 2014 1:07 pm
Location: Cambridge

Re: Autostarting a python script at boot not working in /etc/rc.local and systemd

Mon Aug 13, 2018 8:11 am

[Moved to Advanced Users]

DirkS
Posts: 8876
Joined: Tue Jun 19, 2012 9:46 pm
Location: Essex, UK

Re: Autostarting a python script at boot not working in /etc/rc.local and systemd

Mon Aug 13, 2018 8:37 am

I can see 2 packages called 'systemd'
* package python3-systemd is in the Raspbian repo. This one has the attribute PRIORITY
* module 'systemd' available with pip3 from pypi. This one has Priority

So these 2 are not the same. Did you install both?
Did you install from pypi without 'sudo'? This would mean that if you run the script without sudo then you would use the second version (which is what you did from the CLI)
If you run with sudo (as you did by running from rc.local and most likely when using a service file) then the first version would be used (because the second is only installed for user pi)

I can see 2 ways of solving it (there are probably other ways):
* remove the python3-systemd package if you don't use it. But you have to re-install the other package using 'sudo pip3'
* use a systemd service and run it as user / group pi (add User and Group to Service section)

User avatar
mahjongg
Forum Moderator
Forum Moderator
Posts: 10869
Joined: Sun Mar 11, 2012 12:19 am
Location: South Holland, The Netherlands

Re: Autostarting a python script at boot not working in /etc/rc.local and systemd

Mon Aug 13, 2018 10:22 am

[moved back to Python, this isn't an advanced issue.]

Sohil.Mehta
Posts: 35
Joined: Thu Feb 01, 2018 6:45 am

Re: Autostarting a python script at boot not working in /etc/rc.local and systemd

Mon Aug 13, 2018 6:43 pm

Hey @Dirk, the answer is exactly what you said. And I did both the things that you told, and it worked. I reinstalled systemd with sudo from pypi:

Code: Select all

sudo pip3 install systemd
I also included User=pi in my service file and it worked.

Code: Select all

[Unit]
Description = MESSI v2.0 sensor code
After=multi-user.target

[Service]
Type=idle
ExecStart=/usr/bin/python3 -u cloud_test.py
WorkingDirectory=/home/pi/test_scripts
#StandardOutput=inherit
#StandardError=inherit
Restart=always
User=pi

[Install]
WantedBy=multi-user.target
I did not remove python3-systemd. That wasn't necessary. Also I am yet to test this on /etc/rc.local. But the service file is working absolutely fine. Thanks a lot @DirkS and RaTTus. Sorry for the late reply, I was a bit held up.

DirkS
Posts: 8876
Joined: Tue Jun 19, 2012 9:46 pm
Location: Essex, UK

Re: Autostarting a python script at boot not working in /etc/rc.local and systemd

Mon Aug 13, 2018 7:13 pm

Sohil.Mehta wrote:
Mon Aug 13, 2018 6:43 pm
I did not remove python3-systemd. That wasn't necessary. Also I am yet to test this on /etc/rc.local. But the service file is working absolutely fine. Thanks a lot @DirkS and RaTTus. Sorry for the late reply, I was a bit held up.
Good!

By leaving both there is still a bit of a risk that the wrong module is found.
But at least you know what's going on and how to solve it.

Return to “Python”

Who is online

Users browsing this forum: No registered users and 13 guests