7 Replies - 1858 Views - Last Post: 13 October 2012 - 02:59 AM Rate Topic: -----

#1 n_techo  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 14
  • Joined: 11-October 12

How do I find out if a program is running, using "C"

Posted 12 October 2012 - 05:17 AM

In bash I can check if a program is running or not, and do stuff.


if [ `pstree | grep -o "progname" | head -n 1` = "progname" ]; then

echo "program is running"

else

echo "program is not running"

fi


I have tried to translate this to "C"

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 */


}


The code in "C" does nothing at all, maybe I have something wrong here, there are no error massages or anything.

Maybe there is another way to achieve this in "C"

thanks

The "C" code was an example

here is what I used in my program.

if (system("pstree | grep -o "progname" | head -n 1") == "progname"); {

/* program is running */

gtk_label_set_text(GTK_LABEL (label_status), gettext("Streaming Status: Streaming"));


} else {

/* program is not running */

gtk_label_set_text(GTK_LABEL (label_status), gettext("Streaming Status: Stopped"));

}


Is This A Good Question/Topic? 0
  • +

Replies To: How do I find out if a program is running, using "C"

#2 JackOfAllTrades  Icon User is offline

  • Saucy!
  • member icon

Reputation: 6111
  • View blog
  • Posts: 23,671
  • Joined: 23-August 08

Re: How do I find out if a program is running, using "C"

Posted 12 October 2012 - 05:51 AM

It's wasteful to shell out to the system for this info, when you can use libproc, if it's available. If it's not available, you can read the contents of the /proc directory with scandir to find the process you're interested in.
Was This Post Helpful? 0
  • +
  • -

#3 Salem_c  Icon User is offline

  • void main'ers are DOOMED
  • member icon

Reputation: 1779
  • View blog
  • Posts: 3,437
  • Joined: 30-May 10

Re: How do I find out if a program is running, using "C"

Posted 12 October 2012 - 07:11 AM

> if (system("pstree | grep -o "progname" | head -n 1") == "progname"); {
Well first of all, the ; at the end of the if statement makes it pointless.

Then you should have gotten a warning about comparison between pointer and integer. Having got that, you should have looked up system() in the manual to find out it returns an int. What it returns is the exit status of the process invoked.

Finally, you should have noted from the broken syntax highlighting that there was something wrong with your string.

Rather than trying to compare the output of grep, get grep to tell you success or failure.

Quote

`-q'
`--quiet'
`--silent'
Quiet; do not write anything to standard output. Exit immediately
with zero status if any match is found, even if an error was
detected. Also see the `-s' or `--no-messages' option. (`-q' is
specified by POSIX.)
...
2.3 Exit Status
===============

Normally, the exit status is 0 if selected lines are found and 1
otherwise.


But if you want to parse the stdout of the sub-process yourself, then use popen() instead.


So, to summarise
if (system("pstree | grep -q \"progname\"") == 0 ) {
  // running
} else {
  // or not
}



Or do as JackOfAllTrades says :)
Was This Post Helpful? 0
  • +
  • -

#4 n_techo  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 14
  • Joined: 11-October 12

Re: How do I find out if a program is running, using "C"

Posted 12 October 2012 - 08:10 AM

Quote

So, to summarise

if (system("pstree | grep -q \"progname\"") == 0 ) {
// running
} else {
// or not
}


I tried this code and it worked, simply brilliant, 100%

I am curious about popen(), as I understand this opens up a pipe between the "C" program and shell command or shell script.

can I use a loop in bash to update info to the "C" program, using popen() ?.

Next I wish to make a loop to check if "progname" is running or not every 2 secs, but to do that I will have fork() a child process, or run a loop with a bash script, and update the "C" program.

I can write this loop in bash, I can do that, better to write it in "C" as a damon or child process, I would need to open a pipe between the parent program and the child process (being the loop) in any case.
Was This Post Helpful? 0
  • +
  • -

#5 Salem_c  Icon User is offline

  • void main'ers are DOOMED
  • member icon

Reputation: 1779
  • View blog
  • Posts: 3,437
  • Joined: 30-May 10

Re: How do I find out if a program is running, using "C"

Posted 12 October 2012 - 09:28 AM

Tell me, is "progname" a process that you fork()'ed into the background from within the process which is doing all the checking?

If there is, then you can use sigaction to install a signal handler for SIGCHLD. Then you don't have to do any inline monitoring in your code, and you can just do what is necessary in the signal handler.

popen() works like opening a file (it returns a FILE*). So long as the process invoked doesn't exit, then you can keep reading lines from it (using say fgets) and do what you want with it.
Was This Post Helpful? 0
  • +
  • -

#6 n_techo  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 14
  • Joined: 11-October 12

Re: How do I find out if a program is running, using "C"

Posted 12 October 2012 - 01:45 PM

The process "progname" is darkice which listens to the sound card and encodes the audio using an aac+ encoder, the program I am working on is the front end gui for "darkice" the gui has stop and start buttons that start and stop "darkice. The gui also write the config file for darkice and ameter (ameter which is a VU meter) as well as having a status text label, that tells you if "darkice" is running or not. The start and stop buttons invoke a bash script which I have written, that starts up vlc (media player) on either a windows computer or a linux computer, using ssh password-less login. the result is you push the start button on the gui and the media player on the remote player starts up automatically, as well as sending a playlist *.m3u file on the desktop of the remote computer, just in case some at the remote computer closes the media player. And when you push the stop button it closes the media player on the remote computer as well as deleting the playlist file on the desktop of the remote computer.

So now I need to test if "darkice" is running or not so the status label of the GUI can be updated, I wish to use a damon child process to do the checking every 2 secs and update the status label on the GUI.

Here is the damon code which will be a child process of the GUI program.

#include <stdio.h>
#include <stdlib.h>
#include <unistd.h>
#include <sys/types.h>
#include <sys/stat.h>
#include <string.h>

int main(int argc, char* argv[])
{
    pid_t pid = 0;
    pid_t sid = 0;
    pid = fork();// fork a new child process

    if (pid < 0)
    {
        printf("fork failed!\n");
        exit(1);
    }

    if (pid > 0)// its the parent process
    {
       printf("pid of child process %d \n", pid);
        exit(0); //terminate the parent process succesfully
    }

    umask(0);//unmasking the file mode

    sid = setsid();//set new session
    if(sid < 0)
    {
        exit(1);
    }

    close(STDIN_FILENO);
    close(STDOUT_FILENO);
    close(STDERR_FILENO);


/* I wish to put my loop here using;

if (system("pstree | grep -q \"progname\"") == 0 ) {
// running
} else {
// or not
}

in some kind of loop that loops every 2 secs

*/



}



Kind Regard and thanks again :walkman:
Was This Post Helpful? 0
  • +
  • -

#7 n_techo  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 14
  • Joined: 11-October 12

Re: How do I find out if a program is running, using "C"

Posted 12 October 2012 - 01:53 PM

What would really be the best is to compile a signal handler in darkice and that can update the gui, like You said, I am just not sure how to do that yet.
Was This Post Helpful? 0
  • +
  • -

#8 JackOfAllTrades  Icon User is offline

  • Saucy!
  • member icon

Reputation: 6111
  • View blog
  • Posts: 23,671
  • Joined: 23-August 08

Re: How do I find out if a program is running, using "C"

Posted 13 October 2012 - 02:59 AM

There's a GTK client in the repository? Why not look at that for some ideas?
Was This Post Helpful? 0
  • +
  • -

Page 1 of 1