that's not true.NotRequired wrote:Nice, then we know that the shell script works, but the python script is not invoked at all. When you run command "whereis python3" a list of paths should appear, the first should be something like "/usr/bin/python3", right? Please try to enter the full path to python3 into the shell script (you can remove the echo command again, it has done it's job).
EDIT: Did you put the "print" line into the PY script?
Code: Select all
● analitico.service - Servizio Gestione Google Analytics su MQTT
Loaded: loaded (/lib/systemd/system/analitico.service; enabled)
Active: active (running) since ven 2017-06-30 19:32:32 CEST; 6h ago
Main PID: 1185 (analitico.sh)
CGroup: /system.slice/analitico.service
├─1185 /bin/bash /home/pi/Documents/SH/analitico.sh
└─1192 python3 /home/pi/Documents/PY/analitico.py
giu 30 19:32:32 pitre systemd[1]: Starting Servizio Gestione Google Analytics su MQTT...
giu 30 19:32:32 pitre systemd[1]: Started Servizio Gestione Google Analytics su MQTT.
ok butFerdinand wrote:Perhaps you may post fanspeed.py too.
Then you can compare both files.
Code: Select all
#!/usr/bin/env python3
import os
import time
from time import sleep
import signal
import sys
import RPi.GPIO as GPIO
fanPin = 4
maxTMP = 48
def setup():
GPIO.setwarnings(False)
GPIO.setmode(GPIO.BCM)
GPIO.setup(fanPin, GPIO.OUT)
fanOFF()
return()
def getCPUtemperature():
res = os.popen('vcgencmd measure_temp').readline()
temp =(res.replace("temp=","").replace("'C\n",""))
#print("temp is {0}".format(temp)) #Uncomment here for testing
return temp
def fanON():
setPin(True)
return()
def fanOFF():
setPin(False)
return()
def handleFan():
CPU_temp = float(getCPUtemperature())
if CPU_temp>maxTMP:
fanON()
#print("fan on")
if CPU_temp<maxTMP-5:
fanOFF()
#print("fan off")
return()
def setPin(mode):
GPIO.output(fanPin, mode)
return()
try:
setup()
while True:
handleFan()
sleep(5) # Read the temperature every 5 sec, increase or decrease this limit if you want
except KeyboardInterrupt: # trap a CTRL+C keyboard interrupt
fanOFF()
#GPIO.cleanup() # resets all GPIO ports used by this program
it doesnt make senseFerdinand wrote:Take a look at the first line in analytici.py and fanspeed.py
One file is referring to python 2 the other file is referring to python 3. Perhaps this is the problem. I cannot test at the moment.
If you get output from the shell script, then the shell script is working or at least it is invoked properly. You should use "sudo systemctl status analitico" to check status of the systemd service. You can also check syslog (command: cat /var/log/syslog | grep analitico) if there should be any info about errors there. What happens when you launch the PY script as an executable, without adding it as an argument to python3 (command: /home/pi/Documents/PY/analitico.py)? Does the script still launches properly? If not the issue might be caused by the shebang notation as pointed out by "Ferdinand".komarek wrote:that's not true. as i mentioned before, if i make: sudo service analitico status but the script still not working
if i run this commandNotRequired wrote:What happens when you launch the PY script as an executable, without adding it as an argument to python3 (command: /home/pi/Documents/PY/analitico.py)? Does the script still launches properly? If not the issue might be caused by the shebang notation as pointed out by "Ferdinand".
yes it does:Ferdinand wrote:Your are not the owner of that file
sudo chown pi <your file>
yep you're rightFerdinand wrote:The execute bit is not set
chmod 777 <your file>
yes i changed the first line of analitico.pyNotRequired wrote:I'm launching 2-3 python scripts with systemd - no problem at all, so I'm wondering... Did you try to modify the first line of analitico.py to be "#!/usr/bin/env python3"? Please post contents of analitico.sh (if still used) and analitico.service..
Code: Select all
#!/bin/bash
LOG_FILE="/home/pi/Documents/PY/analitico.log"
echo "Hello from shell" >> $LOG_FILE
/usr/bin/python3 /home/pi/Documents/PY/analitico.py &>> $LOG_FILE
Code: Select all
[Unit]
Description=Servizio Gestione Google Analytics su MQTT
After=multi-user.target
Wants=network.target network-online.target
[Service]
User=pi
Type=idle
ExecStart=/home/pi/Documents/SH/analitico.sh
[Install]
WantedBy=multi-user.target
nothing at allNotRequired wrote:Looks right, are there any indications of errors in analitico.log or syslog (command above)?
Rudestlink wrote:I notice the problem script is using MQTT. I had similar issues running a script using MQTT from cron upon reboot. The fix ended up bring to put a 60 second sleep inside the script before the script does anything (ie after the imports). My guess is it was trying to use something not yet available at that stage of boot. I never bothered exploring what it was but just kept the bandaid solution.
so what do you suggest?tpylkko wrote:My bet is that your script requires session data, which is not available for a systemd system service. Printing to Log or launching a file does not require a user session, but maybe the auth stuff in your Python does? Have you tried a user service instead of the system one? IIRC, this changed in some version of systemd and I am not sure if user services have session in Jessie or not
i never used thattpylkko wrote:Do you mean that you don't know what a user service is?
See this for example, then:
https://wiki.archlinux.org/index.php/Systemd/User
Code: Select all
#!/bin/bash
LOG_FILE="/home/pi/Documents/PY/analitico.log"
#echo "Hello from shell" >> $LOG_FILE
env >> $LOG_FILE
/usr/bin/python3 /home/pi/Documents/PY/analitico.py &>> $LOG_FILE
ok i hope i made it wellNotRequired wrote:You can check the difference in the environment by modifying the analitico.sh to contain:
and compare the output of "env" to analitico.log with the output of "env" run in a console. If the difference is significant this may be something to work on.Code: Select all
#!/bin/bash LOG_FILE="/home/pi/Documents/PY/analitico.log" #echo "Hello from shell" >> $LOG_FILE env >> $LOG_FILE /usr/bin/python3 /home/pi/Documents/PY/analitico.py &>> $LOG_FILE
The idea of adding a "sleep" instruction to the beginning of analitico.py could be a sollution, but I doubt it since the PY script is not getting past initialization (import section).
Code: Select all
Hello from shell
SHELL=/bin/bash
USER=pi
PATH=/usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin:/sbin:/bin
PWD=/
LANG=it_IT.UTF-8
SHLVL=1
HOME=/home/pi
LOGNAME=pi
_=/usr/bin/env
Code: Select all
Hello from shell
INFINALITY_FT_AUTOHINT_HORIZONTAL_STEM_DARKEN_STRENGTH=10
XDG_SESSION_ID=c4
SHELL=/bin/bash
TERM=xterm-256color
INFINALITY_FT_AUTOHINT_VERTICAL_STEM_DARKEN_STRENGTH=25
INFINALITY_FT_BOLD_EMBOLDEN_X_VALUE=0
SSH_CLIENT=192.168.1.10 56681 22
SSH_TTY=/dev/pts/1
INFINALITY_FT_CONTRAST=0
USER=pi
INFINALITY_FT_GRAYSCALE_FILTER_STRENGTH=0
LS_COLORS=rs=0:di=01;34:ln=01;36:mh=00:pi=40;33:so=01;35:do=01;35:bd=40;33;01:cd=40;33;01:or=40;31;01:su=37;41:sg=30;43:ca=30;41:tw=30;42:ow=34;42:st=37;44:ex=01;32:*.tar=01;31:*.tgz=01;31:*.arc=01;31:*.arj=01;31:*.taz=01;31:*.lha=01;31:*.lz4=01;31:*.lzh=01;31:*.lzma=01;31:*.tlz=01;31:*.txz=01;31:*.tzo=01;31:*.t7z=01;31:*.zip=01;31:*.z=01;31:*.Z=01;31:*.dz=01;31:*.gz=01;31:*.lrz=01;31:*.lz=01;31:*.lzo=01;31:*.xz=01;31:*.bz2=01;31:*.bz=01;31:*.tbz=01;31:*.tbz2=01;31:*.tz=01;31:*.deb=01;31:*.rpm=01;31:*.jar=01;31:*.war=01;31:*.ear=01;31:*.sar=01;31:*.rar=01;31:*.alz=01;31:*.ace=01;31:*.zoo=01;31:*.cpio=01;31:*.7z=01;31:*.rz=01;31:*.cab=01;31:*.jpg=01;35:*.jpeg=01;35:*.gif=01;35:*.bmp=01;35:*.pbm=01;35:*.pgm=01;35:*.ppm=01;35:*.tga=01;35:*.xbm=01;35:*.xpm=01;35:*.tif=01;35:*.tiff=01;35:*.png=01;35:*.svg=01;35:*.svgz=01;35:*.mng=01;35:*.pcx=01;35:*.mov=01;35:*.mpg=01;35:*.mpeg=01;35:*.m2v=01;35:*.mkv=01;35:*.webm=01;35:*.ogm=01;35:*.mp4=01;35:*.m4v=01;35:*.mp4v=01;35:*.vob=01;35:*.qt=01;35:*.nuv=01;35:*.wmv=01;35:*.asf=01;35:*.rm=01;35:*.rmvb=01;35:*.flc=01;35:*.avi=01;35:*.fli=01;35:*.flv=01;35:*.gl=01;35:*.dl=01;35:*.xcf=01;35:*.xwd=01;35:*.yuv=01;35:*.cgm=01;35:*.emf=01;35:*.axv=01;35:*.anx=01;35:*.ogv=01;35:*.ogx=01;35:*.aac=00;36:*.au=00;36:*.flac=00;36:*.m4a=00;36:*.mid=00;36:*.midi=00;36:*.mka=00;36:*.mp3=00;36:*.mpc=00;36:*.ogg=00;36:*.ra=00;36:*.wav=00;36:*.axa=00;36:*.oga=00;36:*.spx=00;36:*.xspf=00;36:
INFINALITY_FT_FRINGE_FILTER_STRENGTH=0
INFINALITY_FT_USE_VARIOUS_TWEAKS=true
INFINALITY_FT_BRIGHTNESS=0
INFINALITY_FT_GAMMA_CORRECTION=0 100
PATH=/usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin:/sbin:/bin:/usr/local/games:/usr/games
MAIL=/var/mail/pi
PWD=/home/pi
LANG=it_IT.UTF-8
INFINALITY_FT_FILTER_PARAMS=11 22 38 22 11
INFINALITY_FT_STEM_SNAPPING_SLIDING_SCALE=40
INFINALITY_FT_USE_KNOWN_SETTINGS_ON_SELECTED_FONTS=true
INFINALITY_FT_WINDOWS_STYLE_SHARPENING_STRENGTH=10
NODE_PATH=/usr/lib/nodejs:/usr/lib/node_modules:/usr/share/javascript
INFINALITY_FT_CHROMEOS_STYLE_SHARPENING_STRENGTH=0
INFINALITY_FT_STEM_ALIGNMENT_STRENGTH=25
INFINALITY_FT_AUTOHINT_SNAP_STEM_HEIGHT=100
HOME=/home/pi
SHLVL=2
INFINALITY_FT_BOLD_EMBOLDEN_Y_VALUE=0
LOGNAME=pi
INFINALITY_FT_GLOBAL_EMBOLDEN_Y_VALUE=0
SSH_CONNECTION=192.168.1.10 56681 192.168.1.3 22
INFINALITY_FT_STEM_FITTING_STRENGTH=25
INFINALITY_FT_AUTOHINT_INCREASE_GLYPH_HEIGHTS=true
TEXTDOMAIN=Linux-PAM
XDG_RUNTIME_DIR=/run/user/1000
INFINALITY_FT_GLOBAL_EMBOLDEN_X_VALUE=0
_=/usr/bin/env
Code: Select all
[Unit]
Description=Servizio Gestione Google Analytics su MQTT
After=multi-user.target
Wants=network.target network-online.target
[Service]
EnvironmentFile=/home/pi/env.txt
User=pi
Type=idle
ExecStart=/home/pi/Documents/SH/analitico.sh
[Install]
WantedBy=multi-user.target