Search code examples
c++cprocesskill-process

kill() system call with SIGTERM is not working


I have created a binary using c++ in ubuntu which will run as a daemon and read the data from database and store it into a xml file.

and for stopping the daemon i am using this function but it is not working.

void stopService()
{
    int mypid;

    if(((mypid = validate_pid()) > 0) || ((mypid = validate_non_pid()) > 0)) {


        if(0 == kill(mypid, SIGTERM) ) {

            sleep(1);
        }
        else {
            printf("Stopping %s  [FAILED]\n", Service);// this line is getting printed.
        }

    }
    else {
        printf("Stopping %s  [ Failed ] Not Running....\n", Service);
    }

}

i'm getting the ouput as stopping the service [failed].

'validate_pid()' It will written the pid from /proc/some id/cmdline.

and 'validate_non_pid()' It will written the pid using pgrep.

I am not writing the complete code since it will become lengthy,

Thanks in advance.

and one thing i'm calling this function by taking command line argument and using this in switch(); 'case 'e': stopService();'

so how can i kill this process by usin kill().


Solution

  • It can be a permission issue. Print the Process Id using

    'getpid();'
    

    Kill it manually from terminal.

    'sudo kill -9 processid'