Script in python... hung window gui.. not in process list
Linux - SoftwareThis forum is for Software issues.
Having a problem installing a new program? Want to know which application is best for the job? Post your question in this forum.
Notices
Welcome to LinuxQuestions.org, a friendly and active Linux Community.
You are currently viewing LQ as a guest. By joining our community you will have the ability to post topics, receive our newsletter, use the advanced search, subscribe to threads and access many other special features. Registration is quick, simple and absolutely free. Join our community today!
Note that registered members see fewer ads, and ContentLink is completely disabled once you log in.
If you have any problems with the registration process or your account login, please contact us. If you need to reset your password, click here.
Having a problem logging in? Please visit this page to clear all LQ-related cookies.
Introduction to Linux - A Hands on Guide
This guide was created as an overview of the Linux Operating System, geared toward new users as an exploration tour and getting started guide, with exercises at the end of each chapter.
For more advanced trainees it can be a desktop reference, and a collection of the base knowledge needed to proceed with system and network administration. This book contains many real life examples derived from the author's experience as a Linux system and network administrator, trainer and consultant. They hope these examples will help you to get a better understanding of the Linux system and that you feel encouraged to try out things on your own.
Click Here to receive this Complete Guide absolutely free.
can I ask if you still have this bit of code in there somewhere too?
Code:
for p in psutil.process_iter():
if(p.name() == p_name):
"""Still Found in Process Running""" # this will return false if name is still found in process list, So yes here I could add code If needed to
#reiterate through the kill process.
ret = False
return ret
because to me that reads if one is still running you set it to false then return it false, even though it is true. Because of the test states if one is still running then set ret = false then returns that value(false).
I am not knowing how Python uses their code blocks not seeing any open and close brackets. Nor do know if python lets one use empty, then do nothing, if statements.
one would actually have to sit want watch it within at least an hours time to see how it actually works, or come back within a five minute time frame before and after it should execute every 30 minutes to send that kill command to see how long them windows stay open. Perhaps even check the return value from that last statement. Logically it should still read true because one is still active.
but if what you showed in your last post, replaces everything you showed before hand then my point is diffidently mute.
You are correct that if it is found then the if statement returns true. or rather the inside of the parens equal each other I want to return false with my function cause if process name is found then I did not kill it....
once again, the code above (post #16) is incorrect in python because of the invalid indentation. Please post your actual script within [code][/code] tags.
Distribution: Slackware (current), FreeBSD, Win10, It varies
Posts: 9,952
Rep:
disclaimer: The Python code in this post is not meant to be copy and pasted into any file for testing purposes. Due to its nature. One has to have a working knowledge of python in order to test this code within this post.
Quote:
You may also have a mix of tabs and spaces in your file. By using correct indentation. Python is whitespace aware, so you need to follow its indentation guidlines for blocks or you'll get indentation errors. Turn on visible whitespace in whatever editor you are using and turn on replace tabs with spaces.
I have been running the current code on the box for 18 hours now no problems, so it must have been the above change that I made, Ill bold them in the code below.
Code:
"""open web page\"entropiapartners\", login, confirm login,
find and click the music link, open the web radio and leave
running"""
import time
from selenium import webdriver
from selenium.webdriver.chrome import service
from mypython.proj.reuseable import process
def main():
while True:
body()
def body():
"""This was all in main and then just recall main at end.
open web page\"entropiapartners\", login, confirm login,
find and click the music link, open the web radio and leave
running"""
"""if (not process.exists('chromedriver')):
#Launch /home/ep/bash/chromeBrowserDrvr
#IF CHROMEDRIVER do nothing we need to kill it then restart it
#process.start_sh('/home/ep/bash/chromeBrowserDrvr.sh')
#time.sleep(3)
pass
else:
#chromedriver is started kill it and restart it
process.kill('chromedriver')
time.sleep(3)
pass"""
#lets kill chromedriver
try:
process.kill('chromedriver')
except Exception:
pass
time.sleep(3)
#lets kill chromium-brose
try:
process.kill('chromium-browse')
except Exception:
pass
time.sleep(3)
#now start everything
process.start_sh('/home/ep/bash/chromeBrowserDrvr.sh')
time.sleep(3)
capabilities = {'chrome.binary': '/usr/bin/google-chrome'}
driver = webdriver.Remote('http://127.0.0.1:9515',capabilities)
driver.get('http://www.some url.com');
time.sleep(10) # Let the user actually see something!
user_box = driver.find_element_by_name('e')
user_box.send_keys('username')
pass_box = driver.find_element_by_name('p')
pass_box.send_keys('password')
pass_box.submit()
time.sleep(10) # Let the user actually see something!
close_button = driver.find_element_by_class_name('TdOffline')
close_button.click()
time.sleep(10) # Let the user actually see something!
music_link = driver.find_element_by_partial_link_text('music')
music_link.click()
time.sleep(10) # Let the user actually see something!
music_link = driver.find_element_by_id('SN_Button')
music_link.click()
time.sleep(3600)
if __name__ == "__main__":
main()
Code:
"""Defined functions to manipulate processes
Find, Kill, Start Processes"""
import psutil
import os
import subprocess
def exists(p_name):
"""Returns True if p_name is found to be at least one running process
Returns False if p_name is not found to be a running process"""
ret = False
for p in psutil.process_iter():
if(p.name() == p_name):
ret = True
return ret
def kill(p_name):
"""Returns False if p_name is found and can't be killed
Returns True if p_name is not found
Returns True if p_name is found and killed"""
ret = True
for p in psutil.process_iter():
if(p.name() == p_name):
"""Friendly Kill"""
p.terminate()
for p in psutil.process_iter():
if(p.name() == p_name):
"""Force Kill"""
p.kill()
for p in psutil.process_iter():
if(p.name() == p_name):
"""Still Found in Process Running"""
ret = False
return ret
def kill_chrome():
"""tries to kill chrome-sandbox
tries to kill chromium
Returns True if no chrome-sandbox and chromium processes still running
Returns False if chrome-sandbox or chromium processes still running"""
ret = True
kill('chrome-sandbox')
kill('chromium')
ret = not exists('chrome-sandbox') or not exists('chromium')
return ret
def start_sh(p_file):
"""Starts a process and returns to python without waiting for command to
finish.Returns the pid of new process"""
return subprocess.Popen(['sh', p_file]).pid
def wait_sh(p_file):
"""Starts a process and waits for command to complete before returning
Returns return code."""
return subprocess.call(['sh', p_file])
def test():
"""test -replaces main
use psutil.test()"""
print('Output from psutil.test()')
psutil.test()
print('Output of simplified list')
for p in psutil.process_iter():
print(p.name())
if __name__ == "__main__":
test()
There you go
Last edited by morganjeff7272; 11-26-2016 at 10:44 AM.
Reason: hide pass and user
one thing I can suggest you: print the exception instead of just suppressing it (by pass), probably that will give you an answer - next time if you find a window...
so in each of my try kill process statements I added Print(Exception) to replace the pass statement.
Nothing is ever coming up. So everything is working, However each iteration of the loop executing the body function the python process is gathering 3 MB of memory. Do I need to explicitly delete everything at the end of my body Function?
Distribution: Slackware (current), FreeBSD, Win10, It varies
Posts: 9,952
Rep:
I changed your code a little bit, it just didn't look right to me, but the actual syntax you'll have to check. If you decide to use it.
Code:
import psutil
import os
import subprocess
def exists(p_name):
"""Returns True if p_name is found to be at least one running process
Returns False if p_name is not found to be a running process"""
ret = False
for p in psutil.process_iter():
if(p.name() == p_name):
ret = True
return ret
def kill(p_name):
"""Returns False if p_name is found and can't be killed
Returns True if p_name is not found
Returns True if p_name is found and killed"""
ret = True
for p in psutil.process_iter():
if(p.name() == p_name):
"""Friendly Kill"""
p.terminate()
ret = false
"" if the process did not get killed by the
first test then test for ret = true instead""
for p in psutil.process_iter():
if(ret == true ):
"""Force Kill"""
p.kill()
ret = false"" I'd get rid of this code, it is over kill""
#for p in psutil.process_iter():
# if(p.name() == p_name):
# """Still Found in Process Running""" "" if ret still true then throw an error
then you can handle it elsewhere in your code""
if ( ret == true ):
print("NOT DEAD YET")return ret
def kill_chrome():
"""tries to kill chrome-sandbox
tries to kill chromium
Returns True if no chrome-sandbox and chromium processes still running
Returns False if chrome-sandbox or chromium processes still running"""
ret = True
kill('chrome-sandbox')
kill('chromium')
ret = not exists('chrome-sandbox') or not exists('chromium')
return ret
def start_sh(p_file):
"""Starts a process and returns to python without waiting for command to
finish.Returns the pid of new process"""
return subprocess.Popen(['sh', p_file]).pid
def wait_sh(p_file):
"""Starts a process and waits for command to complete before returning
Returns return code."""
return subprocess.call(['sh', p_file])
def test():
"""test -replaces main
use psutil.test()"""
print('Output from psutil.test()')
psutil.test()
print('Output of simplified list')
for p in psutil.process_iter():
print(p.name())
if __name__ == "__main__":
test()
if it is not freeing up memory after the kill yeah free it, if you can.
in all my recent object oriented programming and logic classes we return true always unless something fails then we return false, so we always set to true then unless we find a reason to change it to false the final stetement for p in psutil sets it to false, and or something went wrong.
That is why i set the ret to true then only 1 set to false statement instead of 1 set to false statement and then three set to true statemnents and a set to false statement.
for cleanliness or proper coding I would like to figure out how to kill the procexsses myself in my own code instead of using psutil, but I have not been able to find out how to do it. Anyways I also wrote it this way because of three days of testing the psutil kill functions and how they returned. It was weird becuase at times it would kill the process but return false. But the next call of p in process_iter() would return a flase IE: not found...
So I kill it friendly and test the iteration again if found I dont need to see if my ret is false because I already found the name of the process in the iter list, We then go kill them I forget the actual unix command it calls but it is now a forcefull kill instead of a unix asking the process to quit it is now killing it, Which in my research previously could orphan the GUI windows.
So then I ask psutil again for the process_iter() list to see if the name of the process is found again It should never get there but you no programming and OS behaviour. If the name is still in the list I return false otherwise I never change the ret from the initial statement and following my comments on my logic...
Returns true if no process found, or if it actually successfully kills it but there is no real test for this unless I logically test, did I find the process and is it gone..more code.
Anyways Yes it could be done differently and probably cleaner but the testing of the ret is a un needed test because we already got the p in iteration() list back and test it for the name to kill
The memory is released from killing the processes chromium and chromium driver. what is growing is the python3 process.
Distribution: Slackware (current), FreeBSD, Win10, It varies
Posts: 9,952
Rep:
Quote:
Originally Posted by morganjeff7272
in all my recent object oriented programming and logic classes we return true always unless something fails then we return false, so we always set to true then unless we find a reason to change it to false the final statement for p in psutil sets it to false, and or something went wrong.
Using the boolean rule. true (1) and false (0). return 0 indicates executed without error which is false. If error occurs it returns a value of true (1). or any other number you want to use besides 0 (zero) in a function. To indicate an error has taken place.
Code:
if (program_executed_fine)
return 0; (false)
else if (program_had_error)
return 1; (true)
Quote:
That is why i set the ret to true then only 1 set to false statement instead of 1 set to false statement and then three set to true statements and a set to false statement.
Quote:
for cleanliness or proper coding I would like to figure out how to kill the processes myself in my own code instead of using psutil, but I have not been able to find out how to do it.
It maybe that is why Python gives you that psutil to use as an api, or wrapper, or whatever it is they call that. you'd have to go look at the code within psutil to see how they do it. In order to learn to do what you just said you'd like to learn.
Quote:
Anyways I also wrote it this way because of three days of testing the psutil kill functions and how they returned. It was weird becuase at times it would kill the process but return false. But the next call of p in process_iter() would return a flase IE: not found...
So I kill it friendly and test the iteration again if found I dont need to see if my ret is false because I already found the name of the process in the iter list, We then go kill them I forget the actual unix command it calls but it is now a forcefull kill instead of a unix asking the process to quit it is now killing it, Which in my research previously could orphan the GUI windows.
Quote:
So then I ask psutil again for the process_iter() list to see if the name of the process is found again It should never get there but you no programming and OS behavior. If the name is still in the list I return false otherwise I never change the ret from the initial statement and following my comments on my logic...
Returns true if no process found, or if it actually successfully kills it but there is no real test for this unless I logically test, did I find the process and is it gone..more code.
Anyways Yes it could be done differently and probably cleaner but the testing of the ret is a un needed test because we already got the p in iteration() list back and test it for the name to kill
The memory is released from killing the processes chromium and chromium driver.
I agree, it was just a different way of doing the same thing. instead of setting ret to false in the end. doing it ahead of time then just using ret value instead of the class name.
But I did not actually think that through when I wrote that, it wasn't until this post when I through about it more I realized that mistake to actually check to see if it was indeed terminated before changing ret value.
Quote:
what is growing is the python3 process.
it does not free itself after running that code. I assume it is because that python program is still and is always running so it keeps itself in memory. And that memory is not released until that program is killed.
you could then place it in a cron job. then have cron call to run it every time you need it to run, then let it do what it does, then have it kill itself in the end. freeing up the memory, then rely on cron to start it up again.
So here is what Ill try then Ill set up a cron job to reun the python job every 35 minutes and run this job but let it die after the wait. Ive tried to let it die after the last function in the body method but it kills the windows and I do not get credit for what Im trying to wait for,,, radio plays for thirty minutes I get paid....
Distribution: Slackware (current), FreeBSD, Win10, It varies
Posts: 9,952
Rep:
Quote:
Originally Posted by morganjeff7272
So here is what Ill try then Ill set up a cron job to reun the python job every 35 minutes and run this job but let it die after the wait. Ive tried to let it die after the last function in the body method but it kills the windows and I do not get credit for what Im trying to wait for,,, radio plays for thirty minutes I get paid....
for t in <process list>:
try:
os.kill(t.pid, signal.SIGTERM)
except Exception, e:
<print exception if you want>
os.kill(t.pid, signal.SIGKILL)
the second kill is usually "successful".
You do not need to take care about memory, but you may try to use a profiler if you want to check what's happening (obviously you can call gc any time you want, especially before the long sleep).
LinuxQuestions.org is looking for people interested in writing
Editorials, Articles, Reviews, and more. If you'd like to contribute
content, let us know.