1

I have an application which is initiated by the shell and terminated by the shell. Now in my application I had created a fork to reduce the load of my application so currently two processes running on same name with different PID's. Now I want to terminate my program, and if I kill with the PID of my process in shell it only kills the parent process leaving it as a zombie process and the child process remains. So how to kill both child and parent processes through the shell?

Some programmer dude
  • 400,186
  • 35
  • 402
  • 621
Mr.Cool
  • 1,525
  • 10
  • 32
  • 51

4 Answers4

2

See the command killall.

Some programmer dude
  • 400,186
  • 35
  • 402
  • 621
  • if i can able to use kill -9 processname in terminal but i can't use in shell or in my application ,because my application i have one child process that child process has the another child process ,so i want to kill second child in my application,and kill the first child in shell ,or kill the parent process ,if it possible to kill like this manner – Mr.Cool Apr 10 '12 at 13:14
2

do ps -el you will get the following

F S   UID   PID  PPID  C PRI  NI ADDR SZ WCHAN  TTY          TIME CMD 

get pid and ppid of your process do pkill -P <parent_pid_name> meaning your parent process id which has spawned children and followed by kill <signal number> <pid> In this case too, in place of pid you have to give the parent process id who has spawned all the children .

Pavan Manjunath
  • 27,404
  • 12
  • 99
  • 125
user1270846
  • 126
  • 1
  • 6
1

Your father process should trap SIGTERM and kill its child before exit.

I wrote this in C

int child;

void sighandler(int sig) {
        if (sig == SIGTERM) {
                if (child != -1) kill(child, SIGTERM);
        }
}

int main() {
        int i;

        child = -1;
        signal(SIGCHLD, SIG_IGN);
        signal(SIGTERM, sighandler);

        setsid();
        setpgid(0,0);

        i = fork();
        switch(i) {
        case -1:
                break;
        case 0: // child
                sleep (100);
                break;
        default:
                child = i;
                sleep (100);
                break;
        }

        return 0;
}

You can kill all processes with the same name with the "killall" command.

dAm2K
  • 9,923
  • 5
  • 44
  • 47
0

pkill command you can use to kill process. Also killall

Satish
  • 16,544
  • 29
  • 93
  • 149