Python script runs fine except when executed as a service - Raspberry Pi Forums


when run

code: select all

sudo service run_moisture start
error...
import.im6: unable open x server `' @ error/import.c/importimagecommand/368.
from: can't read /var/mail/time
from: can't read /var/mail/datetime
/home/pi/moisture.py: 5: /home/pi/moisture.py: syntax error: "(" unexpected
don't see unused '(' in either script.
python script runs fine when run

code: select all

python /home/pi/moisture.py
of course script terminated when close ssh session.
here bash script start python script

code: select all

### begin init info # provides:          run_moisture # required-start:    $remote_fs $syslog # required-stop:     $remote_fs $ syslog # default-start:     2 3 4 5 # default-stop:      0 1 6 # short-description: runs moisture reciever on startup # description:       enables logging of soil moisture ### end init info   case "$1" in   'start')     echo "starting moisture-sensor"     /home/pi/moisture.py &      ;;    'stop')     echo "stopping moisture-sensor"     killall moisture.py     ;;   *)     echo "usage: /etc/init.d/run_moisture {start|stop}"     exit 1     ;; esac   exit 0 
, here python script

code: select all

import serial time import strftime datetime import datetime, time, timedelta  ser = serial.serial('/dev/ttyacm0',9600)  starttime = datetime.now() try:     while 1:         line=ser.readline().rstrip()         #line2=line1.rstrip('[')         #line3=line2.rstrip(']')         #line=line3.rstrip("'")         temp2=line.split(",")         #print 'temp2', temp2         = datetime.now() - timedelta(hours=4)         #elapsedtime = now-starttime         #elapsedseconds = (elapsedtime.microseconds+(elapsedtime.days*24*3600+e$         #print("%s,%s,%s,&s"%(now.strftime("%y-%m-%d %h:%m:%s"),elapsedseconds,$         print("%s,%s,&s"%(now.strftime("%y-%m-%d %h:%m:%s"),temp2))         f=open('/mnt/moisture/moisturelog.csv','a')         #print >>f,("%s,%s,%s,&s,\n"%(now.strftime("%y-%m-%d, %h:%m:%s"),elapse$         print >>f,("%s,%s,&s,\n"%(now.strftime("%y-%m-%d, %h:%m:%s"),temp2))         f.close() except keyboardinterrupt:     print "\ndone"  
have been working on 2 weeks , @ extent of knowledge. have searched google each part of error , haven't found resolutions seem relevant.

the first problem see you're trying directly execute python script has no shebang:

code: select all

case "$1" in   'start')     echo "starting moisture-sensor"     /home/pi/moisture.py &      ;; 
i add 'shebang' first line of python

code: select all

#!/usr/bin/env python 
, make file executable

code: select all

chmod a+x /home/pi/moisture.py


raspberrypi



Comments

Popular posts from this blog

opencv3, tbb and rasp pi 2 - Raspberry Pi Forums

small ethernet problem - Raspberry Pi Forums

Multithumb configuration params not working? - Joomla! Forum - community, help and support