通过Lua脚本重新启动系统

通过Lua脚本重新启动系统,lua,Lua,我需要通过Lua脚本重新启动系统。 我需要在重新启动之前编写一些字符串,并且需要在Lua中编写一个字符串 重新启动完成后编写脚本 例如: print("Before Reboot System") Reboot the System through Lua script print("After Reboot System") 我将如何实现这一点?在Lua中,没有办法实现您的要求。您可以使用操作系统执行此操作。执行取决于您的系统和设置,但Lua的库仅包括标准c库中可能的功能,而标准c库不包

我需要通过Lua脚本重新启动系统。 我需要在重新启动之前编写一些字符串,并且需要在Lua中编写一个字符串 重新启动完成后编写脚本

例如:

print("Before Reboot System")

Reboot the System through Lua script

print("After Reboot System")

我将如何实现这一点?

在Lua中,没有办法实现您的要求。您可以使用操作系统执行此操作。执行取决于您的系统和设置,但Lua的库仅包括标准c库中可能的功能,而标准c库不包括操作系统特定的功能,如重新启动。

您可以使用操作系统。执行发出系统命令。对于Windows,它是
shutdown-r
,对于Posix系统,它只是
reboot
。因此,您的Lua代码将如下所示:

请注意,reboot命令的一部分是停止活动程序,如Lua脚本。这意味着存储在RAM中的任何数据都将丢失。您需要将任何要保留的数据写入磁盘,例如使用

不幸的是,如果不了解您的环境,我无法告诉您如何再次调用脚本。您可以将对脚本的调用附加到
~/.bashrc
或类似内容的末尾

请确保加载此数据并在调用重新启动功能后的某个点开始,这是您回来时要做的第一件事!你不想陷入无休止的重启循环中,当你的电脑开机时,第一件事就是关机。像这样的方法应该会奏效:

local function is_rebooted()
    -- Presence of file indicates reboot status
    if io.open("Rebooted.txt", "r") then
        os.remove("Rebooted.txt")
        return true
    else
        return false
    end
end

local function reboot_system()
    local f = assert(io.open("Rebooted.txt", "w"))
    f:write("Restarted!  Call On_Reboot()")

    -- Do something to make sure the script is called upon reboot here

    -- First line of package.config is directory separator
    -- Assume that '\' means it's Windows
    local is_windows = string.find(_G.package.config:sub(1,1), "\\")

    if is_windows then
        os.execute("shutdown -r");
    else
        os.execute("reboot")
    end
end

local function before_reboot()
    print("Before Reboot System")
    reboot_system()
end

local function after_reboot()
    print("After Reboot System")
end

-- Execution begins here !
if not is_rebooted() then
    before_reboot()
else
    after_reboot()
end

(警告-未测试的代码。我不想重新启动。:)

有没有办法重新启动系统注意:这在不是Posix或Windows的系统上不起作用。但我猜你知道如果你在其中一个系统上工作的话。