Lucifer22334
Posts: 94
Joined: Wed Mar 06, 2019 7:36 am

Problem with closing the terminal processes

Thu Aug 29, 2019 12:11 pm

Hi all,

I have made a bash script which opens 3 terminals and runs 3 individual processes on them. I have also created a bash file which kills the process.

My application is on detection of RFID tag it will start the bash script which opens the 3 terminal and it will continuously read the card if I remove RFID card from the reader it should run the kill process file.

But after the start of 3 terminal, it doesn't go further. If I remove card it doesn't do anything. I am attaching the Image of the part where it gets stucks.

My RFID read/start and close code is:


Code: Select all

import RPi.GPIO as GPIO
from rfid_read import Read
import time
import os
from gtts import gTTS


class StartApp:

    def __init__(self):
        pass

    def main(self):
        val = True
        while True:
            name = Read()
            user = name.userid()
            if user != 'bye':
                if user in ["Nome", "Eric", "Lucifer"]:
                    if val is True:
                        val = False
                        tts = gTTS(
                            text='Welcome,to this magnificent Hotel. What can I do for you?', lang='en')
                        tts.save("welcome.mp3")
                        os.system("mpg321 welcome.mp3")
                        # os.system("sudo killall -9 sudo python autossh & sudo bash start.sh")
                        os.system("sudo bash start.sh")
                       
            else:
                if val is False:
                    val = True
                    # os.system("sudo killall -9 sudo python autossh")
                    os.system("sudo bash exit.sh")

             
    # def main(self):
    #     if self.reading_rfid() == "start":
    #         # os.system("sudo killall -9 sudo python autossh")
    #         tts = gTTS(
    #             text='Welcome,to this magnificent Hotel. What can I do for you?', lang='en')
    #         tts.save("welcome.mp3")
    #         os.system("mpg321 welcome.mp3")
    #         os.system("sudo bash start.sh")
    #     elif self.reading_rfid() == "exit":
    #         os.system("sudo bash exit.sh")


if __name__ == '__main__':
    pg = StartApp()
    # while True:
    pg.main()
So after starting the bash which is

Code: Select all

os.system("sudo bash start.sh")
It should go in else if card removed from the reader but it gets stuck.
Attachments
ERROR-min.png
ERROR-min.png (89.87 KiB) Viewed 954 times

User avatar
MrYsLab
Posts: 372
Joined: Mon Dec 15, 2014 7:14 pm
Location: Noo Joysey, USA

Re: Problem with closing the terminal processes

Thu Aug 29, 2019 11:44 pm

Looking at the posted code, the first iteration of the while loop will set val to False and execute the os.system commands. Then for the next iteration, assuming that the user did not enter bye, the code finds the valid name, and since val is False, the next iteration of the while loop is started. Other than the 'bye' test, there is no way to break out of this loop.

Lucifer22334
Posts: 94
Joined: Wed Mar 06, 2019 7:36 am

Re: Problem with closing the terminal processes

Fri Aug 30, 2019 5:27 am

MrYsLab wrote:
Thu Aug 29, 2019 11:44 pm
Looking at the posted code, the first iteration of the while loop will set val to False and execute the os.system commands. Then for the next iteration, assuming that the user did not enter bye, the code finds the valid name, and since val is False, the next iteration of the while loop is started. Other than the 'bye' test, there is no way to break out of this loop.
"bye" is sent when I lift the RFID card from the reader and I do it. But id doesn't take or validate. In short, after opening the terminals loop doesn't start again. I have put print just before the "bye" validation. So If I don't lift the card up it should print but it doesn't.

User avatar
MrYsLab
Posts: 372
Joined: Mon Dec 15, 2014 7:14 pm
Location: Noo Joysey, USA

Re: Problem with closing the terminal processes

Fri Aug 30, 2019 7:59 am

This is long shot, but try adding an ampersand to each os.system call. Here is an example:

Code: Select all

                        os.system("mpg321 welcome.mp3 &")
This will put each job in the background.

Lucifer22334
Posts: 94
Joined: Wed Mar 06, 2019 7:36 am

Re: Problem with closing the terminal processes

Fri Aug 30, 2019 9:23 am

MrYsLab wrote:
Fri Aug 30, 2019 7:59 am
This is long shot, but try adding an ampersand to each os.system call. Here is an example:

Code: Select all

                        os.system("mpg321 welcome.mp3 &")
This will put each job in the background.
By adding this it worked:

Code: Select all

tts.save("welcome.mp3")
                        os.system("mpg321 welcome.mp3")
                        # os.system("sudo killall -9 sudo python autossh & sudo bash start.sh")
                        os.system("sudo bash start.sh")
But there is now another issue after removing the card it runs the exit.sh script which kills that 3 terminals and after that it exits the program.
But I want it to again start reading the card means to check for the card. Is there any way for that?

Lucifer22334
Posts: 94
Joined: Wed Mar 06, 2019 7:36 am

Re: Problem with closing the terminal processes

Fri Aug 30, 2019 9:44 am

I think I have found the problem, In exit.sh I am using

Code: Select all

sudo killall -9 "sudo" "autossh" "python"
I tested it is also killing the main process from which the program runs.
I looked in this:

Code: Select all

ps -A | grep "python"
And found that there was 3 python process were running.
1. Main.py file [python main.py]
2. python tv.py which starts flask server
3. No idea after killing second third vanishes or If I kill 3rd first then second vanishes

and PID was different but the name was the same python and there was something:
1. PID pts/0 python [main.py file]
2. PID pts/1 python [tv.py]
3. PID pts/1 python [same as 2]

So is there any way to kil specific " pts/0" this one? And can I name the processes on starting them like:

I run :python main.py
So it creates a process name with python

For sudo bash startsample.sh
It creates a process name sudo

Can I give the custom process name? Like on running:
python main.py
It should create process Main?

User avatar
MrYsLab
Posts: 372
Joined: Mon Dec 15, 2014 7:14 pm
Location: Noo Joysey, USA

Re: Problem with closing the terminal processes

Fri Aug 30, 2019 12:24 pm

Take a look at psutils https://psutil.readthedocs.io/en/latest/#processes. Perhaps there is something there that will help you solve your problem.

Lucifer22334
Posts: 94
Joined: Wed Mar 06, 2019 7:36 am

Re: Problem with closing the terminal processes

Fri Aug 30, 2019 12:54 pm

MrYsLab wrote:
Fri Aug 30, 2019 12:24 pm
Take a look at psutils https://psutil.readthedocs.io/en/latest/#processes. Perhaps there is something there that will help you solve your problem.
OK, Thanks. I will read the doc and will try to find a solution if not found I will ping here.

Lucifer22334
Posts: 94
Joined: Wed Mar 06, 2019 7:36 am

Re: Problem with closing the terminal processes

Fri Aug 30, 2019 1:02 pm

Do you know how to get "pts/0" this from: PID pts/0 python [main.py file] ?

Aydan
Posts: 692
Joined: Fri Apr 13, 2012 11:48 am
Location: Germany, near Lake Constance

Re: Problem with closing the terminal processes

Fri Aug 30, 2019 1:09 pm

I see you are using os.system.
I'd suggest using subprocess instead.
This way you can collect the process objects into a list, and then signal them when you're done.

I'd also suggest looking at e.g. espeak for TTS so you don#t have to save the audio file before playing it.

Regards
Aydan

Lucifer22334
Posts: 94
Joined: Wed Mar 06, 2019 7:36 am

Re: Problem with closing the terminal processes

Fri Aug 30, 2019 1:40 pm

Aydan wrote:
Fri Aug 30, 2019 1:09 pm
I see you are using os.system.
I'd suggest using subprocess instead.
This way you can collect the process objects into a list, and then signal them when you're done.

I'd also suggest looking at e.g. espeak for TTS so you don#t have to save the audio file before playing it.

Regards
Aydan
Ok, so I should use subprocess inseated os.system to call sudo bash start.sh and sudo bash start.sh.
But in the start.sh file there are commands to run that 3 terminal with the individual process so How will I find which process is of which?
As I said above I am getting 3 python process running and only one I need to kill so how can I get that pts/0? So I can kill process according to it.

And as for espeak I will look into it how it works and everything. Hope it is not slow then this gTTS.

Lucifer22334
Posts: 94
Joined: Wed Mar 06, 2019 7:36 am

Re: Problem with closing the terminal processes

