In lua 5.2.4, when using the widely used os.execute('sleep n')
method in an infinite loop, the program can't be stopped by ^C
(Ctrl-C).
Minimal example:
while true do
print("HELLO!")
os.execute("sleep 3")
end
My questions are:
Is this expected behavior? I would have guessed the program receives the ^C
signal after returning from the os.execute
command.
Is there a "builtin" way to sleep efficiently?
Control-c is most probably being caught by the shell spawned by os.execute
, not by Lua. You need to look at the code returned by os.execute
. When the command ends normally, os.execute
returns true,"exit",rc. Otherwise, it returns nil,etc. When it is interrupted with control-c, it returns nil,"signal",2 in my machine.
Bottom line, try this code:
while true do
print("HELLO!")
if not os.execute("sleep 3") then break end
end