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

Re: python script at startup

Sat Jul 01, 2017 7:35 pm

still doesn't work

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

Re: python script at startup

Sat Jul 01, 2017 7:48 pm

komarek wrote:
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
Why?

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

Re: python script at startup

Sat Jul 01, 2017 7:55 pm

tpylkko wrote:
komarek wrote:
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
Why?
just didnt know it

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

Re: python script at startup

Sat Jul 01, 2017 8:00 pm

Ok. I thought you maybe dismissed the suggestion because you perceived it to be 'not good'. And was wondering if you had d some reasoning for it

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

Re: python script at startup

Sat Jul 01, 2017 8:23 pm

guys
this thing is way too difficult and complicated for me
i think i'm giving up

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

Re: python script at startup

Sun Jul 02, 2017 10:36 am

I have one final suggestion based on the "sleep" idear earlier in the thread. First remove the "EnvironmentFile" line from your service, then modify your analitico.sh to:

Code: Select all

#!/bin/bash
sleep 60s
LOG_FILE="/home/pi/Documents/PY/analitico.log"
echo "Starting analitico.py.." >> $LOG_FILE
/usr/bin/python3 /home/pi/Documents/PY/analitico.py &>> $LOG_FILE
This will wait for 60 seconds before the PY script is executed.
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

Sun Jul 02, 2017 12:30 pm

NotRequired wrote:I have one final suggestion based on the "sleep" idear earlier in the thread. First remove the "EnvironmentFile" line from your service, then modify your analitico.sh to:

Code: Select all

#!/bin/bash
sleep 60s
LOG_FILE="/home/pi/Documents/PY/analitico.log"
echo "Starting analitico.py.." >> $LOG_FILE
/usr/bin/python3 /home/pi/Documents/PY/analitico.py &>> $LOG_FILE
This will wait for 60 seconds before the PY script is executed.
service analitico status:

Code: Select all

● analitico.service - Servizio Gestione Google Analytics su MQTT
   Loaded: loaded (/lib/systemd/system/analitico.service; enabled)
   Active: failed (Result: exit-code) since dom 2017-07-02 14:27:53 CEST; 1min 58s ago
  Process: 31202 ExecStart=/home/pi/Documents/SH/analitico.sh (code=exited, status=203/EXEC)
 Main PID: 31202 (code=exited, status=203/EXEC)

lug 02 14:27:53 pitre systemd[1]: Started Servizio Gestione Google Analytics su MQTT.
lug 02 14:27:53 pitre systemd[1]: analitico.service: main process exited, code=exited, status=203/EXEC
lug 02 14:27:53 pitre systemd[1]: Unit analitico.service entered failed state.

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

Re: python script at startup

Sun Jul 02, 2017 12:47 pm

Is the line "Starting analitico.py.." added to the analitico log? If not try to put "TimeoutStartSec=infinity" in your [Service] section of the 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

Sun Jul 02, 2017 1:06 pm

NotRequired wrote:Is the line "Starting analitico.py.." added to the analitico log? If not try to put "TimeoutStartSec=infinity" in your [Service] section of the service.
analitico was empty
then i added the line you told me so the file service now is like this:

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
TimeoutStartSec=infinity
ExecStart=/home/pi/Documents/SH/analitico.sh

[Install]
WantedBy=multi-user.target
then
restarted the service
and still doesnt work... this is the service status:

Code: Select all

● analitico.service - Servizio Gestione Google Analytics su MQTT
   Loaded: loaded (/lib/systemd/system/analitico.service; enabled)
   Active: failed (Result: exit-code) since dom 2017-07-02 15:04:06 CEST; 1min 39s ago
  Process: 16045 ExecStart=/home/pi/Documents/SH/analitico.sh (code=exited, status=203/EXEC)
 Main PID: 16045 (code=exited, status=203/EXEC)

lug 02 15:04:06 pitre systemd[1]: Started Servizio Gestione Google Analytics su MQTT.
lug 02 15:04:06 pitre systemd[1]: analitico.service: main process exited, code=exited, status=203/EXEC
lug 02 15:04:06 pitre systemd[1]: Unit analitico.service entered failed state.

