简体   繁体   中英

How do I run terminal commands in a script?

I am currently running an Raspberry Pi 4b with an x728 UPS . I am trying to make a script so that when power gets cut off from the UPS, it would run a shut down script.

Currently, I have this -

import RPi.GPIO as GPIO
import subprocess

GPIO.setmode(GPIO.BCM)
GPIO.setup(6, GPIO.IN)

while:
      if GPIO.input(6) == 1:
          subprocess.call(['shutdown', '-h', 'now'], shell=False)

This works in shutting down the Pi when power gets cut off, however, the UPS doesnt turn off, and in turn, I cant use the device's auto-turn on feature when the power comes back on.

In their wiki , they have a command x728off that you can run in the terminal to shutdown both the Pi and the UPS. It works great if I just typed it directly into terminal, however, I dunno how to put it in my code.

To run the command as it is use.

import os
os.system("x728off")

or if you want to use subprocess module, use

import subprocess
subprocess.call(["x728off"], shell=False)

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM