python script to call the system automatically enters the 'yes / no'

script:

import shlex
import subprocess

args=shlex.split('sh /mysqlbackup/wn19testcdb1002/my_restore.sh')
p=subprocess.Popen(args,stdin=subprocess.PIPE)
a=p.stdin.write('YES')

Interpretation:

shlex.split ( 'sh /mysqlbackup/wn19testcdb1002/my_restore.sh') generate a sequence parameter list [ 'sh', '/ mysqlbackup / wn19testcdb1002 / my_restore.sh']

subprocess: standard input to generate a child process and the child process is connected to the I / O / go error, the child may also be obtained return value.
Several old Italian subprocess modules or functions other alternatives, such as: os.system os.spawn * os.popen * popen2 * commands *..

subprocess.Popen equivalent to using os.execvp () routine is performed.

If a general args [list]. If args is a string, it will be treated as a path to the executable file, so you can not pass up any parameters.


stdin stdout and stderr, respectively subroutine standard input, standard output and standard error. Optional values ​​are PIPE or a valid file descriptor (actually a positive integer) or a file object, and None.

If PIPE, it means you need to create a new pipeline, if it is None, will not do any work redirect file descriptor will inherit the parent of the child process. In addition, the value of stderr can be STDOUT, standard error of the child process to standard output.

 

Published 117 original articles · won praise 20 · views 330 000 +

Guess you like

Origin blog.csdn.net/u010719917/article/details/100357151