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.

Requirements

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

'Linux'

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')

hello

0

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)
    else:
        print('Sorry this feature is not available in ', OS_type)
        return

    os.system(shutdown_str)

def cancel_shutdown():
    if OS_type == 'linux':
        cancel_str = 'shutdown -c'
    elif OS_type == 'windows':
        cancel_str = 'shutdown -a'
    else:
        return
    os.system(cancel_str)

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 : '))
        shutdown(time)
    elif option == 2:
        cancel_shutdown()
        print('Shutdown successful canceled ...')
    else:
        print('Invalid option try again!!!\n')
        main()

if __name__ == '__main__':
    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

Kalebu Jordan
I am a Mechatronics engineer and professional Python Developer passionate about innovation, opensource and technical writing, Here I will be writing mostly on Python, DevOps, AI/ML and security
Dar es Salaam, Tanzania