0

I have very little experience with scripting, but I'd like to get into it.

Basically, this is for a StarMade server, as its java process doesn't shut down on a crash but rather remains running, so it becomes hard to properly detect crashes, and these occur very frequently.

What I want though, is to find a specific line in the server log file that indicates the server is frozen/has crashed, and kill the process after reading this line, then another script would start it up again (got that part already). The log file continuously gets updated by the server, so the script basically has to "watch" the file somehow.

I couldn't find any exact matches for this idea anywhere, but I tried to scrape something together from many sources, although I'm pretty sure it's entirely wrong:

#!/bin/bash
cd "$(dirname "$0")"
if grep -Fxq "[SERVER] SERVER SHUTDOWN" log.txt.0
then
    kill -9 $(pidof StarMade.jar)
else
    false
fi

As far as I can tell, this should continuously check the file log.txt.0 to find direct matches with "[SERVER] SERVER SHUTDOWN" and kill the server process when it does, or do nothing when it doesn't, is this a correct way to do it? I don't want to break things by stupid mistakes.

slugster
  • 49,403
  • 14
  • 95
  • 145
Archer
  • 3
  • 1
  • 3

1 Answers1

0

Try this:

if ( grep -Fxq "[SERVER] SERVER SHUTDOWN" log.txt.0 > /dev/null ); then
    kill -9 $(pidof StarMade.jar)
fi
paddy
  • 60,864
  • 6
  • 61
  • 103
  • I'll try it, but the downside is that I can only find out if it works if the server crashes, so it could take some time. – Archer Nov 07 '13 at 04:38
  • `grep -q` wouldn't write anything to `STDOUT`. I'm wondering what's being redirected to `/dev/null` in that case. – devnull Nov 07 '13 at 04:45
  • @devnull Thanks - I didn't look at the parameters. In that case, the redirection is unnecessary. I don't think anything else is being redirected. – paddy Nov 07 '13 at 04:55
  • @Archer You could construct a log file with the relevant phrase, and test the script. You don't have to wait for a crash. – paddy Nov 07 '13 at 04:56
  • I have a log file containing the string I want it to find, but when running the script the console tells me how the kill command works instead, not sure what's wrong with it. (So it probably grabs the string correctly) – Archer Nov 07 '13 at 05:08
  • So found out that it didn't really like the $(pidof StarMade.jar) part, although the process is running via that jar. Any particular reason why? – Archer Nov 07 '13 at 05:25