Want to protect your cyber security and still get fast solutions? Ask a secure question today.Go Premium


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
  • 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

Get your problem seen by more experts

Be seen. Boost your question’s priority for more expert views and faster solutions

Question has a verified solution.

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

If you’re thinking to yourself “That description sounds a lot like two people doing the work that one could accomplish,” you’re not alone.
When you discover the power of the R programming language, you are going to wonder how you ever lived without it! Learn why the language merits a place in your programming arsenal.
In this fifth video of the Xpdf series, we discuss and demonstrate the PDFdetach utility, which is able to list and, more importantly, extract attachments that are embedded in PDF files. It does this via a command line interface, making it suitable …
Six Sigma Control Plans

580 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