and analitico.log is still empty

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

Re: python script at startup

Sun Jul 02, 2017 1:22 pm

If you run the shell script from the command line, does it add a line to analitico.log?

EDIT: Can you start the service manually with: "sudo systemctl start analitico"?
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

Sun Jul 02, 2017 1:35 pm

NotRequired wrote:If you run the shell script from the command line, does it add a line to analitico.log?

EDIT: Can you start the service manually with: "sudo systemctl start analitico"?
i had to chamod 777 /home/pi/Documents/SH/analitico.sh to make it run
and then it ran
but analitico.log is still empty


ps
i can start/restart/stop the service manually... but i use the syntax "sudo service start" which i think is the same as "sudo systemctl start analitico"

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

Re: python script at startup

Sun Jul 02, 2017 1:43 pm

If you run analitico.sh from the command line, it MUST add a line to analitico.log before it starts the py script. If it does not there must be a typo in the path or something. If you run the command: "echo "Added a line" >> /home/pi/Documents/PY/analitico.log" it must also add a line.
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

Sun Jul 02, 2017 1:52 pm

NotRequired wrote:If you run analitico.sh from the command line, it MUST add a line to analitico.log before it starts the py script. If it does not there must be a typo in the path or something. If you run the command: "echo "Added a line" >> /home/pi/Documents/PY/analitico.log" it must also add a line.
yes you're right
there was an error

you know what...it's strange
if i run it directly with /home/pi/Documents/SH/analitico.sh
the script is actually working BUT
the line in analitico.py "print ("DEBUG ATTEMPT ANALITICO")" is like ignored.. cause i have nothing like that in analitico.log
indeed analitico.log is just like this:

Code: Select all

Starting analitico.py..

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

Re: python script at startup

Sun Jul 02, 2017 2:07 pm

so HOLD ON A SECOND
this is the situation
- sudo service analitico start
- sudo service analitico status

Code: Select all

● analitico.service - Servizio Gestione Google Analytics su MQTT
   Loaded: loaded (/lib/systemd/system/analitico.service; enabled)
   Active: active (running) since dom 2017-07-02 15:57:31 CEST; 1min 42s ago
 Main PID: 8658 (analitico.sh)
   CGroup: /system.slice/analitico.service
           ├─8658 /bin/bash /home/pi/Documents/SH/analitico.sh
           └─9221 /usr/bin/python3 /home/pi/Documents/PY/analitico.py

lug 02 15:57:31 pitre systemd[1]: Started Servizio Gestione Google Analytics su MQTT.
after 60sec:
- cat Documents/PY/analitico.log

Code: Select all

Starting analitico.py..
but nothing else... so the script doesn't work

if i run the bash script manually:
- /home/pi/Documents/SH/analitico.sh
after 60 sec i have analitico.log like this:

Code: Select all

Starting analitico.py..
and the script is actually working
but analitico.log doesnt change
ONLY when i interrupt the bash script with ctrl-c then analitico.log changes to this:

Code: Select all

Starting analitico.py..
DEBUG ATTEMPT ANALITICO
Traceback (most recent call last):
  File "/home/pi/Documents/PY/analitico.py", line 135, in <module>
    client.loop_forever()
  File "/usr/local/lib/python3.4/dist-packages/paho/mqtt/client.py", line 1410, in loop_forever
    rc = self.loop(timeout, max_packets)
  File "/usr/local/lib/python3.4/dist-packages/paho/mqtt/client.py", line 911, in loop
    socklist = select.select(rlist, wlist, [], timeout)
KeyboardInterrupt
ps: i added the line: print ("DEBUG ATTEMPT ANALITICO") at the beginning of analitico.py

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

Re: python script at startup

Sun Jul 02, 2017 3:05 pm

Perhaps you may try this:
edit in your home directory the file autostart in the directory .config/lxsession/LXDE-pi
And add the following line:
@lxterminal -e './Documents/PY/analitico.py'

