Linux

Screen -X exec 命令在手動附加之前不起作用

  • February 16, 2011

我有一個批處理腳本,可以在螢幕內啟動一個 java 伺服器應用程序。該命令如下所示:

cd /dir/ && screen -A -m -d -S javascreen java -Xms640M -Xmx1024M -jar javaserverapp.jar nogui

執行批處理腳本後,它會啟動伺服器並將其放入正確的螢幕中。如果我之後列出我的螢幕,我會看到如下內容:

user@gtwy /dir $ screen -list
There is a screen on:
       16180.javascreen       (Detached)
1 Socket in /var/run/screen/S-user.

但是,我有第二個批處理腳本,它將自動命令發送到此伺服器並在不同的 crontab 間隔上執行。由於應用程序的工作方式,我像這樣向它發送命令(該命令告訴它提醒連接的使用者“測試 123”):

screen -X exec .\!\! echo say testing 123

我也試過:

screen -R -X exec .\!\! echo say testing 123
screen -S javascreen -X exec .\!\! echo say testing 123

不幸的是,這些命令不起作用。他們甚至不給我錯誤資訊,他們什麼也不做。

但是 - 如果我首先手動附加到螢幕(使用以下命令)然後分離,現在我可以完美地執行上述任何命令。如果我在這裡不夠清楚,我可以用影片展示這一點。

screen -r -d

提前致謝。

更新:這裡是 /etc/screenrc 的重要部分。它應該完全是香草,我從未編輯過這個文件。

# VARIABLES
# ===============================================================
# No annoying audible bell, using "visual bell"
# vbell on                              # default: off
# vbell_msg "   -- Bell,Bell!! --   "   # default: "Wuff,Wuff!!"

# Automatically detach on hangup.
 autodetach on                         # default: on

# Don't display the copyright page
 startup_message off                   # default: on

# Uses nethack-style messages
# nethack on                            # default: off

# Affects the copying of text regions
 crlf off                              # default: off

# Enable/disable multiuser mode. Standard screen operation is singleuser.
# In multiuser mode the commands acladd, aclchg, aclgrp and acldel can be used
# to enable (and disable) other user accessing this screen session.
# Requires suid-root.
 multiuser off

# Change default scrollback value for new windows
 defscrollback 1000                    # default: 100

# Define the time that all windows monitored for silence should
# wait before displaying a message. Default 30 seconds.
 silencewait 15                        # default: 30

# bufferfile:   The file to use for commands
#               "readbuf" ('<') and  "writebuf" ('>'):
 bufferfile            $HOME/.screen_exchange
#
# hardcopydir:  The directory which contains all hardcopies.
# hardcopydir           ~/.hardcopy
# hardcopydir           ~/.screen
#
# shell:  Default process started in screen's windows.
# Makes it possible to use a different shell inside screen
# than is set as the default login shell.
# If begins with a '-' character, the shell will be started as a login shell.
# shell                 zsh
# shell                 bash
# shell                 ksh
 shell -$SHELL

# shellaka '> |tcsh'
# shelltitle '$ |bash'

# emulate .logout message
 pow_detach_msg "Screen session of \$LOGNAME \$:cr:\$:nl:ended."

# caption always " %w --- %c:%s"
# caption always "%3n %t%? @%u%?%? [%h]%?%=%c"

# advertise hardstatus support to $TERMCAP
# termcapinfo  * '' 'hs:ts=\E_:fs=\E\\:ds=\E_\E\\'

# set every new windows hardstatus line to somenthing descriptive
# defhstatus "screen: ^En (^Et)"

# don't kill window after the process died
# zombie "^["

#screen (Freenode) 上的 Sadrul 給出了這個答案,並且成功了:

使用 -X 命令,也嘗試指定視窗編號(例如 screen -p0 -X …)

引用自:https://serverfault.com/questions/235067