简体   繁体   English

如何使用python脚本关闭计算机?

[英]How can I shut down my computer using python script?

I want to shut down my computer(mac) using a python script for some code I have written. 我想使用python脚本关闭我的计算机(mac),以获取我编写的某些代码。 I have tried the following 我尝试了以下

os.system('shutdown -s')
os.system('shutdown -h now')

But both of these produce the output 256 and my system doesn't shut down. 但是这两个都产生输出256,而我的系统没有关闭。

I read it might be due to not having root privileges but I guess I can't provide root privileges through python. 我读到它可能是由于没有root特权,但我想我无法通过python提供root特权。 Is there a way I can run a python script and shut down my mac with the above problem ? 有没有一种方法可以运行python脚本并关闭上述问题的Mac?

For restart: 重新启动:

re_start = ["shutdown", "-f", "-r", "-t", "30"]

For shutdown: 对于关机:

 shut_down = ["shutdown", "-f", "-s", "-t", "30"]

Hence: 因此:

def shutdown(self):
    import subprocess
    subprocess.call(shut_down)

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 如何在计算机关闭的情况下在后台运行python的spark应用程序? - How can I run my spark app with python in the background with my computer shut off? 在 Python 中构建模型后,如何保存模型以便我可以在第二天关闭计算机并继续工作? - After building a model in Python, how do I save the model so I could shut down my computer and work on it the next day? 现在该服务已关闭,如何制作一个脚本来恢复我的Grooveshark播放列表? - How can I make a script to recover my Grooveshark playlists now that the service has been shut down? 如何关闭 python simpleHTTPserver? - How do I shut down a python simpleHTTPserver? 通过双击执行时我的Python程序关闭 - my Python program shut down when i executing by double click 数据存储在哪里以及关闭计算机后为什么数据不见了 - Where is datastore and why my data is gone when I shut down the computer 如何关闭python服务器 - How to shut down python server 使用python关闭实例脚本 - Use python to shut down instance script runs on Python:为什么我的 discord 机器人会为单个命令打印多个输出? 此外,我如何重新启动/关闭我的机器人? - Python: Why does my discord bot print multiple outputs for a single command? Additionally, how do I restart/shut down my bot? 如何在计算机关闭时运行 cmd 脚本? - How can I run a cmd script while my computer is off?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM