Restart the system via Lua script

I need to restart the system through a Lua script.
I need to write some strings before restarting, and I need to write a string in Lua
The script after restarting is complete .

Example:

print("Before Reboot System")

Reboot the System through Lua script< br />
print("After Reboot System")

How do I do this?

You can use os.execute to issue system commands. For Windows, it is to close -r, for Posix system, it just restarts. Therefore, your Lua code will look like this:

Please note that some reboot commands are stopping active programs, such as Lua scripts. This means storing in RAM Any data from will be lost. You need to write any data you want to keep to disk using for example table serialization.

Unfortunately, if you don’t understand your environment, I can’t tell you how to call the script again. You The call of the script can be appended to the end of ~/.bashrc or similar.

Make sure to load this data after you call the restart function and start from a certain point, this is the first thing you do when you come back ! You don’t want to get stuck in an endless restart loop, your computer must first shut it down when it is turned on. Things like this should work:

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")< br /> 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< br /> before_reboot()
else
after_reboot()
end

(Warning – untested code. I don’t want to reboot. 🙂

I need to restart the system through a Lua script.
I need to write some strings before restarting, and I need to write a string in Lua
The script after restarting is complete .

Example:

print("Before Reboot System")

Reboot the System through Lua script< br />
print("After Reboot System")

How do I do this?

You can use os.execute to issue system commands. For Windows, it is shutdown -r, for Posix systems, it is just restarting. Therefore, your Lua The code will look like this:

Please note that some reboot commands are stopping active programs, such as Lua scripts. This means that any data stored in RAM will be lost. You need to use for example table serialization writes any data you want to keep to disk.

Unfortunately, if you don’t understand your environment, I can’t tell you how to call the script again. You can append the call of the script to ~/.bashrc or Similar to the end.

Make sure to load this data after you call the restart function and start from a certain point, this is the first thing you do when you come back! You don’t want to get stuck in an endless restart loop, your computer must first shut it down when it is turned on. Things like this should work:

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")< br /> 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

(Warning – untested code. I don’t want to reboot. 🙂

< /p>

Leave a Comment

Your email address will not be published.