加入收藏 | 设为首页 | 会员中心 | 我要投稿 李大同 (https://www.lidatong.com.cn/)- 科技、建站、经验、云计算、5G、大数据,站长网!
当前位置: 首页 > 大数据 > 正文

通过Lua脚本重新启动系统

发布时间:2020-12-14 21:45:57 所属栏目:大数据 来源:网络整理
导读:我需要通过Lua脚本重启系统. 我需要在重启之前写一些字符串,并且需要在Lua中写一个字符串 重新启动完成后的脚本. 示例: print("Before Reboot System")Reboot the System through Lua scriptprint("After Reboot System") 我怎么做到这一点? 解决方法 您可
我需要通过Lua脚本重启系统.
我需要在重启之前写一些字符串,并且需要在Lua中写一个字符串
重新启动完成后的脚本.

示例:

print("Before Reboot System")

Reboot the System through Lua script

print("After Reboot System")

我怎么做到这一点?

解决方法

您可以使用os.execute发出系统命令.对于Windows,它是关闭-r,对于Posix系统,它只是重启.因此,您的Lua代码将如下所示:

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

不幸的是,如果不了解您的环境,我无法告诉您如何再次调用脚本.您可以将脚本的调用追加到?/ .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

(警告 – 未经测试的代码.我不想重新启动.:)

(编辑:李大同)

【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容!

    推荐文章
      热点阅读