meminoglu
Posts: 29
Joined: Sun Mar 05, 2017 3:15 pm

How can i run 2 python instance which has infinity loop at start up

Sat Nov 10, 2018 3:27 pm

I need to run 2 python instance at run time. I have edited /etc/rc.local file but it doesnt work, because they don't return anything, and i don't want to return it. Because one of the my file is flask file, which is web service, it must run all the time, another is, mifare card reader program (MFRC522-python/Read.py) it has while loop so, it never returns..

i need both files run at start up. i edited start up like (/etc/rc.local)

sudo python /home/pi/MFRC522-python/Read.py & sudo python /home/pi/Flask/Webservis.py

When i try this, web servis run, Read.py doesn't, when i tried other variation
sudo python /home/pi/Flask/Webservis.py & sudo python /home/pi/MFRC522-python/Read.py

Web Servis stops, Read.py is running. How can i solve this problem?

User avatar
thagrol
Posts: 943
Joined: Fri Jan 13, 2012 4:41 pm
Location: Darkest Somerset, UK
Contact: Website

Re: How can i run 2 python instance which has infinity loop at start up

Sat Nov 10, 2018 3:56 pm

meminoglu wrote:
Sat Nov 10, 2018 3:27 pm
sudo python /home/pi/MFRC522-python/Read.py & sudo python /home/pi/Flask/Webservis.py
I'm not sure that's a valid bash command. Also, as /etc/rc.local is run by root you don't need "sudo"

Try splitting those commands over two lines, giving the full path to "python", capturing any output and starting both as background jobs e.g.

Code: Select all

/usr/bin/python /home/pi/MFRC522-python/Read.py >/tmp/Read.log 2>&1 &
/usr/bin/python /home/pi/Flask/Webservis.py >/tmp/Webservis.log 2>&1 &
If one, or both, fail to start look in the log file for error messages.

Or create your own systemd service instead (don't ask me how. I'm just saying it before someone else does ;))

Oh, and how are you checking that both process are(not) running? Looking for output? Via somethinglike 'ps aux|grep python'?
Note to self: don't feed the trolls
If you believe "L'enfer, c'est les autres" (Hell is other people) have you considered that it may be of your own making?

klricks
Posts: 5919
Joined: Sat Jan 12, 2013 3:01 am
Location: Grants Pass, OR, USA
Contact: Website

Re: How can i run 2 python instance which has infinity loop at start up

Sat Nov 10, 2018 3:59 pm

meminoglu wrote:
Sat Nov 10, 2018 3:27 pm
I need to run 2 python instance at run time. I have edited /etc/rc.local file but it doesnt work, because they don't return anything, and i don't want to return it. Because one of the my file is flask file, which is web service, it must run all the time, another is, mifare card reader program (MFRC522-python/Read.py) it has while loop so, it never returns..

i need both files run at start up. i edited start up like (/etc/rc.local)

sudo python /home/pi/MFRC522-python/Read.py & sudo python /home/pi/Flask/Webservis.py

When i try this, web servis run, Read.py doesn't, when i tried other variation
sudo python /home/pi/Flask/Webservis.py & sudo python /home/pi/MFRC522-python/Read.py

Web Servis stops, Read.py is running. How can i solve this problem?
The & means start the program in the background...... It does not mean start one and then start another. So your second command is lilely being ignored.
Run them on 2 separate lines:

Code: Select all

python /home/pi/Flask/Webservis.py & 
python /home/pi/MFRC522-python/Read.py &
exit 0
Note that rc.local will probably try to start each program at near the same time. If that matters then start both programs in rc.local using a single bash script.
Unless specified otherwise my response is based on the latest and fully updated Raspbian Stretch w/ Desktop OS.

meminoglu
Posts: 29
Joined: Sun Mar 05, 2017 3:15 pm

Re: How can i run 2 python instance which has infinity loop at start up

Sat Nov 10, 2018 5:20 pm

I solved problem with run as a service, maybe this post will help others;

i created two files;

sudo nano /lib/systemd/system/WEB.service
sudo nano /lib/systemd/system/READ.service

i modified both files with nano editor like this;

[Unit]
Description=My Sample Service
After=multi-user.target

[Service]
Type=idle
ExecStart=/usr/bin/python [Place here your file dir]

[Install]
WantedBy=multi-user.target

save your file; after that give permission both files;

sudo chmod 644 /lib/systemd/system/WEB.service
sudo chmod 644 /lib/systemd/system/READ.service

and configure it for both files;

sudo systemctl daemon-reload
sudo systemctl enable WEB.service

source; https://www.dexterindustries.com/howto/ ... t-startup/

Return to “Advanced users”