Re: periodically check if a service is running



On Thu, Jul 13, 2006 at 01:02:38PM +0200, rupert wrote:
Hello,

imm trying to create a small window that shows me if a serice is running and
displays a small icon that shows the status
i created this function that checks if a PID is there:

gint check_samba(){
    gint smb_pid2;
    gchar check_smb_cmd[512];
    sprintf(check_smb_cmd, "cat /var/run/samba/smbd.pid 2>/dev/null");
    //g_print("%s", check_smb_cmd);
    smb_pid2 = system(check_smb_cmd);
    //g_print("%s", smb_pid2);
    return smb_pid2;
}

the function is called here:

  gint smb_pid;
  smb_pid=check_samba();
  //g_print("PID: %i", smb_pid);
//while(1){
      if(smb_pid > 0)
      {
          image1 =  g_object_new(GTK_TYPE_IMAGE,"file", "pics/ledgreen.png",
NULL);
      }else{
          image1 =  g_object_new(GTK_TYPE_IMAGE,"file", "pics/ledred.png",
NULL);
      }
  //}

  gtk_widget_show (image1);


So how can I check and if neccessary change the picture lets say every each
2 seconds?
I thought i could use an endless while loop, but that didnt worked.

You can't run in a small loop and expect GTK to handle the rest
automatically. It has to get a chance to do its GUI thing.

The way to accomplish that is with g_timeout_add and friends (where you
can schedule a function, e.g. your little piece of code to be executed
some time from now).

Now to the most serious one:

This won't work:
============================================
    gint smb_pid2;
    gchar check_smb_cmd[512];
    sprintf(check_smb_cmd, "cat /var/run/samba/smbd.pid 2>/dev/null");
    //g_print("%s", check_smb_cmd);
    smb_pid2 = system(check_smb_cmd);
    //g_print("%s", smb_pid2);
============================================
Also the PID gets displayes when I call the function, but a g_print("PID:
%i", smb_pid); give me (null)
has been quite a while since i worked with C, so this may be a dump error by
me.

Note that the system() command won't return the contents of
/var/run/samba/smbd.pid. It will return zero (at least, when all went
well). This is the zero zou are seeing.

As to the pid you are seeing printed -- well, that's cat printing on
stdout, which is inherited from the caller.

Why don't you fopen() this file, fread() its contents and try to atoi()
this into an int (don't forget to fclose()). No system() at all.

BTW. system() is (at least) a can of worms. Don't use it lightly.

HTH
-- tomás


[Date Prev][Date Next]   [Thread Prev][Thread Next]   [Thread Index] [Date Index] [Author Index]