-
Notifications
You must be signed in to change notification settings - Fork 0
/
popen.cpp
88 lines (74 loc) · 1.36 KB
/
popen.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <unistd.h>
#include <signal.h>
#include <string>
class CProcess
{
public:
CProcess(const char* _cmd)
{
if(NULL == _cmd)
{
printf("T --- NULL == cmd\n");
exit(-1);
}
cmd = _cmd;
fp = NULL;
}
int start_process()
{
fp = popen(cmd.c_str(), "r");
if(!fp) {
printf("Runing --- [%s]\n",cmd.c_str());
return -1;
} else {
printf("process run success...");
return 0;
}
pclose(fp);
}
int stop_process()
{
int rtn = false;
char pid_buf[100];
FILE* pid_fp = NULL;
char *cmd_pid = (char *)calloc(200,1);
sprintf(cmd_pid, "ps aux | grep %s | grep -v \"sh -c\" | grep -v \"grep\" | awk '{print $2}'",cmd.c_str());
pid_fp = popen(cmd_pid, "r");
if(NULL == pid_fp){
rtn = false;
printf("Create get child process id task failed!");
goto err;
}
if(fgets(pid_buf,100,pid_fp) == NULL) {
rtn = false;
printf("Get Child Process id failed!");
goto err;
} else {
printf("T --- pid_buf[%s]\n", pid_buf);
kill(atoi(pid_buf), SIGINT);
rtn = true;
}
err:
pclose(pid_fp);
pid_fp = NULL;
printf("Process exited success!!!!");
return rtn;
}
~CProcess()
{
}
private:
std::string cmd;
FILE *fp;
};
int main(void)
{
CProcess cp("./echo.sh");
cp.start_process();
sleep(10);
cp.stop_process();
return 0;
}