Restart Server using lua

Is there any way I can restart / stop a server using server side lua? I’ve tried doing RunConsoleCommand(‘exit’), as well as game.ConsoleCommand(“exit”) but it seems to block that. The “killserver” command works but it does not have the behavior that I would like it to, where a script running in the background can restart the server when it stops. Any help is appreciated, thank you.

You can do a trick using a cfg file. For example, one of the automatically executed CFG files that is unused in garrysmod is network.cfg

garrysmod/cfg/network.cfg, If it doesn’t exist you can create it.

Lua blocks console commands based on string and not the command reference.

So put this in the network.cfg file.

 alias restart_server "exit" 

Now, you’ll be able to use restart_server to exit your server.

[lua] RunConsoleCommand(“restart_server”) [/lua]


You can just do RunConsoleCommand( “_restart” ), which is a native command that works fine.

I had no idea this was a thing… seems to be working. Thank you!

:frowning: I’ve had trouble in the past where it hangs the server.

I’ve only used it a few times and don’t remember having an issue. Dunno :confused:

I’ve had it run daily for a few months with no issues.