Fri Aug 30, 2019 1:42 pm

MrYsLab wrote:
Fri Aug 30, 2019 12:24 pm
Take a look at psutils https://psutil.readthedocs.io/en/latest/#processes. Perhaps there is something there that will help you solve your problem.
Do you know how to get "pts/0" this from: PID pts/0 python [main.py file] ?

User avatar
MrYsLab
Posts: 372
Joined: Mon Dec 15, 2014 7:14 pm
Location: Noo Joysey, USA

Re: Problem with closing the terminal processes

Fri Aug 30, 2019 1:59 pm

Try :

Code: Select all

ps -ft pts/0
Here is the output after opening several windows using Popen on my system

Code: Select all

pi@BanyanBot:~ $ ps -ft pts/0
UID        PID  PPID  C STIME TTY          TIME CMD
pi         942   890  0 09:52 pts/0    00:00:00 bash
pi        1028   942  2 09:53 pts/0    00:00:11 /usr/bin/python3 /usr/local/bin/bls -f exp1.csv
pi        1029  1028  0 09:53 pts/0    00:00:00 /usr/bin/python3 /usr/local/bin/backplane
pi        1034  1028  0 09:53 pts/0    00:00:00 xterm -e monitor
pi        1036  1028  0 09:53 pts/0    00:00:00 xterm -e python3 ../../test_fixtures/exp_pro_gui.py
pi        1038  1028  0 09:53 pts/0    00:00:00 xterm -e coverage run --branch  --append --omit=/usr/lib*,/usr/local* ../../banyan_assets/exp_pro_gateway.py
:

Lucifer22334
Posts: 94
Joined: Wed Mar 06, 2019 7:36 am

Re: Problem with closing the terminal processes

Fri Aug 30, 2019 2:26 pm

MrYsLab wrote:
Fri Aug 30, 2019 1:59 pm
Try :

Code: Select all

ps -ft pts/0
Here is the output after opening several windows using Popen on my system

Code: Select all

pi@BanyanBot:~ $ ps -ft pts/0
UID        PID  PPID  C STIME TTY          TIME CMD
pi         942   890  0 09:52 pts/0    00:00:00 bash
pi        1028   942  2 09:53 pts/0    00:00:11 /usr/bin/python3 /usr/local/bin/bls -f exp1.csv
pi        1029  1028  0 09:53 pts/0    00:00:00 /usr/bin/python3 /usr/local/bin/backplane
pi        1034  1028  0 09:53 pts/0    00:00:00 xterm -e monitor
pi        1036  1028  0 09:53 pts/0    00:00:00 xterm -e python3 ../../test_fixtures/exp_pro_gui.py
pi        1038  1028  0 09:53 pts/0    00:00:00 xterm -e coverage run --branch  --append --omit=/usr/lib*,/usr/local* ../../banyan_assets/exp_pro_gateway.py
:
If I have open multiple python processess then I get pts/0 for main process and pts/1 or 2 for child processes. So if I want to kill the child processes using pts then how can I do it?

Aydan
Posts: 692
Joined: Fri Apr 13, 2012 11:48 am
Location: Germany, near Lake Constance

Re: Problem with closing the terminal processes

Fri Aug 30, 2019 2:34 pm

Lucifer22334 wrote:
Fri Aug 30, 2019 1:40 pm
Ok, so I should use subprocess inseated os.system to call sudo bash start.sh and sudo bash start.sh.
But in the start.sh file there are commands to run that 3 terminal with the individual process so How will I find which process is of which?

As I said above I am getting 3 python process running and only one I need to kill so how can I get that pts/0? So I can kill process according to it.
In that case I woud not use start.sh or at least remove the process you need to kill from start.sh and start that directly with subprocess.
I'm actually a bit confused why you start python processes via a shell script which you start from a python script.
Lucifer22334 wrote:
Fri Aug 30, 2019 1:40 pm
And as for espeak I will look into it how it works and everything. Hope it is not slow then this gTTS.
espeak works locally, no need for a network connection. It might sound a bit more computery though.

Regards
Aydan

User avatar
MrYsLab
Posts: 372
Joined: Mon Dec 15, 2014 7:14 pm
Location: Noo Joysey, USA

Re: Problem with closing the terminal processes

