LinuxQuestions.org
Download your favorite Linux distribution at LQ ISO.
Go Back   LinuxQuestions.org > Forums > Linux Forums > Linux - Software
User Name
Password
Linux - Software This 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


Reply
  Search this Thread
Old 11-26-2016, 09:44 AM   #16
morganjeff7272
Member
 
Registered: Dec 2015
Location: Oklahoma City
Distribution: Ubuntu
Posts: 38

Original Poster
Blog Entries: 2

Rep: Reputation: Disabled

Quote:
Originally Posted by BW-userx View Post
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....
 
Old 11-26-2016, 10:26 AM   #17
pan64
LQ Guru
 
Registered: Mar 2012
Location: Hungary
Distribution: debian/ubuntu/suse ...
Posts: 13,617

Rep: Reputation: 4367Reputation: 4367Reputation: 4367Reputation: 4367Reputation: 4367Reputation: 4367Reputation: 4367Reputation: 4367Reputation: 4367Reputation: 4367Reputation: 4367
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.
 
Old 11-26-2016, 10:39 AM   #18
BW-userx
LQ Guru
 
Registered: Sep 2013
Location: Somewhere in my head.
Distribution: Slackware FreeBSD Win10 MX
Posts: 9,637

Rep: Reputation: 2084Reputation: 2084Reputation: 2084Reputation: 2084Reputation: 2084Reputation: 2084Reputation: 2084Reputation: 2084Reputation: 2084Reputation: 2084Reputation: 2084
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.
 
Old 11-26-2016, 10:42 AM   #19
morganjeff7272
Member
 
Registered: Dec 2015
Location: Oklahoma City
Distribution: Ubuntu
Posts: 38

Original Poster
Blog Entries: 2

Rep: Reputation: Disabled
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
 
Old 11-26-2016, 11:01 AM   #20
pan64
LQ Guru
 
Registered: Mar 2012
Location: Hungary
Distribution: debian/ubuntu/suse ...
Posts: 13,617

Rep: Reputation: 4367Reputation: 4367Reputation: 4367Reputation: 4367Reputation: 4367Reputation: 4367Reputation: 4367Reputation: 4367Reputation: 4367Reputation: 4367Reputation: 4367
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...
 
Old 11-26-2016, 07:48 PM   #21
morganjeff7272
Member
 
Registered: Dec 2015
Location: Oklahoma City
Distribution: Ubuntu
Posts: 38

Original Poster
Blog Entries: 2

Rep: Reputation: Disabled
Thank you I will look into that is that somewhat like system.out.println("") in java I just need to print(Exception)
 
Old 11-27-2016, 01:20 PM   #22
morganjeff7272
Member
 
Registered: Dec 2015
Location: Oklahoma City
Distribution: Ubuntu
Posts: 38

Original Poster
Blog Entries: 2

Rep: Reputation: Disabled
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?
 
Old 11-27-2016, 01:32 PM   #23
BW-userx
LQ Guru
 
Registered: Sep 2013
Location: Somewhere in my head.
Distribution: Slackware FreeBSD Win10 MX
Posts: 9,637

Rep: Reputation: 2084Reputation: 2084Reputation: 2084Reputation: 2084Reputation: 2084Reputation: 2084Reputation: 2084Reputation: 2084Reputation: 2084Reputation: 2084Reputation: 2084
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.

Last edited by BW-userx; 11-27-2016 at 01:38 PM.
 
Old 11-27-2016, 02:37 PM   #24
morganjeff7272
Member
 
Registered: Dec 2015
Location: Oklahoma City
Distribution: Ubuntu
Posts: 38

Original Poster
Blog Entries: 2

Rep: Reputation: Disabled
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.
 
Old 11-27-2016, 04:44 PM   #25
BW-userx
LQ Guru
 
Registered: Sep 2013
Location: Somewhere in my head.
Distribution: Slackware FreeBSD Win10 MX
Posts: 9,637

