0

We have a Server Startup Script that is calling a Mate Terminal (or GNOME Terminal - should not make a difference) as Part of the Server Startup process which is, in essence, tailing the Servers info log like that:

mate-terminal --title="APPSERVER LOG" --working-directory=$SERVER_LOCATION --hide-menubar -e "sh -c 'tail -f --retry -n 400 ./server.log'" &

Now as you might imagine, while developing one ends up with quite a couple of open terminals all tailing the same logfile at the end of the day, caused by several server restarts.

Now my quastion: Is it possible to somehow check the open terminals for a specific running command, a specific title or similar, to only open a new terminal if there is not already one tailing?

I checked the man pages of mate-terminal but could not find anything that does any Window Management.

We are running CentOS7 and MATE 1.12.1

cat /etc/*release
CentOS Linux release 7.2.1511 (Core) 
achschneid
  • 67
  • 7

2 Answers2

1

You could try to grep pid of mate like this:

pgrep -d " " -f path/to/mate/terminal/binary

And from there you decide if is necessary to open another, kill existing one, or whatever you want :)

kitz
  • 879
  • 2
  • 9
  • 24
  • the thing is that `ps aux | grep "mate-terminal"` only lists one mate-terminal instance, despite I currently have >10 Terminal Windows open...? `$ ps aux | grep mate-terminal user+ 26601 0.1 0.2 620616 25924 ? Sl 10:40 0:02 mate-terminal --working-directory=/home -e sh -c "./skript"` `pgrep` also only lists the pid of one terminal, which is not the one that I am looking for. So it seems like MATE does not spawn new processeses for each Terminal Window? – achschneid Mar 16 '16 at 10:25
  • 1
    Ok my bad, the way we call the tail via `sh -c` will result in the tail being listed as `user+ 26697 0.0 0.0 107932 688 pts/1 Ss+ 10:40 0:00 tail -f --retry -n 400 ./server.log` hence no hit for "mate" or "mate-terminal". Anyways I should be able to grep for the right tail. Thank you very much! – achschneid Mar 16 '16 at 10:41
0

For what its worth: Based on @kitz Answer I now ended up to not use pgrep to get a process id and not open a new tailing window, but rather use pkill to close the existing one and tail again. The reason is, that the logfile might have been deleted in the meantime, so even with --retry, which only works for the initial open, the original tail might have lost the log.

So this is it:

TAILCMD="tail -f --retry -n 400 ./server.log"
pkill -ef "$TAILCMD"
mate-terminal --title="Server Log" --working-directory=$SERVER_LOCATION --hide-menubar -e "sh -c '${TAILCMD}'" &

Thanks again @kitz for pointing me in the right direction!

achschneid
  • 67
  • 7