首页 > 解决方案 > 我已经安排了一个 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 作业后,我只收到广播消息,所以我需要让用户输入是或否来打盹。请帮助我。

标签: pythoncron

解决方案


cron无法访问调用用户的终端或桌面 GUI。您将需要重构代码以通过其他方式获得答案。

但是这里正确的解决方案可能是在您的 GUI 中挂接一些东西,以便在您登录到桌面时定期运行。具体如何做到这一点取决于您的平台。在 linux 上登录时运行我的 Python 脚本有一些提示,但 Linux 桌面环境是一个移动目标,而且您还没有告诉我们您使用的是哪个发行版和 GUI 框架。


推荐阅读