-2

I have an application running in c language (my_process.c) and a script (stop_process.sh) to stop the application and clean up all resources held by the process.

In the application code, let's say I want call stop_process script for some reason, it looks like stop_process is getting killed after the line (kill -9 my_process) and is not executing the rest of the script. I think it is getting killed because the script is sending SIGKILL to parent process.

my_process.c

function1() {
    system(nohup /home/admin/stop_process.sh < /dev/null &);
}

int main() {
    .....
    ......
    function1();
    .....
    .....
}

stop_process.sh

cleanup 
.......
.......
kill -9 my_process
kill -9 xxx
kill -9 yyy
......
.......
cleanup

After the "kill -9 my_process" line, I see this message:

Jan 17 21:28:22 Signal 15 (TERM) caught by ps (procps-ng version 3.3.10).
Jan 17 21:28:22 ps:display.c:66: please report this bug

How can I write a script that can be called from c to kill C process and restart the process at the end of cleanup?

  • Why not the C program managing everything and not calling bash? – Rui F Ribeiro Jan 17 at 21:54
  • why the hell someone is giving negative score without any comments. – user1762571 Jan 20 at 20:50
-1

Your stop_process script appears to be successfully killing your my_process; the trouble, as you saw, is that my_process then dies and cannot execute any more code. To restart my_process, I'd suggest a differently-named script that kills and restarts my_process, versus just stopping it:

/home/admin/restart_process.sh

# ...
kill -9 my_process
kill -9 xxx
kill -9 yyy
# ...
my_process

my_process.c

function1() {
    system(nohup /home/admin/restart_process.sh < /dev/null);
}

Note that there's no point to calling the shell script in the background.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.