Python script to schedule computer shutdown
2 min read

Python script to schedule computer shutdown

Python script to schedule computer shutdown

Hi guys,

One thing that as a python programmer you should be proud of is that it gives you the capability to automate a bunch of repetitive tasks effortlessly just with the basics.

In this article, you're going to learn about building a cross-platform python program for automating shutdown.


In this article, we gonna use OS + Platform to implement our script, both of them comes with the python standard library utilities so you don't need to install anything

Basics of Platform Module

Platform Module, we will use it to determine the type of Operating system in which our program is running.

Example of usage

>>> import platform

>>> platform.system()


If you're using windows you should see Windows instead of Linux.

Basics of OS Module

OS Module, we are going to use this module to run a terminal/command prompt command in Python.

To run the system command in Python we are going to use system ( )

Example of Usage

>>> import os

>>> os.system('echo hello')



The command for shutdown computers varies depending on the operating system.

System command for automated shutdown

Below a system command for automating computer shutdown, our task gonna lie on running programmatically do this based on user input from python.

In Linux

  • shutdown command for Linux
shutdown -t time_in_minutes
  • Usage
kalebu@kalebu-PC:~$ shutdown -t 12

Shutdown scheduled for Wed 2020-06-03 15:55:40 EAT,
 use 'shutdown -c' to cancel.

  • canceling automated shutdown
kalebu@kalebu-PC:~$  shutdown -c

In Window

  • shutdown command in window OS
shutdown -s -t time_in_seconds
  • Usage
shutdown -s -t 1200
  • Cancelling scheduled shutdown in Window
shutdown -a

Building our Python program.

Therefore we need to get the time parameter to automate the shutdown from the user and then run those commands from our Python program.

import os
import platform

OS_type = platform.system()
OS_type = OS_type.lower()

def shutdown(time):
    It automate shutdown by scheduling the 
    time for computer to shutdown

    if OS_type == 'linux':
        shutdown_str = 'shutdown -t '+ str(time)
    elif OS_type == 'windows':
        time = time * 60
        shutdown_str == 'shutdown -s -t ' + str(time)
        print('Sorry this feature is not available in ', OS_type)


def cancel_shutdown():
    if OS_type == 'linux':
        cancel_str = 'shutdown -c'
    elif OS_type == 'windows':
        cancel_str = 'shutdown -a'

def main():
    print('Use Shutup To schedule your shutdown'.center(50, '='))
    print('1.Automate Shutdown\n2.Cancel shutdown')
    option = int(input('Option here: '))
    if option == 1:
        time = int(input('\nEnter time to shutdown in Minutes : '))
    elif option == 2:
        print('Shutdown successful canceled ...')
        print('Invalid option try again!!!\n')

if __name__ == '__main__':

Hope you find this post interesting, don't forget to subscribe to so you never miss tips  like this. Full code available on My Github

In case of any suggestion or comment, drop it in the comment box and I will get back to you ASAP