python - 我已经安排了一个 python 文件通过 cron 作业在 Linux 中运行,所以只有广播消息来了,但用户输入没有出现
问题描述
import sys
import platform
import os
if platform.system() == "Linux":
cmd= ' sudo wall "System going to shutdown at 5:00 AM UST"'
os.system(cmd)
print("Type yes/no to snooze the shutdwon ")
snooze=input('snooze:')
def shutdown():
if snooze == "yes" :
delay_time=input("enter delay_time to shutdown:")
if platform.system()== 'Linux':
print(" Hello this is Linux os")
command= 'sudo wall "System going to shutdown at {} "'.format(delay_time)
os.system(command)
if platform.system() == "Linux" and snooze == "no":
cmd= 'sudo wall "System going to shutdown at 5:00 AM UST"'
os.system(cmd)
shutdown()
运行完 cron 作业后,我只收到广播消息,所以我需要让用户输入是或否来打盹。请帮助我。
解决方案
cron
无法访问调用用户的终端或桌面 GUI。您将需要重构代码以通过其他方式获得答案。
但是这里正确的解决方案可能是在您的 GUI 中挂接一些东西,以便在您登录到桌面时定期运行。具体如何做到这一点取决于您的平台。在 linux 上登录时运行我的 Python 脚本有一些提示,但 Linux 桌面环境是一个移动目标,而且您还没有告诉我们您使用的是哪个发行版和 GUI 框架。
推荐阅读
- unit-testing - 带有@Quarkus 注释的测试用例抛出错误(也使用 Intellij idea 插件)
- selenium-chromedriver - 无法使用 findElements()、filter() 或 selectLink() 定位特定的 HTML 元素
- python - 使用一个函数的输出作为另一个函数的输入
- sql - 对于以下场景,如何在 oracle sql 中执行类似 unpivot 的操作?
- python - 如何使用 glob 正确调用当前工作目录的子目录
- azure-devops-migration-tools - AzDo 迁移工具:PBI 到用户故事的迁移不起作用
- android - Frida 没有在私有进程中挂钩函数
- c# - 让 DI、不变性和异步共存
- angular - 如何过滤 BehaviorSubject 数组?
- javascript - 为什么 conv.add 在使用 API 时 console.log 不起作用