Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to kill background process from system function call

How can I kill a background process that was executed using a system function call in C language. Like for example I have a compiled application call "fooprocess". Then I want to write a program that will execute the fooprocess application in the background using the system function, please kindly see below the code,

const char app[] = "fooprocess &";
system(app);

As you can see there is a "&" character so that I can run the fooprocess application in the background. How can I kill this fooprocess?

Many thanks.

like image 337
domlao Avatar asked Dec 14 '10 04:12

domlao


People also ask

Can we kill any background executing process?

pkill. pkill is one of the commands that can directly kill a background process, using the process name.

Which command will stop a background process?

The killall command is used to kill processes by name. By default, it will send a SIGTERM signal. The killall command can kill multiple processes with a single command.


1 Answers

To interact with the process you need its PID. I'm not sure if it's possible with system but one alternative is to fork the process yourself using fork + exec.

like image 116
codaddict Avatar answered Oct 02 '22 15:10

codaddict