Go4Expert

Go4Expert (http://www.go4expert.com/)
-   C (http://www.go4expert.com/forums/c/)
-   -   C Program to check if unix service is up/down (http://www.go4expert.com/forums/c-program-check-unix-service-t29433/)

nrhayyal 21Jan2013 16:42

C Program to check if unix service is up/down
 
Hi All,
Is there any API in C to check if any of the unix service is up or down.
Like i want to know if syslog service is up and down in C program.
if down how to bring it up using C program?
please help...

Regards
nrhayyal

DRK 11Feb2013 16:31

Re: C Program to check if unix service is up/down
 
Code:

#include <stdio.h>
#include <stdlib.h>
#include <string.h>
 
#define PROCESS_NAME "syslog"
#define TEMP_FILE_NAME "temp.txt"
#define MAX_LINE_LENGTH 100
 
int main()
{
    FILE *temp;
    char line[MAX_LINE_LENGTH];
    int found = 0;
 
    if (system("ps > "TEMP_FILE_NAME) == 0)
    {
        temp = fopen(TEMP_FILE_NAME, "r");
        if (temp != NULL)
        {
            while (fgets(line, MAX_LINE_LENGTH, temp) != NULL)
            {
                if (strstr(line, PROCESS_NAME) != NULL)
                {
                    found = 1;
                    break;
                }
            }
            fclose(temp);
            if (found == 1)
            {
                puts(PROCESS_NAME" is running");
            }
            else
            {
                puts(PROCESS_NAME" is not running");
            }
        }
        else
        {
            puts("Unable to open temporary file "TEMP_FILE_NAME);
        }
    }
    else
    {
        puts("Unable to get process list");
    }
    system("rm "TEMP_FILE_NAME);
 
    return 0;
}



All times are GMT +5.5. The time now is 07:17.