简体   繁体   中英

sh kill illegal number : Kill parallel process

I am trying to open multiple parallel processes on a C++ program using. The four of them have to run in parallel. But when my C++ program closes, I want to kill each process. This is my attempt:

system("python okcsend.py & PID1=$! python okccnysend.py & PID2=$! python okc.py & PID3=$! python okccny.py & PID4=$!");

And when trying kill them, this is what I do:

system("kill PID1; kill PID2; kill PID3; kill PID4");

However, this is what I get:

sh: 1: kill: Illegal number: PID1
sh: 1: kill: Illegal number: PID2
sh: 1: kill: Illegal number: PID3
sh: 1: kill: Illegal number: PID4

What's the correct way of doing this?

Thanks.

You need to get back the PID from each process separately.

Pseudo code:

pid1 = system("python okcsend.py & echo $!)
pid2 = system("python okcsend.py & echo $!)
pid3 = system("python okcsend.py & echo $!)
pid4 = system("python okcsend.py & echo $!)

then you could do something like:

system("kill " + pid1 + "; kill " + pid2 + "; kill " + pid3 + "; kill " + pid4 + ";");

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM