0
我需要殺死java進程,運行主類blabla.class。由於這個原因,我可以使用功能kill(pid_t, SIGKILL)
,但我需要獲得PID ID
。獲取PID,當有程序名片段時
我可以運行linux命令ps-ax | grep blabla
找到PID ID。用C來做這件事的最好方法是什麼?
我需要殺死java進程,運行主類blabla.class。由於這個原因,我可以使用功能kill(pid_t, SIGKILL)
,但我需要獲得PID ID
。獲取PID,當有程序名片段時
我可以運行linux命令ps-ax | grep blabla
找到PID ID。用C來做這件事的最好方法是什麼?
適應由Marco https://stackoverflow.com/a/8166467/1967396給出的鏈接:
#define LEN 100
char line[LEN];
FILE *cmd = popen("ps -ax | grep blabla", "r");
fgets(line, LEN, cmd);
// now parse `line` for the information you want, using sscanf perhaps?
// I believe the pid is the first word on the line returned, and it fits in an int:
int pid;
sscanf(line, "%d", &pid);
pclose(cmd);
您可以在子進程運行'ps',或者通過/ proc目錄的每個進程的子目錄嚇嚇自己,檢查每一個的/ proc /# ##/cmdline針對您的興趣模式。 –
'man pgrep','man pkill' – bobah
[如何在C中的Linux中獲取進程的PID]可能的重複(http://stackoverflow.com/questions/8166415/how-to-get-the-pid -of-a-process-in-linux-in-c) – Marco