如何在后台从脚本 运行 return 到命令行
How to return to command line from script running in the background
我有这个紧急按钮和一个响应按钮按下的脚本。
如果我将进程发送到后台,使用命令 $sudo python3 ./Panicbutton.py &
我会返回命令行。当我按下紧急按钮时,脚本会响应,并打印与按下按钮的次数相对应的消息。我想让它做的是回到后台,同时等待更多的按钮按下。按 ctrl+c return 让我进入命令行,但我不想每次按下按钮后都需要按 ctrl+c。有没有办法在等待进一步按键时将脚本 return 放到后台?如何从脚本向终端发送 ctrl+c 命令?我不想终止进程,我只想在消息打印后命令行提示 return。
我正在使用 Linux 和 python3.4。请协助。
这是脚本:
# USB Panic Button interface code
# Copyright 2010 Ken Shirriff
# http://arcfn.com
import usb.core
x = 0
comment0 = """ PanicButton - interface to USB Panic Button This code requires PyUSB."""
class PanicButton:
def __init__(self):
# Device is: ID 1130:0202 Tenx Technology, Inc.
self.dev = usb.core.find(idVendor=0x1130, idProduct=0x0202)
if not self.dev:
raise ValueError("Panic Button not found")
try:
self.dev.detach_kernel_driver(0) # Get rid of hidraw
except Exception as e:
pass # already unregistered
def read(self):
comment1 = """ Read the USB port. Return 1 if pressed and released, 0 otherwise."""
#Magic numbers are from http://search.cpan.org/~bkendi/Device-USB-PanicButton-0.04/lib/Device/USB/PanicButton.pm
return self.dev.ctrl_transfer(bmRequestType=0xA1, bRequest=1, wValue=0x300, data_or_wLength=8, timeout=500)[0]
if __name__ == "__main__":
import time
button = PanicButton()
while 1:
if button.read():
global x
x = x + 1
if x < 5:
print(x)
elif x == 5:
print("Baby just screem if you want some more!")
elif x == 6:
print("Like AH!")
elif x == 7:
print("push it, push it.")
elif x == 8:
print("watch me work it.")
elif x == 9:
print("I'm PERFECT")
else:
x = 0
print("")
time.sleep(.5)
脚本始终在后台,不会在任何时候进入前台。它只是看起来那样,因为脚本的输出具有看似混乱提示的纯粹的装饰效果。
您可以直接输入 ls
并按回车键,而不是按 Ctrl-C。您会看到 shell 工作正常并且您的脚本不在前台。
Bash 无法真正重绘提示,因为它无法控制哪些其他程序写入屏幕。考虑寻找一种侵入性较小的报告按钮按下的方式,例如:
- 打印一个
bel
字符让终端播放声音
- 更改 xterm 的标题
- 使用
tmux
或 screen
获得 window 或状态栏消息
- 使用xmessage或类似的方式弹出对话框
- 让您的 bash 提示在每次重绘时包含来自脚本的消息
我有这个紧急按钮和一个响应按钮按下的脚本。
如果我将进程发送到后台,使用命令 $sudo python3 ./Panicbutton.py &
我会返回命令行。当我按下紧急按钮时,脚本会响应,并打印与按下按钮的次数相对应的消息。我想让它做的是回到后台,同时等待更多的按钮按下。按 ctrl+c return 让我进入命令行,但我不想每次按下按钮后都需要按 ctrl+c。有没有办法在等待进一步按键时将脚本 return 放到后台?如何从脚本向终端发送 ctrl+c 命令?我不想终止进程,我只想在消息打印后命令行提示 return。
我正在使用 Linux 和 python3.4。请协助。
这是脚本:
# USB Panic Button interface code
# Copyright 2010 Ken Shirriff
# http://arcfn.com
import usb.core
x = 0
comment0 = """ PanicButton - interface to USB Panic Button This code requires PyUSB."""
class PanicButton:
def __init__(self):
# Device is: ID 1130:0202 Tenx Technology, Inc.
self.dev = usb.core.find(idVendor=0x1130, idProduct=0x0202)
if not self.dev:
raise ValueError("Panic Button not found")
try:
self.dev.detach_kernel_driver(0) # Get rid of hidraw
except Exception as e:
pass # already unregistered
def read(self):
comment1 = """ Read the USB port. Return 1 if pressed and released, 0 otherwise."""
#Magic numbers are from http://search.cpan.org/~bkendi/Device-USB-PanicButton-0.04/lib/Device/USB/PanicButton.pm
return self.dev.ctrl_transfer(bmRequestType=0xA1, bRequest=1, wValue=0x300, data_or_wLength=8, timeout=500)[0]
if __name__ == "__main__":
import time
button = PanicButton()
while 1:
if button.read():
global x
x = x + 1
if x < 5:
print(x)
elif x == 5:
print("Baby just screem if you want some more!")
elif x == 6:
print("Like AH!")
elif x == 7:
print("push it, push it.")
elif x == 8:
print("watch me work it.")
elif x == 9:
print("I'm PERFECT")
else:
x = 0
print("")
time.sleep(.5)
脚本始终在后台,不会在任何时候进入前台。它只是看起来那样,因为脚本的输出具有看似混乱提示的纯粹的装饰效果。
您可以直接输入 ls
并按回车键,而不是按 Ctrl-C。您会看到 shell 工作正常并且您的脚本不在前台。
Bash 无法真正重绘提示,因为它无法控制哪些其他程序写入屏幕。考虑寻找一种侵入性较小的报告按钮按下的方式,例如:
- 打印一个
bel
字符让终端播放声音 - 更改 xterm 的标题
- 使用
tmux
或screen
获得 window 或状态栏消息 - 使用xmessage或类似的方式弹出对话框
- 让您的 bash 提示在每次重绘时包含来自脚本的消息