-2
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()

After running through cron job I'm getting just broadcast message so I need to get user input yes or no to snooze .Please help me.

tripleee
  • 175,061
  • 34
  • 275
  • 318
hariraj
  • 71
  • 6
  • If you’re running this through a cron job, then the question is, *what user*? – deceze Aug 02 '21 at 05:42
  • This script is running on some other session that cronjob has created. Perhaps the script is waiting for a user input forever there, or it terminates automatically. If you want to generate a pop-up message or something, you need to program that specifically. – Kota Mori Aug 02 '21 at 05:42
  • yes ,I need to create a pop-up message like yes or no option to delay the shutdown time . – hariraj Aug 02 '21 at 05:45
  • 1
    Your `cron` does not have access to your current desktop session, and can't even assume that you are logged in. You can try to access `DISPLAY=:0.0` but hardcoding this is brittle and error-prone. – tripleee Aug 02 '21 at 06:08

2 Answers2

0

cron has no access to the invoking user's terminal, or to your desktop GUI. You will need to refactor your code to get the answer by other means.

But the proper solution here is probably to hook something into your GUI to run periodically when you are logged in to your desktop. How exactly to do that depends on your platform. Run my Python script at login on linux has some hints but Linux desktop environments are a moving target, and you haven't told us which distro and GUI framework you're on.

tripleee
  • 175,061
  • 34
  • 275
  • 318
-1

Alright, I guess you need the function like this

change your python script replacesnooze=input('snooze:') to snooze=sys.argv[1] and save the file to test.py
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:')
snooze=sys.argv[1]
def shutdown():
      if snooze == "yes" :
         # delay_time=input("enter delay_time to shutdown:")
         delay_time=sys.argv[2]



         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()
edit your crontab command
* * * * * python test.py yes 1

or just running

python test.py yes/no 1
jasondayee
  • 549
  • 4
  • 11