开发者

How to escape pipes in .screenrc for commands to run at startup?

开发者 https://www.devze.com 2023-02-25 01:37 出处:网络
I\'m 开发者_运维百科using byobu/screen, and I would like to have a new screen session default to containing a few windows set up specially for tailing a specific log file.

I'm 开发者_运维百科using byobu/screen, and I would like to have a new screen session default to containing a few windows set up specially for tailing a specific log file.

My .screenrc looks something like this (technically this is my .byobu/windows file):

chdir /home/matt/code/project/logs
screen -t 'logs' tail -F current.log

chdir /home/matt/code/project
screen -t 'errors' tail -F current.log | grep -A 3 "ERROR"

chdir /home/matt/code/project
screen -t 'project'

chdir 
screen -t 'bash'

My intention is to set up four windows in the new screen session:

  1. A window titled "logs" which tails the current.log file
  2. A window titled "errors" which tails the current.log file and greps for ERROR
  3. A window titled "project" which starts in my project's main directory
  4. A window titled "bash" which starts in my home directory.

However, the pipe in the screen -t 'errors' tail -F current.log | grep -A 3 "ERROR" command ends up being interpreted by screen literally, and thus my second window never appears.

How can I escape the pipe in this command to have it interpreted as I wish?

Furthermore, is there an easier way to setup screen/byobu to launch windows that are running (complicated) commands at startup?


I ended up solving this by using the stuff command to simulate entering a command in the window and pressing enter to execute it. This has the nice effect of making it possible to break out of the tail command in the screen window without also killing the window itself.

Here is an example of what my .screenrc looks like to accomplish this; I've written up a longer explanation on my blog:

screen -t 'errors'
stuff 'tail -F /var/ec/current.log | grep -A 3 "ERROR"^M'

(the ^M is entered by pressing Ctrl+V, Enter with your keyboard, not by actually typing caret and uppercase M)


The following works for me:

screen -t errors bash -c "tail -F current.log | grep -A 3 ERROR"

The use of bash (or other shell) is required to prevent screen from giving a "file not found"-error, which will be the result if bash -c is removed from the above.


You should be fine with creating custom script and use it in your .screenrc - so you would have screen -t 'error' ./bin/current.log.sh

And tail -F ... in current.log.sh

0

精彩评论

暂无评论...
验证码 换一张
取 消