Rep: Reputation: 2084Reputation: 2084Reputation: 2084Reputation: 2084Reputation: 2084Reputation: 2084Reputation: 2084Reputation: 2084Reputation: 2084Reputation: 2084Reputation: 2084
Quote:
Originally Posted by morganjeff7272 View Post
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.
 
Old 11-27-2016, 06:56 PM   #26
morganjeff7272
Member
 
Registered: Dec 2015
Location: Oklahoma City
Distribution: Ubuntu
Posts: 38

Original Poster
Blog Entries: 2

Rep: Reputation: Disabled
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....
 
Old 11-27-2016, 07:07 PM   #27
BW-userx
LQ Guru
 
Registered: Sep 2013
Location: Somewhere in my head.
Distribution: Slackware FreeBSD Win10 MX
Posts: 9,637

Rep: Reputation: 2084Reputation: 2084Reputation: 2084Reputation: 2084Reputation: 2084Reputation: 2084Reputation: 2084Reputation: 2084Reputation: 2084Reputation: 2084Reputation: 2084
Quote:
Originally Posted by morganjeff7272 View Post
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....
got a get paid.

googled this "freeing python memory" and got this

Code:
According to Python Official Documentation, 
you can force the Garbage Collector to release unreferenced memory with gc.collect()
that reminds me of Java -- I've never actually looked into python until now. But that garbage collector reminds me of Java.

Last edited by BW-userx; 11-27-2016 at 07:10 PM.
 
Old 11-27-2016, 09:25 PM   #28
morganjeff7272
Member
 
Registered: Dec 2015
Location: Oklahoma City
Distribution: Ubuntu
Posts: 38

Original Poster
Blog Entries: 2

Rep: Reputation: Disabled
I was thinking of trying that too but it is supposed to clean up by itself just like java, so ??!!??
 
Old 11-28-2016, 01:48 AM   #29
pan64
LQ Guru
 
Registered: Mar 2012
Location: Hungary
Distribution: debian/ubuntu/suse ...
Posts: 13,617

Rep: Reputation: 4367Reputation: 4367Reputation: 4367Reputation: 4367Reputation: 4367Reputation: 4367Reputation: 4367Reputation: 4367Reputation: 4367Reputation: 4367Reputation: 4367
Code:
        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).
 
Old 11-28-2016, 08:34 AM   #30
BW-userx
LQ Guru
 
Registered: Sep 2013
Location: Somewhere in my head.
Distribution: Slackware FreeBSD Win10 MX
Posts: 9,637

Rep: Reputation: 2084Reputation: 2084Reputation: 2084Reputation: 2084Reputation: 2084Reputation: 2084Reputation: 2084Reputation: 2084Reputation: 2084Reputation: 2084Reputation: 2084
Quote:
Originally Posted by morganjeff7272 View Post
I was thinking of trying that too but it is supposed to clean up by itself just like java, so ??!!??
Like @pan64 said giving you some code, and it does not hurt to call for the garbage collector. Just like in Java.
 
  


Reply


Thread Tools Search this Thread
Search this Thread:

Advanced Search

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off



Similar Threads
Thread Thread Starter Forum Replies Last Post
Seek a python script through zabbix api to list hosts? 624867243@qq.com Linux - Server 3 02-27-2016 11:01 AM
python displaying list contents in scrollable window error bloodyscript Programming 2 09-25-2011 02:38 AM
Hung script as a result of bg process Poki Linux - Newbie 1 05-01-2009 06:01 PM
Shell script to check process and restart if "hung" georage Programming 5 10-29-2008 08:10 PM
killing a hung up process with a script lockejr Linux - General 2 04-14-2006 12:40 AM

LinuxQuestions.org > Forums > Linux Forums > Linux - Software

All times are GMT -5. The time now is 04:36 PM.

Main Menu
Advertisement
My LQ
Write for LQ
LinuxQuestions.org is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
Syndicate
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
Facebook: linuxquestions Google+: linuxquestions
Open Source Consulting | Domain Registration