gpt4 book ai didi

deno - 如何关闭计算机(主机)

转载 作者:行者123 更新时间:2023-12-02 01:29:33 28 4
gpt4 key购买 nike

我知道这听起来很奇怪,但我有一个情况,Deno 需要关闭自己的主机(并因此杀死自己的进程)。这可能吗?

我特别需要这个用于 linux (lubuntu),如果相关的话。我想这需要 sudo 权限,这很糟糕,但也是一个选择。

对于那些对细节感兴趣的人:我正在编写一个《我的世界》服务器软件,如果服务器30分钟内没有玩家,它会自行关闭以节省电量。无论如何,24/7 运行的树莓派具有局域网唤醒功能,以便它可以再次启动。启动后,服务器管理器软件将自 Action 为Linux服务启动。

最佳答案

您可以create a subprocess为此:

await Deno.run({ cmd: ["shutdown", "-h", "now"] }).status();

Concepts

  • Deno is capable of spawning a subprocess via Deno.run.
  • --allow-run permission is required to spawn a subprocess.
  • Spawned subprocesses do not run in a security sandbox.
  • Communicate with the subprocess via the stdin, stdout and stderr streams.
  • Use a specific shell by providing its path/name and its string input switch, e.g. Deno.run({cmd: ["bash", "-c", "ls -la"]});

另请参阅command line - Shutdown from terminal without entering password? - Ask Ubuntu了解如何避免需要 sudo 调用 shutdown 或可以从 Deno 调用的替代命令。

关于deno - 如何关闭计算机(主机),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/73641180/

28 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com