Fri Aug 30, 2019 2:45 pm

If you want an easy way to see all the process names mapped to pids, psutil can help. Here is an example:

Code: Select all

import psutil

for proc in psutil.process_iter(attrs=['pid', 'name', 'username']):
    print(proc.info)
It will place each entry into a dictionary. Here is a sample:

Code: Select all

{'username': 'afy', 'pid': 8950, 'name': 'backplane'}


Lucifer22334
Posts: 94
Joined: Wed Mar 06, 2019 7:36 am

Re: Problem with closing the terminal processes

Sat Aug 31, 2019 6:25 am

Aydan wrote:
Fri Aug 30, 2019 2:34 pm
Lucifer22334 wrote:
Fri Aug 30, 2019 1:40 pm
Ok, so I should use subprocess inseated os.system to call sudo bash start.sh and sudo bash start.sh.
But in the start.sh file there are commands to run that 3 terminal with the individual process so How will I find which process is of which?

As I said above I am getting 3 python process running and only one I need to kill so how can I get that pts/0? So I can kill process according to it.
In that case I woud not use start.sh or at least remove the process you need to kill from start.sh and start that directly with subprocess.
I'm actually a bit confused why you start python processes via a shell script which you start from a python script.
Lucifer22334 wrote:
Fri Aug 30, 2019 1:40 pm
And as for espeak I will look into it how it works and everything. Hope it is not slow then this gTTS.
espeak works locally, no need for a network connection. It might sound a bit more computery though.

Regards
Aydan
Actually I tried using subprocess but it is sequential. I have a python flask server script to run, a terminal command to port forward and a bash file to start Alexa sample app.
SO when I used subprocess to run these things it gets stuck and doesn't go further after flask server created. All these three processes should be running simultaneously and continuously until I remove the RFID card. But it was not working so... I moved to create 3 terminal process. I am looking forward to converting my localhost IP to global one so I don't need to open port forwarding terminal only Alexa and Flask. And also Finding solution If possible to remove flask.

And I cannot use espeak due to its computerized voice I need almost like natural so gTTS will do in the meantime.

Lucifer22334
Posts: 94
Joined: Wed Mar 06, 2019 7:36 am

Re: Problem with closing the terminal processes

Sat Aug 31, 2019 6:25 am

MrYsLab wrote:
Fri Aug 30, 2019 2:45 pm
If you want an easy way to see all the process names mapped to pids, psutil can help. Here is an example:

Code: Select all

import psutil

for proc in psutil.process_iter(attrs=['pid', 'name', 'username']):
    print(proc.info)
It will place each entry into a dictionary. Here is a sample:

Code: Select all

{'username': 'afy', 'pid': 8950, 'name': 'backplane'}

OK, I will try and Ping Here.

Aydan
Posts: 692
Joined: Fri Apr 13, 2012 11:48 am
Location: Germany, near Lake Constance

Re: Problem with closing the terminal processes

Sat Aug 31, 2019 1:11 pm

Lucifer22334 wrote:
Sat Aug 31, 2019 6:25 am
Actually I tried using subprocess but it is sequential. I have a python flask server script to run, a terminal command to port forward and a bash file to start Alexa sample app.
Subprocess is not inherently sequential. Depends how you use it.
I usually use subprocess.popen. If your child process generates a lot of data (multiple kBytes) on stdout you need to read the processes stout pipe or the process will stall.
I know this works in parallel because I've used it that way already.

Regards
Aydan

Lucifer22334
Posts: 94
Joined: Wed Mar 06, 2019 7:36 am

Re: Problem with closing the terminal processes

Sun Sep 01, 2019 3:28 pm

Aydan wrote:
Sat Aug 31, 2019 1:11 pm
Lucifer22334 wrote:
Sat Aug 31, 2019 6:25 am
Actually I tried using subprocess but it is sequential. I have a python flask server script to run, a terminal command to port forward and a bash file to start Alexa sample app.
Subprocess is not inherently sequential. Depends how you use it.
I usually use subprocess.popen. If your child process generates a lot of data (multiple kBytes) on stdout you need to read the processes stout pipe or the process will stall.
I know this works in parallel because I've used it that way already.

Regards
Aydan
Ok will do that. And I will ping you once done or found some errors.

Return to “Python”