Bottom Page

Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
Issues with Python script as service
#1
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-sc...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

logging.json

--
Satish Gunjal
Quote
#2
Systemd logs into syslog, try to run:

sudo journalctl -u hydroponic
Also check the /var/log folder, maybe the stdout of your service is writing in the /var/log/message file.

About file logs, use the full path when writing them.
/home/pi/lionsfort/hydroponic.log
Quote
#3
@gontajones Logs issue resolved after giving the full path of the log file. Thanks! Also please help me on issue 2: command " sudo systemctl stop hydroponic.service" stops service but relays are still power on!
Quote
#4
Good, you're welcome!

About the "systemctl stop" command, it sends a SIGTERM to the service.
You'll have to get this signal (try: except:), reset the relays (clear GPIOs) and then exit.

Maybe your code now is just exiting.
Quote
#5
Code snippet as below. Please suggest where to make change?

try:
    initialize()
    while True:	
        hourMinute = datetime.now().strftime('%H:%M')
        logger.info("Main()> hourMinute= " + hourMinute)
	
	humidityAndTemp();
	
        if(iState == 'off'):
            startInletFan(hourMinute)
        else:
            stopInletFan(hourMinute)            
        
	if(eState == 'off'):
            startExhaustFan(hourMinute)
        else:
            stopExhaustFan(hourMinute)
        
	if(vState == 'off'):
            startVentilationFan(hourMinute)
        else:
            stopVentilationFan(hourMinute)

	if(pState == 'off'):
            startPump(hourMinute)
        else:
            stopPump(hourMinute)
	
	time.sleep(30)

# End program cleanly with keyboard
except KeyboardInterrupt:
    print " Quit"

# Reset GPIO settings
GPIO.cleanup()
Quote
#6
Could you edit your post (code) using the python tags?
Just click on "Insert python" at text formatting bar when editing (writing) posts.
Quote
#7
done.
Quote
#8
import time
import signal
import sys

def handler(signum, frame):
    print 'Got SIGTERM!'
    sys.exit(0) # raises a SystemExit exception

# Register a handler (function) for the SIGTERM signal
signal.signal(signal.SIGTERM, handler)

try:
    initialize()
    while True:
        hourMinute = datetime.now().strftime('%H:%M')
        logger.info("Main()> hourMinute= " + hourMinute)

        humidityAndTemp();

        if(iState == 'off'):
            startInletFan(hourMinute)
        else:
            stopInletFan(hourMinute)

        if(eState == 'off'):
            startExhaustFan(hourMinute)
        else:
            stopExhaustFan(hourMinute)

        if(vState == 'off'):
            startVentilationFan(hourMinute)
        else:
            stopVentilationFan(hourMinute)

        if(pState == 'off'):
            startPump(hourMinute)
        else:
            stopPump(hourMinute)

        time.sleep(30)

# End program cleanly with keyboard or sys.exit(0)
except KeyboardInterrupt:
    print " Quit (Ctrl+C)"
except SystemExit:
    print " Quit (SIGTERM)"

# Reset GPIO settings
GPIO.cleanup()
Quote
#9
thanks. all issues resolved!
Quote

Top Page

Possibly Related Threads...
Thread Author Replies Views Last Post
  Python's id() function output in prompt and script mode stormshadow 3 69 Dec-07-2018, 06:48 AM
Last Post: casevh
  Pyserial Issues with Python3.7 and Fluke Multimeter jfisher930 11 786 Dec-06-2018, 10:43 AM
Last Post: jfong
  How to use servo motor with TFMini Distance Sensor in python script? programerguy 1 48 Dec-04-2018, 04:57 PM
Last Post: Larz60+
  Run python script remotely using screen programerguy 0 45 Dec-04-2018, 02:31 PM
Last Post: programerguy
  python script cant find folder where files are kept Shameendra 2 99 Nov-23-2018, 04:46 PM
Last Post: Shameendra
  Is this possible in Python? Auto-Send-to-printer on script run? pcsailor 7 211 Nov-06-2018, 06:30 AM
Last Post: pcsailor
  rework of a little python-script - extending getters and setters apollo 7 220 Nov-04-2018, 01:37 PM
Last Post: snippsat
  Error with using pyinstaller to convert python script in Mac OS Takeshio 2 149 Oct-19-2018, 02:42 PM
Last Post: Takeshio
  How can I make this Python script add a limit to trades made with API? streetlaw 0 139 Oct-12-2018, 11:41 AM
Last Post: streetlaw
  using a shell script within python Krszt 2 201 Oct-10-2018, 11:00 AM
Last Post: snippsat

Forum Jump:


Users browsing this thread: 1 Guest(s)