satish.gunjal
Posts: 20
Joined: Sat May 23, 2015 11:50 am

Issues with Python script as service

Wed Jun 20, 2018 9:00 am

Setup details:
RPI Version: Raspberry Pi 2 Model BV1.1
Python Version: 2.7

I have created a small program to control the fan and lights in my hydroponic room. I am using 8 channel relay board with external power supply. Everything works fine if I run script using below command
‘sudo python /home/pi/lionsfort/hydroponic.py’. To stop the execution I type CTRL+C then script stops properly with relay getting power off.

Since I wanted script to start with Raspberry pi, I am running ‘hydroponic.py’ script as a service. But with script running as service created below problems
1. Only console logs getting written and file logs are empty.
2. Script starts ok with relay functioning properly, but when I stop the service (sudo systemctl stop hydroponic.service) relay wont get power off.
3. Script start with raspberry pi power on but also start with every new PUTTY session. There should be only one instance of script running irrespective PUTTY session.

Note: If run script using ‘sudo python /home/pi/lionsfort/hydroponic.py’ I don’t face any of above problems!!

Followed below process to run script as a service on boot: (ref http://www.diegoacuna.me/how-to-run-a-s ... an-jessie/)
1. cd /lib/systemd/system/
2. sudo nano hydroponic.service
3. hydroponic.service content
[Unit]
Description=Hydroponic
After=multi-user.target

[Service]
Type=simple
ExecStart=/usr/bin/python /home/pi/lionsfort/hydroponic.py
Restart=on-abort

[Install]
WantedBy=multi-user.target

4. To activate it, I ran below commands
a. sudo chmod 644 /lib/systemd/system/ hydroponic.service
b. chmod +x /home/pi/lionsfort/hydroponic.py
c. sudo systemctl daemon-reload
d. sudo systemctl enable hydroponic.service
e. sudo systemctl start hydroponic.service


--
Satish Gunjal
Attachments
logging.zip
(503 Bytes) Downloaded 6 times

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

Re: Issues with Python script as service

Thu Jun 21, 2018 8:21 am

1. If your script is writing files you had better set the the directory that it should be run from in your systemd service file. Sorry I forget the name of the option just now. Google knows.

3. If you script is starting on every new PUTTY session that is nothing to do with systemd. You must having something somewhere configured to start the script when a new user session is opened.

As for 2. I'm not sure. Presumably it is not stopped with the control C signal by systemd.

You might want to look into using the "ExecStop" option in your systemd service file.
https://www.digitalocean.com/community/ ... unit-files

And use that to send a signal to you program causing it shutdown cleanly.
http://0pointer.de/blog/projects/system ... ins-4.html

satish.gunjal
Posts: 20
Joined: Sat May 23, 2015 11:50 am

Re: Issues with Python script as service

Thu Jun 21, 2018 10:05 am

all the issues resolved! if anyone facing same issue please refer below link for solution.
https://python-forum.io/Thread-Issues-w ... 2#pid50272

Return to “Python”

Who is online

Users browsing this forum: mike_3rdgear and 21 guests