NotRequired
Posts: 195
Joined: Sat Apr 29, 2017 10:36 am
Location: Denmark

Re: python script at startup

Fri Jun 30, 2017 5:41 pm

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?
Please do not ask questions in private messages, they will not help others.

komarek
Posts: 146
Joined: Fri Mar 31, 2017 1:19 am

Re: python script at startup

Sat Jul 01, 2017 12:18 am

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?
that's not true.
as i mentioned before, if i make:
sudo service analitico status

i have this:

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.
but the script still not working

User avatar
Ferdinand
Posts: 236
Joined: Sun Dec 01, 2013 2:24 pm
Location: Leiderdorp, NL

Re: python script at startup

Sat Jul 01, 2017 6:34 am

Perhaps you may post fanspeed.py too.
Then you can compare both files.
Success with your project!
Ferdinand

komarek
Posts: 146
Joined: Fri Mar 31, 2017 1:19 am

Re: python script at startup

Sat Jul 01, 2017 7:01 am

Ferdinand wrote:Perhaps you may post fanspeed.py too.
Then you can compare both files.
ok but
i can't see the point

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

User avatar
Ferdinand
Posts: 236
Joined: Sun Dec 01, 2013 2:24 pm
Location: Leiderdorp, NL

Re: python script at startup

Sat Jul 01, 2017 8:13 am

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.
Success with your project!
Ferdinand

komarek
Posts: 146
Joined: Fri Mar 31, 2017 1:19 am

Re: python script at startup

Sat Jul 01, 2017 8:59 am

Ferdinand 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.
it doesnt make sense
if i run the script analitico.py or even the bash script analitico.sh out of the systemd it's working perfectly
but i can try

NotRequired
Posts: 195
Joined: Sat Apr 29, 2017 10:36 am
Location: Denmark

Re: python script at startup

Sat Jul 01, 2017 9:35 am

komarek wrote:that's not true. as i mentioned before, if i make: sudo service analitico status but the script still not working
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".
Please do not ask questions in private messages, they will not help others.

komarek
Posts: 146
Joined: Fri Mar 31, 2017 1:19 am

Re: python script at startup

Sat Jul 01, 2017 10:36 am

NotRequired 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".
if i run this command
the answer is:
-bash: /home/pi/Documents/PY/analitico.py: Permission denied

User avatar
Ferdinand
Posts: 236
Joined: Sun Dec 01, 2013 2:24 pm
Location: Leiderdorp, NL

Re: python script at startup

Sat Jul 01, 2017 10:41 am

Your are not the owner of that file
sudo chown pi <your file>
Success with your project!
Ferdinand

komarek
Posts: 146
Joined: Fri Mar 31, 2017 1:19 am

Re: python script at startup

Sat Jul 01, 2017 11:21 am

Ferdinand wrote:Your are not the owner of that file
sudo chown pi <your file>
yes it does:
-rw-r--r-- 1 pi pi 4580 lug 1 11:00 /home/pi/Documents/PY/analitico.py

but when i try to run it as you said... i have the same error:
-bash: /home/pi/Documents/PY/analitico.py: Permission denied

User avatar
Ferdinand
Posts: 236
Joined: Sun Dec 01, 2013 2:24 pm
Location: Leiderdorp, NL

Re: python script at startup

Sat Jul 01, 2017 11:49 am

The execute bit is not set
chmod 777 <your file>
Success with your project!
Ferdinand

komarek
Posts: 146
Joined: Fri Mar 31, 2017 1:19 am

Re: python script at startup

Sat Jul 01, 2017 12:42 pm

Ferdinand wrote:The execute bit is not set
chmod 777 <your file>
yep you're right
anyway... now it's working.... with /home/pi/Documents/PY/analitico.py
but if i run the daemon with systemd the script still doesnt work

NotRequired
Posts: 195
Joined: Sat Apr 29, 2017 10:36 am
Location: Denmark

Re: python script at startup

Sat Jul 01, 2017 1:17 pm

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..
Please do not ask questions in private messages, they will not help others.

komarek
Posts: 146
Joined: Fri Mar 31, 2017 1:19 am

Re: python script at startup

Sat Jul 01, 2017 1:27 pm

NotRequired 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..
yes i changed the first line of analitico.py

this is analitico.sh

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
and this is analitico.service

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
as we said before, the shell is running good for sure

NotRequired
Posts: 195
Joined: Sat Apr 29, 2017 10:36 am
Location: Denmark

Re: python script at startup

Sat Jul 01, 2017 1:47 pm

Looks right, are there any indications of errors in analitico.log or syslog (command above)?
Please do not ask questions in private messages, they will not help others.

tpylkko
Posts: 382
Joined: Tue Oct 14, 2014 5:21 pm

Re: python script at startup

Sat Jul 01, 2017 2:00 pm

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

Rudestlink
Posts: 22
Joined: Mon Mar 02, 2015 4:57 am
Location: Land of the Famous Potato
Contact: Website

Re: python script at startup

Sat Jul 01, 2017 2:05 pm

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.

komarek
Posts: 146
Joined: Fri Mar 31, 2017 1:19 am

Re: python script at startup

Sat Jul 01, 2017 2:20 pm

NotRequired wrote:Looks right, are there any indications of errors in analitico.log or syslog (command above)?
nothing at all

komarek
Posts: 146
Joined: Fri Mar 31, 2017 1:19 am

Re: python script at startup

Sat Jul 01, 2017 2:23 pm

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.

i don't think so
i tried the daemon without rebooting... just doing "sudo service analitico restart"... or stop and then start.
same results.
also:
i have other scripts goin on in the boot sequence (with rc.local as you can see in the beginning of this thread) using MQTT as well but not givin me any issue

komarek
Posts: 146
Joined: Fri Mar 31, 2017 1:19 am

Re: python script at startup

Sat Jul 01, 2017 2:23 pm

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
so what do you suggest?

tpylkko
Posts: 382
Joined: Tue Oct 14, 2014 5:21 pm

Re: python script at startup

Sat Jul 01, 2017 3:37 pm

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

komarek
Posts: 146
Joined: Fri Mar 31, 2017 1:19 am

Re: python script at startup

Sat Jul 01, 2017 4:23 pm

tpylkko 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
i never used that

NotRequired
Posts: 195
Joined: Sat Apr 29, 2017 10:36 am
Location: Denmark

Re: python script at startup

Sat Jul 01, 2017 5:32 pm

You can check the difference in the environment by modifying the analitico.sh to contain:

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
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.

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).
Please do not ask questions in private messages, they will not help others.

komarek
Posts: 146
Joined: Fri Mar 31, 2017 1:19 am

Re: python script at startup

Sat Jul 01, 2017 6:55 pm

NotRequired wrote:You can check the difference in the environment by modifying the analitico.sh to contain:

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
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.

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).
ok i hope i made it well

in this case the script doesn't work (with systemd):
the log:

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

in this case the script works and that's the log:

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

NotRequired
Posts: 195
Joined: Sat Apr 29, 2017 10:36 am
Location: Denmark

Re: python script at startup

Sat Jul 01, 2017 7:19 pm

Try to execute "env > /home/pi/env.txt" and modify your service to:

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
This will ensure the existence of the entire pi-user's environment - you should remove all per session variables, though..
Please do not ask questions in private messages, they will not help others.

Return to “General discussion”