user5387687
user5387687

Reputation:

Process management in C++

I am developing my application in C, and when application starts it needs to kill shell script which runs from system startup.

Here is my function that kills shell script from C++:

void Kill_Script_sh(void)
{
    FILE *fp;
    char buffer[30];
    char pid_number[5];
    int pid;
    int fd;
    std::stringstream command;

    command.str("");
    fp = popen("ps aux | grep tick.sh", "r");

    fgets(buffer, 30, fp);

    pclose(fp);

    pid_number[0] = buffer[11];
    pid_number[1] = buffer[12];
    pid_number[2] = buffer[13];
    pid_number[3] = buffer[14];
    pid_number[4] = buffer[15];


    pid = atoi(pid_number);

    printf("%d \n", pid);

    command << "sudo kill " << pid;

    system(command.str().c_str());
}

After I killed the process I need to check if it still exists. Is this correct way to do what I need? Any idea is helpful. Thanks

Upvotes: 2

Views: 716

Answers (1)

Hellmar Becker
Hellmar Becker

Reputation: 2972

After killing it (or in general), you can check for existence of a process using kill -0 [pid].

Upvotes: 1

Related Questions