Restart script (os.restart)
Declaration
ok, err = os.restart([ scriptPath ])
Parameters
- scriptPath
String, optional. If a valid script file path is provided, the runtime restarts into that target script. Default: "" (current script).
Returns
- ok
Boolean. Returns false only when a target path is provided and the operation fails. On success, this function does not return because the process restarts. - err
String. Error message when the operation fails.
Notes
Without the parameter, the function immediately restarts the current script process; the current script ends right away.
With a valid script path, the current script ends and the target script starts.
On failure (usually due to invalid arguments), the function returns false with an error message.
Cautions
"Current script" means the script instance that was launched, not the current file on disk. If the file changes after launch,
os.restart()
will NOT run the modified file.
Avoid using this function in multi-threaded contexts if possible.
Zero-delay restarts may cause other logical issues; handle accordingly.
Example 1
os.restart() -- restart into the "current script" (not the "current script file")
Example 2
os.restart(utils.launch_args().path) -- restart into the current script file
Note: Uses utils.launch_args