Results 1 to 6 of 6

Thread: Check if running

  1. #1
    Join Date
    Oct 2006
    Location
    New York
    Beans
    1,118
    Distro
    Xubuntu 12.10 Quantal Quetzal

    Check if running

    I'm trying to use start stop daemon to check if a python script is running. The catch is if I execute
    ./myscript.py
    what's actually running is /usr/bin/python, and I might want multiple instances of python running. So I'm interested in getting start-stop-daemon to specifically check for a python which is running a certain script. Is there a way to do this, or another clever way? Sadly pid/lock files don't work well here because the daemon tends to terminate unexpectedly/filesystem issues (not my server).
    xubuntu minimal, extensive experience, lshw: http://goo.gl/qCCtn
    blog: http://goo.gl/yLg78
    Linux viruses: http://goo.gl/6OCKA

  2. #2
    Join Date
    Apr 2007
    Beans
    617
    Distro
    Ubuntu 12.04 Precise Pangolin

    Re: Check if running

    you could process the output of ps -e -F through grep looking for your program

  3. #3
    Join Date
    Oct 2007
    Location
    Chennai, India
    Beans
    3,804
    Distro
    Ubuntu Development Release

    Re: Check if running

    You can use the "-c" option of top to display the actual commandline used for the process.

    Eg,
    Code:
    top -c -b -n 1|grep python
    should give you a list of running python processes, INCLUDING the command line used to initiate the process. -c = show command line, -b = batch mode (non-interactive) -n 1 (run only once).

    From that point, I guess it's a simple matter to extract the PID, though I failed miserably when using the "cut" command (apparently, "top" uses multiple spaces to align fields).

    Please post back if you need more information.
    Cheers,PRShah
    Make your own: Ubuntu, Kubuntu, Xubuntu, Mythbuntu All-in-One Live DVD
    "I never make mistakes; I thought I did, once.. but I was wrong."

  4. #4
    Join Date
    Apr 2012
    Beans
    7,256

    Re: Check if running

    ... or just get the pid with

    Code:
    pgrep -f 'myscript.py'
    maybe? (the '-f' searches the whole command not just the process name - similar to the -c in top)

    For top, awk seems to work if cut is tricky:

    Code:
    top -cbn1 | awk '$NF ~ /myscript/ {print $1}'

  5. #5
    Join Date
    Oct 2006
    Location
    New York
    Beans
    1,118
    Distro
    Xubuntu 12.10 Quantal Quetzal

    Re: Check if running

    Thanks, I switched to a mini-script with top & grep, but it seems like it should be unnecessary.

    I'll try pgrep as that's one I haven't looked at.

    Unrelated, but did mark as solved disappear again in the remodel? If not where is it hiding?
    xubuntu minimal, extensive experience, lshw: http://goo.gl/qCCtn
    blog: http://goo.gl/yLg78
    Linux viruses: http://goo.gl/6OCKA

  6. #6
    Join Date
    Apr 2012
    Beans
    7,256

    Re: Check if running

    I guess one caveat with pgrep is that afaik it won't distinguish between running and defunct processes - so if you really need to find running instances of your script you may need to do something more sophisticated - see here for example http://unix.stackexchange.com/questi...unct-processes

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •