Restarting a Linux service from C++ program

Posted on 2003-03-08
Medium Priority
Last Modified: 2010-04-17
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!
Question by:bgriscom
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 2

Expert Comment

ID: 8095794
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>

Accepted Solution

Joeisanerd earned 100 total points
ID: 8095824
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;

Expert Comment

ID: 9447261
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.

Featured Post

New benefit for Premium Members - Upgrade now!

Ready to get started with anonymous questions today? It's easy! Learn more.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Entering a date in Microsoft Access can be tricky. A typo can cause month and day to be shuffled, entering the day only causes an error, as does entering, say, day 31 in June. This article shows how an inputmask supported by code can help the user a…
Q&A with Course Creator, Mark Lassoff, on the importance of HTML5 in the career of a modern-day developer.
Viewers will learn how to properly install Eclipse with the necessary JDK, and will take a look at an introductory Java program. Download Eclipse installation zip file: Extract files from zip file: Download and install JDK 8: Open Eclipse and …
In this seventh video of the Xpdf series, we discuss and demonstrate the PDFfonts utility, which lists all the fonts used in a PDF file. It does this via a command line interface, making it suitable for use in programs, scripts, batch files — any pl…

752 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question