Save blend file and shut down at certain time?

hi,
as i often forget about the time around me, i was thinking about a script that reminds on how late it is, and/or saves and shuts down blender at a given time. For example:

  • set a shutdown time, say 21:00, and a latest reminder, say 10 minutes before termination.
  • then, at 20:50, blender reminds you that you have 10 minutes left to go.
  • at 21:00, blender saves the blend file and shuts down, regardless of what you are doing.

Is something like this hard to code? i unfortunately have no experience with that.

Thank you in advance!

Don’t know why it’s not working, but saving and shutdown alone are executed without problems. I’ll play with it at weekend probably (if cyberpunk and blender alow it :stuck_out_tongue: )

import os,time,bpy

#check current time
def get_time():
    t = time.localtime()
    current_time = time.strftime("%H:%M:%S", t)
    print(current_time)
    return current_time

#shutdown command for windows
def shutdown():
    bpy.ops.wm.save_as_mainfile(filepath=bpy.data.filepath) #save project
    os.system("shutdown /s /t 600")

#check if its time and shutdown
def check_time():
    shutdown_time = '20:50'
    shutdown_started = False
    if shutdown_time == get_time():
        print("It's time for a shutdown")
        shutdown()
        shutdown_started = True
    else:
        #print("There's still time")
        pass

#run check_time() every minute
while True:
    if shutdown_started == False:
        time.sleep(60 - time() % 60)
        check_time()
    else:
        break

Hi, thanks for the support. But the script does not work either on my setup. It returns an erro on line 29.
Is there maybe an issue because the current time i defined as "%H:%M:%S, and the shutdown time is gicen in HH:MM ??

Yes that was it, also… but mostly my bad with defining boolean.

Bigger problem is that this code will now run ok in blender, but only once:

import os,time,bpy

#check current time
def get_time():
    t = time.localtime()
    current_time = time.strftime("%H:%M", t)
    print(current_time)
    return current_time

#shutdown command for windows
def shutdown():
    bpy.ops.wm.save_as_mainfile(filepath=bpy.data.filepath) #save project
    os.system("shutdown /s /t 600")

#check if its time and shutdown
def check_time():
    shutdown_time = '20:50'
    if shutdown_time == get_time():
        shutdown()
    else:
        pass

check_time()

It need to run in a loop, and when I put only that simple loop, blender is crushing - or rather running loop that is blocking it:

import time

while True:
    print('something')
    time.sleep(2)

I don’t know how to run loops in blender that wouldn’t block it, I have to google for it or maybe someone smarter will showup :stuck_out_tongue: