Thursday, August 21, 2025

FUN PROJECT : Turn Your storage device (or Cloud Storage) into an Auto Shutdown Switch for Your PC


Ever wondered if you could use a file on your hard disk—or even a file stored online—as a “key” to keep your PC running, and automatically shut it down if that key disappears or changes? With a few lines of Python, you can do just that! This project demonstrates how to create an auto shutdown switch using a simple text file.


🔹 Project Overview

This Python script monitors a specific file on your system. If the file is missing or its content is not exactly "run", the script will immediately shut down your computer. You can place this script in your Startup folder so that it runs every time your PC starts.

This is useful for scenarios like:

  • Making sure your system only runs when a specific storage device is connected.

  • Creating a safety switch for shared computers.

  • Remotely controlling shutdowns via an online repository.


🔹 The Code

import os import sys # 🔧 Change this to your external storage file path file_path = "E:\\shutdownpc\\shutdownpc.txt" # Example path, update as needed def shutdown(): if os.name == "nt": # Windows os.system("shutdown /s /t 1") elif os.name == "posix": # Linux / macOS os.system("shutdown now") sys.exit() def check_file_once(): if not os.path.exists(file_path): shutdown() else: with open(file_path, "r") as f: content = f.read().strip().lower() if content != "run": shutdown() if __name__ == "__main__": check_file_once()

🔹 How the Code Works

  1. Set the file path
    The variable file_path should point to the text file you want to use as your “key.” This can be a file on a USB drive, an external hard disk, or even a folder synced with cloud storage.

  2. Shutdown function
    The shutdown() function detects your operating system. On Windows, it executes the shutdown /s /t 1 command to shut down immediately. On Linux or macOS, it runs shutdown now. After issuing the shutdown command, the script exits.

  3. Check file once

    • check_file_once() first checks if the file exists. If it doesn’t, the shutdown is triggered.

    • If the file exists, it reads the content. If the content is not "run" (case-insensitive), it triggers shutdown.

  4. Run on startup
    By placing this script in your Startup folder, your system will check the file automatically whenever it boots up.


🔹 Extending to Remote Shutdown

You don’t have to rely on a physical hard disk. Any online repository (like Google Drive, Dropbox, or even a GitHub file) can act as the shutdown trigger. You just need to:

  • Use a Python library such as requests to fetch the file content from the web.

  • Check if the content equals "run".

  • Trigger the shutdown if the file is missing or modified.

This opens up exciting possibilities for remotely controlling your laptop or server



No comments:

Post a Comment

🐍What is scikitlearn??