Quantcast
Channel: Raspberry Pi Forums
Viewing all articles
Browse latest Browse all 4814

HATs and other add-ons • Re: Run script requiring network before shutdown or reboot

$
0
0
Sure! I want to send a last log to a server, to inform about the shutdown (for whatever reason). I just want to know that it shut down.

Here's my python code.

Code:

import sysimport logfrom utils.config import configfrom utils.helpers import system_infodef run():   conf = config()   log.info("Ready for shutdown. Exiting.")   log.toServer(      title="Down",      message="Last Log before RPi Shutdown",      details=system_info(conf),      config=conf   )   sys.exit(0)if __name__ == "__main__":   run()
And the log.py

Code:

import requestsfrom addict import Dictdef toServer(message: str, title: str, details: Dict = Dict(), config: Dict = Dict()) -> bool :   data = {            "title": title,            "message": message,            "details": details.to_dict(,   }      try:      response = requests.post(         "https://url.com/asdf",         headers={'Authorization': f'Bearer {config.token}'},         json=data,         timeout=10      )   except Exception as e:      exception("log.toServer(): Exception", e)      return False      if response.status_code != 200:      error(f"log.toServer() failed with status {response.status_code} and message: {response.text}. Url: {url}")      return False      debug(f"log.toServer() successfull. title: {title}")   return True

Statistics: Posted by mmoollllee — Sat Apr 20, 2024 5:36 am



Viewing all articles
Browse latest Browse all 4814

Trending Articles