CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com
Results 1 to 4 of 4
  1. #1
    Join Date
    Oct 2012
    Location
    New Zealand
    Posts
    11

    Cool How do I find out if a program is running in "C" (this is linux)

    In bash I can find out if a program is running and do stuff if it is running or not running

    the code in bash

    Code:
    if [ `pstree | grep -o "progname" | head -n 1` = "progname" ]; then
    
    echo "program is running"
    
    else
    
    echo "program is not running"
    
    fi
    How do I do the same thing in "C"

    Thanks
    Last edited by n_techo; October 12th, 2012 at 01:31 AM.

  2. #2
    Join Date
    Oct 2006
    Location
    Sweden
    Posts
    3,654

    Re: How do I find out if a program is running in "C" (this is linux)

    I would do it the same way as pstree does it. See http://www.thp.uni-duisburg.de/pstree/
    Debugging is twice as hard as writing the code in the first place.
    Therefore, if you write the code as cleverly as possible, you are, by
    definition, not smart enough to debug it.
    - Brian W. Kernighan

    To enhance your chance's of getting an answer be sure to read
    http://www.codeguru.com/forum/announ...nouncementid=6
    and http://www.codeguru.com/forum/showthread.php?t=366302 before posting

    Refresh your memory on formatting tags here
    http://www.codeguru.com/forum/misc.php?do=bbcode

    Get your free MS compiler here
    https://visualstudio.microsoft.com/vs

  3. #3
    Join Date
    Oct 2012
    Location
    New Zealand
    Posts
    11

    Cool Re: How do I find out if a program is running in "C" (this is linux)

    you mean something like:

    Code:
    if (system("pstree | grep -o "progname" | head -n 1") == "progname"); {
    
    /* program is running */
    
    /* do something here if program is running */
    
    
    } else {
    
    /* program is not running */
    
    /* do something here if program is not running */
    
    }

    for some reason I could not get the above code to work.
    Last edited by n_techo; October 12th, 2012 at 01:31 AM.

  4. #4
    Join Date
    Oct 2006
    Location
    Sweden
    Posts
    3,654

    Re: How do I find out if a program is running in "C" (this is linux)

    No I mean that you can use the pstree code.

    The code you have should work as well. See this regarding system return codes though http://pubs.opengroup.org/onlinepubs...ns/system.html
    Debugging is twice as hard as writing the code in the first place.
    Therefore, if you write the code as cleverly as possible, you are, by
    definition, not smart enough to debug it.
    - Brian W. Kernighan

    To enhance your chance's of getting an answer be sure to read
    http://www.codeguru.com/forum/announ...nouncementid=6
    and http://www.codeguru.com/forum/showthread.php?t=366302 before posting

    Refresh your memory on formatting tags here
    http://www.codeguru.com/forum/misc.php?do=bbcode

    Get your free MS compiler here
    https://visualstudio.microsoft.com/vs

Posting Permissions

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





Click Here to Expand Forum to Full Width

Featured