简体   繁体   English

Pexpect关闭Shell脚本

[英]Pexpect shutting down a shell script

I have two defined functions, foo and bar which run pexpect that calls a script from the command line using pexpect.spawn . 我有两个已定义的函数foobar运行pexpect ,它们使用pexpect.spawn从命令行调用脚本。 Next it just sends a control-c using pexpect's sendcontrol and close to tell the script it's running to shut down, and then terminate the object. 接下来,它仅使用pexpect的sendcontrol发送一个control-c并close以告诉脚本它正在运行以关闭,然后终止该对象。 After the program runs tho, the object is still running in memory and it looks like control-c and/or child.close never reached foo() 程序运行完后,该对象仍在内存中运行,看起来像是control-c和/或child.close从未到达foo()

The script should terminate my spawned object, but instead my if logic in bar(child) does not seem to have any effect on the object created in foo() 该脚本应终止我的派生对象,但我的bar(child)中的if逻辑似乎对foo()创建的对象没有任何影响

Here's the code: 这是代码:

def foo():
   child = pexpect.spawn ('./script.sh')
   return child
def bar(child):
   if child.isalive():
      child.sendcontrol('c')
      child.close()
   if child.isalive():
      print 'Child did not exit gracefully.'
   else:
      print 'Child exited gracefully.'
a = foo()
bar(a)

The program always ends by printing "Child exited gracefully" and but leaves my script.sh running in the background 该程序始终以打印“ Child优雅退出”而结束,但是让我的script.sh在后台运行

This code here works tho: 这段代码可以这样工作:

def bar(child):
   child.sendcontrol('c')

that interacts ok with the object created in foo() but for some reason my other code with the if logic runs the if correctly but doesn't do anything to my object 可以与在foo()创建的对象进行交互,但是由于某种原因,我的其他带有if逻辑的代码可以正确地运行if ,但对我的对象没有任何作用

我认为我的问题是我的.PY文件中的缩进被字符破坏了

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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