Reboot your machine.
Success with your project!
Ferdinand

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

Re: python script at startup

Sun Jul 02, 2017 3:20 pm

Ferdinand wrote:Perhaps you may try this:
edit in your home directory the file autostart in the directory .config/lxsession/LXDE-pi
And add the following line:
@lxterminal -e './Documents/PY/analitico.py'

Reboot your machine.
i dont know if it's working but
it doesnt look like an excellent solution to me. sorry

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

Re: python script at startup

Sun Jul 02, 2017 6:50 pm

Did you try:
sudo journalctl -u <your application>.service > <your log file>
Success with your project!
Ferdinand

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

Re: python script at startup

Sun Jul 02, 2017 7:58 pm

Ferdinand wrote:Did you try:
sudo journalctl -u <your application>.service > <your log file>
no

now i did it
but nothing interesting happens

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

Re: python script at startup

Mon Jul 03, 2017 9:51 am

Success with your project!
Ferdinand

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

Re: python script at startup

Mon Jul 03, 2017 10:04 am

Ferdinand wrote:This you follow this tutorial?

https://learn.adafruit.com/diy-esp8266- ... i?view=all
no
anyway it's not the problem of the script. the script works perfect when i run it manually
the problem is the systemd

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

Re: python script at startup

Mon Jul 03, 2017 10:20 am

I have read that already.
I cannot test your application because I have no mqtt server running on my pi. I used the server and application of eclipse.org and that runs. See my previous post of @lxterminal -e './home/pi.../

Adafruit writes:
The mosquitto mqtt server is started with systemd and the <your application>.py file is started with rc.local.

Please read that adafruit project.
Success with your project!
Ferdinand

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

Re: python script at startup

Mon Jul 03, 2017 11:01 am

I think it sounds like a good idea to start the mqtt as a background system service and run the script autostarted from the desktop, since then the script will run with the pi user and have the same session and environment as when it is run manually. But it will not work when /if you are using a Raspbian without graphicall desktop to begin with

You are daemon-reloading when you test changes to the service? If you post all the required data to get to the point where you are now, I could try to see if I can get it to work

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

Re: python script at startup

Mon Jul 03, 2017 2:54 pm

I think I found your problem in your original setup (rc.local). Your network is not yet running after a reboot, so you have to wait a couple of seconds.

analitico.py
add the following line just after import time
time.sleep(20)

rc.local
edit the line "python3 /home/pi/Documents/PY/analitico.py &" into
"python3 /home/pi/Documents/PY/analitico.py > /home/pi/Documents/PY/analitico.log"

reboot your system (don't forget to disable analitico.service on your system)

I tested my setup with iot.eclipse.org
Success with your project!
Ferdinand

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

Re: python script at startup

Mon Jul 03, 2017 5:14 pm

Ferdinand wrote:I think I found your problem in your original setup (rc.local). Your network is not yet running after a reboot, so you have to wait a couple of seconds.

analitico.py
add the following line just after import time
time.sleep(20)

rc.local
edit the line "python3 /home/pi/Documents/PY/analitico.py &" into
"python3 /home/pi/Documents/PY/analitico.py > /home/pi/Documents/PY/analitico.log"

reboot your system (don't forget to disable analitico.service on your system)

I tested my setup with iot.eclipse.org
sorry that's not the problem

i have the same issue when i m running analitico as a daemon with "sudo service analitico restart"... analitico.sh is starting and the analitico.py is launched but it's not actually working (and the raspi is not rebooting)

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

Re: python script at startup

Mon Jul 03, 2017 5:16 pm

tpylkko wrote:I think it sounds like a good idea to start the mqtt as a background system service and run the script autostarted from the desktop, since then the script will run with the pi user and have the same session and environment as when it is run manually. But it will not work when /if you are using a Raspbian without graphicall desktop to begin with

You are daemon-reloading when you test changes to the service? If you post all the required data to get to the point where you are now, I could try to see if I can get it to work
i dont think i understand what you mean

Return to “General discussion”