-2

I have a Ubuntu Linux Server 14.04 LTS which is running a Minecraft server.
I want it to reboot every 6 hours. I have that now and after the reboot, it starts the Minecraft server.

Now what I want. I have the server console in a screen session named cds_screen. Is there a way to do something like screen -d -r cds_screen && say Server is restarting && sudo reboot?
The server runs as jebg user and not as root so can anyone help me?

n.st
  • 8,128

1 Answers1

3
  1. If you have to reboot your server every 6 hours, you are probably doing something wrong.
    If you're doing this because of memory leaks in Minecraft or something like that, you might want to consider only restarting Minecraft, not the whole system.

  2. You can send keystrokes to a screen session "from the outside". (Searching really does wonders sometimes…)
    If the screen session is running as a different user than the current one, you will have to preface that command with su <user> -c and enclose it with quotes.


Edit: There's even a Minecraft-related question here on U&L Stack Exchange that does almost exactly what you want.

n.st
  • 8,128
  • can i do the su user -c in the /etc/crontab file? – BSteurful Jul 06 '14 at 10:46
  • @BSteurful When you're using /etc/crontab, you could just specify the correct user instead of using su. The Minecraft user wouldn't be able to initiate the reboot, though, so you would need two crontab entries. This in turn will allow you to send the "restart warning" a few minutes before the actual reboot (which is far less frustrating for your users). – n.st Jul 06 '14 at 10:50
  • Yes I understand. I try to say it 5 minutes before but that doesnot work also – BSteurful Jul 06 '14 at 10:58
  • @BSteurful Can you be more specific on how it doesn't work? – n.st Jul 06 '14 at 10:59
  • Well, it totally does not do anything. I have: 5 * * * * jebg screen -d -r cds_screen && say TEST and that does not work – BSteurful Jul 06 '14 at 11:04
  • well, he does something. He laggs for a second but he does not send the command – BSteurful Jul 06 '14 at 11:06
  • @BSteurful Two things: Are you aware that that cronjob will only run at 5 minutes past the hour, not every 5 minutes? And the way you're currently running it, say is interpreted as a shell command, instead of being sent to the Minecraft server. If you had read the link I gave in my answer, you should have figured out that you will need something like screen -S cds_screen -X stuff "say TEST$(printf \\r)" – n.st Jul 06 '14 at 11:11
  • I figured that out now but it still wont work. – BSteurful Jul 06 '14 at 11:15
  • @BSteurful What does your crontab line look like now? – n.st Jul 06 '14 at 11:16
  • jebg screen -S cds_console -X stuff 'say TEST' – BSteurful Jul 06 '14 at 12:37
  • @BSteurful You either haven't copied the whole line or you have created an invalid line (it's lacking the time/date specifiers). Also, you left out the $(printf \\r), so your crontob "types" the command into the Minecraft server's prompt, but doesn't "press Enter" to execute it. Note that you will need double quotes (") instead of single quotes (') for the printf to work. – n.st Jul 06 '14 at 15:03
  • okey so no i have this: 28 17 * * * jebg screen -S cds_console -X stuff "say TEST$(printf \r)" But it still wont work – BSteurful Jul 06 '14 at 15:19
  • okey it works now but he does not press auto enter he adds say TESTr – BSteurful Jul 06 '14 at 15:40
  • Does anyone know how to fix becuase it became very anoying – BSteurful Jul 06 '14 at 15:56
  • okey i looked it up and I replaced $(printf \r) with ^M and now it finally does work – BSteurful Jul 06 '14 at 15:59