forked from raczben/wexpect
-
Notifications
You must be signed in to change notification settings - Fork 0
/
termination.py
68 lines (51 loc) · 2.31 KB
/
termination.py
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
''' This script shows three method to terminate your application. `terminate_programmatically()`
shows the recommended way, which kills the child by sending the application specific exit command.
After that waiting for the terminaton is recommended.
`terminate_eof()` shows how to terminate child program by sending EOF character. Some program can be
terminated by sending EOF character. Waiting for the terminaton is recommended in this case too.
`terminate_terminate()` shows how to terminate child program by sending kill signal. Some
application requires sending SIGTERM to kill the child process. `terminate()` call `kill()`
function, which sends SIGTERM to child process. Waiting for the terminaton is not required
explicitly in this case. The wait is included in `terminate()` function.
'''
import wexpect
def terminate_programmatically():
'''Terminate child program by command. This is the recommended method. Send your application's
exit command to quit the child's process. After that wait for the terminaton.
'''
print('terminate_programmatically')
# Start cmd as child process
child = wexpect.spawn('cmd.exe')
# Wait for prompt when cmd becomes ready.
child.expect('>')
# Exit from cmd
child.sendline('exit')
# Waiting for cmd termination.
child.wait()
def terminate_eof():
'''Terminate child program by sending EOF character. Some program can be terminated by sending
an EOF character. Waiting for the terminaton is recommended in this case too.
'''
print('terminate_eof')
# Start cmd as child process
child = wexpect.spawn('cat')
# Exit from cmd
child.sendeof()
# Waiting for cmd termination.
child.wait()
def terminate_terminate():
'''Terminate child program by sending kill signal. Some application requires sending SIGTERM to kill
the child process. `terminate()` call `kill()` function, which sends SIGTERM to child process.
Waiting for the terminaton is not required explicitly in this case. The wait is included in
`terminate()` function.
'''
print('terminate_terminate')
# Start cmd as child process
child = wexpect.spawn('cmd.exe')
# Wait for prompt when cmd becomes ready.
child.expect('>')
# Exit from cmd
child.terminate()
terminate_programmatically()
terminate_eof()
terminate_terminate()