Restarting a Linux service from C++ program

Hi all,

I'm creating a C++ app that I'm running on a Red Hat Linux 8.0 box and am compiling with g++. I want to give the users several options, including one that says, "Restart the Apache service." I know how to restart a service at the command line, but how do I have a C++ program restart a service?

Any help would be appreciated. Thanks!
Who is Participating?
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

If I remember correctly if you want to kill a process by its process id it would be as follows:


Where pid is the numeric id of the process and signal is the signal you are sending it such as -HUP

To kill the process by name use the following


where signal is again the type of signal you are sending it and prc_name is the process name so for apache it should be something like


I think you can man both of those functions to get more information. Make sure you include the right header files <signal.h>
Oops! I made a mistake, the sendtoname function is one that I had to create for a project. To kill the process by name you need to open the /proc folder and find the process by its name, then get its pid and then you can use kill(pid,signal).  The sendtoname type of function is not fun but here is the code anyway

#include <unistd.h>
#include <dirent.h>
#include <sys/types.h>
#include <sys/stat.h>
#include <fcntl.h>
#include <stdio.h>
#include <signal.h>

int sendtoname(int sig, const char* name)
  DIR* dp;  //A directory pointer used to traverse through the proc directory

  struct stat fstat;
  struct dirent *de;

  int userid = -1;
  int fd, pid;        //fd-> file descripter, pid -> Process ID
  char  line[100];    //line is a string used to open the cmdline file
  char cmdline[1024]; //command line string used for the cmdline file

  userid = getuid();      //get my userid
  chdir("/proc");         //change the directory to /proc
  dp = opendir("/proc");  //opend the /proc directory and assign it dp

  while((de=readdir(dp))!= NULL) //traverse throught the /proc directory
    if( (stat(de->d_name, &fstat))!=-1) //get the stats on the file or directory
          //check to see if the userid of the process is yours
      if( userid == fstat.st_uid)
        if(S_ISDIR(fstat.st_mode)) //next check to see that it is a directory
        {   //Now create the string to open the cmdline file

          if( strcmp(de->d_name,"self")!=0) //make sure this process is not your own
             pid = atoi(de->d_name);

          fd = open(line,O_RDONLY);              //open the cmdline file
          cmdline[read(fd,cmdline,1024)] = '\0'; //read the file
          close(fd);                             //close the file

         if( strcmp(name,cmdline)==0) //check to see if the process name
         {                            //and cmdline match
           if( pid > 0 && sig > -1)   //if the pid is greater than 0 and there
           {                          //is a signal to send then
             if( kill(pid,sig) != 0 ) //attempt to send the process the signal
                 puts("\n\nFailed to kill the process specified\n");
         }//end if cmdline == to proc name
        }//end if proc id is my id
      }//end if DIR
    }//end if userid is mine
    else               //if there was an error checking the stats of
      perror("stat");  //the file or directory then show it
  }//end while loop
 return 0;

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
This old question needs to be finalized -- accept an answer, split points, or get a refund.  For information on your options, please click here-> http:/help/closing.jsp#1 
Post your closing recommendations!  No comment means you don't care.
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today

From novice to tech pro — start learning today.

Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.