0

This ServerRestart.sh was recommended to me to restart my game server on critical failure.

while true; do
    LD_LIBRARY_PATH=lib ./Server
done

I "improved" it with this to delete the logs directory on each restart.

while true; do
    rm -r /home/_jc3105/logs
    LD_LIBRARY_PATH=lib ./Server
done

This particular server will restart if you delete the logs directory while it is running. My improvement causes it to sometimes double restart So i think this is the solution.

while true; do
    sleep 4
    rm -r /home/_jc3105/logs
    LD_LIBRARY_PATH=lib ./Server
done

But i suspect it will also have a new problem. That sometimes for a brief second it falsely thinks the server needs restarting when it actually does not which was not a problem until i introduced the folder delete. Which if triggered will cause the server to restart as a result of deleting the logs files.

I want to auto restart the Game Server using a script.sh. I'd like it to first wait. then recheck the true? condition.

Inian
  • 80,270
  • 14
  • 142
  • 161
WerewolF
  • 39
  • 7
  • 1
    Are you sure you want to be deleting the log if the server has crashed with a failure? Wouldn't you want to see the logs in that case? – lxop Nov 28 '17 at 09:05
  • Is the line `./Server` blocking, or does the server fork to background? If it is blocking, your script only ever does something after the server stopped. If not, you should use another mechanism, like PID file and checking running processes. Or maybe just use systemd. – Karsten Koop Nov 28 '17 at 09:07
  • @lxop Seems self-defeating, yes. But not the question. The question: How do I ask if the while true; do case is still true after it waits 4 seconds? – WerewolF Nov 28 '17 at 09:37
  • @karsten-koop the server runs in the background. I've heard of systemd, looked in the manuals for systemd, systemctl, no progress toward doing this with either resulted. So the question was: How do I ask if the while true; do case is still true after it waits 4 seconds? – WerewolF Nov 28 '17 at 09:39
  • You want to figure out if the server process is still running. This is a massive FAQ with hundreds of duplicates on Stack Overflow, but one of them is https://stackoverflow.com/questions/18765298/how-to-get-pid-given-the-process-name – tripleee Nov 28 '17 at 09:53
  • Do you want to check if `true` returns `true`, because thats your current condition? It runs /bin/true and returns true every time, unless you create a symlink on it from /bin/false. :D – ntj Nov 28 '17 at 09:56
  • @tripleee I did search extensively before posting. Your link is completely unrelated to my question. – WerewolF Nov 28 '17 at 09:56
  • My link explains what you need to do in order to avoid killing an already-running server process. – tripleee Nov 28 '17 at 09:58
  • @ntj `while true` is a completely transparent and idiomatic way to run an endless loop -- much better than the convoluted `while [ "nonempty-string" ]` or even `while [ 1 ]` (or `while [ true ]`!) hacks you see a lot. – tripleee Nov 28 '17 at 09:59
  • @ntj "while true; do" Triggers once the server is no longer functioning. I want to wait 4 seconds then ask that same question. I do not know how to ask it again clearly. "if true; do rm -r /home/_jc3105/logs done" Is this right? Do you know? – WerewolF Nov 28 '17 at 09:59
  • @tripleee read it again.. Still unrelated and/or not implementable from my skill level therefore not a duplicate. – WerewolF Nov 28 '17 at 10:02
  • I'm not saying it's a duplicate (I would have closed it already if I thought it was). I'm telling you how to solve your problem. Welp, posted an answer with that now. – tripleee Nov 28 '17 at 10:04
  • @WerewolF it's just strange that You want this, but ok than, you can do it with if but I write what I usually use. – ntj Nov 28 '17 at 10:06
  • @tripleee I know, I use that frequenty as well. – ntj Nov 28 '17 at 10:07
  • @triplee thanks for your time and the answer looks good, will try it in a few hours and let you know. – WerewolF Nov 28 '17 at 10:09

1 Answers1

1

If the server forks and remains running in the background, your while loop will run another iteration as soon as the server has started. You want to put in a condition to avoid killing a perfectly healthy running server.

while true; do
    if ! pidof Server; then
        rm -r /home/_jc3105/logs
        LD_LIBRARY_PATH=lib ./Server
    fi
    sleep 5
done

If the server doesn't show up as Server in a ps listing, you will need to adjust the argument to pidof, obviously.

There is no need to sleep after removing the logs. rm will remove the files, and this operation is done when rm finishes. The other sleep I put in just to avoid executing this in a spin lock (thousands of times per second).

tripleee
  • 175,061
  • 34
  • 275
  • 318
  • Depending on the server, checking that it's live every 5 or 10 minutes may well be quite sufficient. Every 5 seconds actually feels like a bit of overkill. Maybe tweak the number to find a good balance. – tripleee Dec 03 '17 at 12:27