Daily restart times (Linux + Screen)

Hey,
I’m hosting my GMod Servers on my debian 7.8 linux machine.
I want to have daily restart times and I’m wondering whether anybody could tell me how to do this?
I think I need cron for this?
I’m using screen for running the servers.
Regards,
Pascal

You should make script, that runs every x times, which runs set of commands, but i think the problem is Detaching the screen as you have to do CTRL + A + D but theres no such command.

Yeah stat is the thing. I don’t now how I could run “exit” in my server via a script.

You should find maybe some server managment webpanels, they should have probably done this feature. Or just look how did they do it. I dont remember how, you could make permanent script, just make it run screen -r, CTRL + C, ./yourserverstart.sh, CTRL + A + D, and that should do it.

doing CTRL + C is a bad idea, I want to close the server with exit

[editline]29th August 2015[/editline]

But thanks for your help, I will try to find a solution :slight_smile:

Just develop something with C# or something else which does it with RCon.

Actually the answers above are not really good (bull****) (why write a C# script on a linux server?).

There are basically two Methods:

  1. Using SCREEN
  • Write a start.sh which looks like this


#!/bin/bash
screen -A -m -d -S garrysmod ./srcds_run -game garrysmod -port 27015 +maxplayers 22 +map gm_flatgrass +gamemode sandbox +rcon_password please_dont_specifyit_in_yourserver.cfg -autoupdate

And make it chmod +x startscriptname . You will be able to run your server with ./startscript, and view the screen with screen -x garrysmod (or screen -r garrysmod, use x if multiple users should view)
The -autoupdate option is important, as it will restart your server when you type exit.

  • Now make a cronjob, type crontab -e with your gameserver user (not root)

* */6 * * * screen -S garrysmod -X stuff 'exit'`echo -ne '\015'`

When using the editor nano, you can save it by typing ctrl + x
This will send exit to your screen and so your server will restart every 6 hours: 00:00 6:00 12:00 18:00 etc… (6 hours is the maximum time a server should be online, as proposed on the wiki: http://wiki.garrysmod.com/page/Global/CurTime)

  1. Using a restart script
    You can also grab a restart script (I have written one particularly for garrysmod, if you want I will post it here).
    Normally they work by ./script start ./script restart etc…
    The drawback is that you can’t type into the console but still read it (depends on script)
    You can restart it then similar…

* */6 * * * /path/to/restartscript/script.sh restart

Thanks for the really useful answer!
So I have a few questions:
I’m currently storing my password in the server.cfg. Should I put in in my start command line and why?
What exactly does the -autoupdate thing? It will just restart the server when I type in ‘exit’? Nothing more?
Then could you explain the screen command?

Putting rcon_password in the server.cfg is a bad idea in my opinion, as exploits showed up where it’s possible to download files from server like server.cfg and so users will be able to get your password. Also it’s a good idea to write sv_allowdownload 0 and sv_allowupload 0 to the server.cfg, this will disable downloading from server (but downloading from fastdl is still possible in garrysmod).
I don’t really know if the exploit still exists, but similiar seem to have shown up multiple times (type rcon_password exploit allowupload into google). So I wouldn’t risk it.

The autoupdate will only restart your server on crash or exit, however it has also the capability to update your server, but you will need to specify -steambin etc. on the command line (see ./srcds_run -help).

The screen parameters:
-A will adapt the screen terminal size (You can omit it, I’m using it by habit)
-m will create the screen regardless if it’s in another screen
-d will detach it instantly. So you won’t see the server starting, omit this option if you want to see it and detach then with ctrl + a + d
-S will allow you to name the screen (in this case it gets named garrysmod)

Basically it will start your server, name it garrysmod, and hide it instantly.

Great thanks! I will try this out asap! :slight_smile:
The only thing I can’t understand atm is the screen -S garrysmod -X stuff ‘exit’ thing

Ah yes.
-S refers to the screen name. So it will use the screen we opened before.
-X is for sending general commands to screen
stuff will write the string specified “stuff it” to the stdin of the screen, also the input.
So exit gets written into stdin. But now it’s only written into the screen, you still need to send it (press enter). echo -ne '\015' does it by sending a special control character. Instead of echo (which makes problems on some shells) you can also use print, like this: -X stuff “exit$(printf \r)”