Watching logs on 3 remote boxes and SSH’ing into one of them - one command, thanks to Emacs :D
I have a repeated use case where I’m interested in the logs of three different remote boxes, and I also need to SSH into one of them to run a shell script. I got fed up with doing this multiple times a day (
ssh box1;tail -f log;ssh box2; tail -f log;ssh box3; tail -f log;. shellscript.sh gets oooold), so I went about making life easier for myself, fumbling and stumbling through Elisp on the way :)
First things off, I want to be able to open the catalina.out files in Emacs, and not have to manually use an SSH session to tail them each one by one. The following tells Emacs to add an entry to its
auto-mode-alist variable - namely, that files matching a particular regex should be using auto-revert-tail-mode
(setq auto-mode-alist (cons '("\\.out$" . auto-revert-tail-mode) auto-mode-alist))
So, now whenever I open a .out file, it’s in Emacs’s built in tail-mode. This also works in conjunction with TRAMP mode, at no extra cost to me; nifty.
Okay, great so now I can tail things inside of Emacs.
For a couple days, I set the 3 log files up as bookmarks, so that they were each a shortcut away - I’m using
C-c b j to jump to bookmarks, and then I’d have to type a few letters to distinguish each log from each other. Down to 3 keystrokes + 3 more per log file to get to three files on three remote boxes, but it’s still too much work! I’m super lazy, not just normal-person lazy.
So, the final part is just to write an elisp function composed of a simple set of commands - open a new frame so I don’t lose my existing window set up, split it into 4 parts, open the remote log files in 3 of the windows, and in the 4th, open an ssh session to the last box.
(defun open-qa-catalina () (interactive) (make-frame-command) (delete-other-windows) (split-window-horizontally) (split-window-vertically) (split-window-vertically) (balance-windows) (find-file "/ssh:remote1:/opt/tomcat/logs/catalina.out" t) (end-of-buffer) (other-window 1) (find-file "/ssh:remote2:/opt/tomcat/logs/catalina.out" t) (end-of-buffer) (other-window 1) (find-file "/ssh:remote3:/opt/tomcat/logs/catalina.out" t) (end-of-buffer) (other-window 1) (cterm) )
Not sure if there’s a better way to do that stuff, as it looks repetitive, but frankly now that all I have to do is
M-x open-, I’m not really too concerned. And that’s even before binding it to a single keystroke :D
So now, 6 keystrokes to get to 3 remote log files and ssh’d into a remote box. Winner :)