-1

I use screen to run a minecraft server .jar, and I would like to write a bash script to see if the most recent line has changed every five minutes or so. If it has, then the script would start from the beginning and make the check again in another five minutes. If not, it should kill the java process.

How would I go about getting the last line of text from a screen via a bash script?

Vadim Kotov
  • 8,084
  • 8
  • 48
  • 62

2 Answers2

0

If I have understand, you can redirect the output of your program in a file and work on it, with the operator >. Try to run :

ls -l > myoutput.txt

and open the file created.

nan
  • 73
  • 4
0

You want to use the tail command. tail -n 1 will give you the last line of the file or redirected standard output, while tail -f will keep the tail program going until you cancel it yourself.

For example:

echo -e "Jello\nPudding\nSkittles" | tail -n 1 | if grep -q Skittles ; then echo yes; fi

The first section simply prints three lines of text:

Jello
Pudding
Skittles

The tail -n 1 finds the last line of text ("Skittles") and passes that to the next section.

grep -q simply returns TRUE if your pattern was found or FALSE if not, without actually dumping or outputting anything to screen.

So the if grep -q Skittles section will check the result of that grep -q Skittles pattern and, if it found Skittles, prints 'yes' to the screen. If not, nothing gets printed (try replacing Skittles with Pudding, and even though it was in the original input, it never made it out the other end of the tail -n 1 call).

Maybe you can use that logic and output your .jar to standard output, then search that output every 5 minutes?

OnlineCop
  • 4,019
  • 23
  • 35