Advertisement

创建screen自启动脚本

阅读量:

SYNOPSIS
screen [ -options ] [ cmd [ args ] ]
screen -r [[pid.]tty[.host]]
screen -r sessionowner/[[pid.]tty[.host]]

COMMAND-LINE OPTIONS
Screen has the following command-line options:

The system comprises all capabilities except for minor exceptions within each window's termcap, even though the screen may redraw portions of the display to implement functions.

-A Adjust all window sizes to conform with the current terminal's size. In this default behavior, screen will attempt to revert to its previous window configurations when connecting to resizable terminals (those with 'WS' attributes, such as suncmd or xterm).

Replace the default config file instead of "$HOME/.screenrc" with a new file.

使用-d或-D选项指定目标tyy主机的ID。
此选项不会启动屏幕显示功能... 但会将当前运行中的其他会话... 等待连接到该tyy主机。
此选项的作用与在控制台输入' C-a d'等效。
-D等同于断开电源键。
如果无法断开会话,则此选项不起作用。
结合使用-r或-R选项可以获得更强大的效果。

-d -r Reattach a session and if necessary detach it first.

-d -R Reattach a session and if necessary detach or even create it
first.

-d -RR Reattach the session and, if necessary, detach or create it. Preferentially use the first available among multiple sessions that are available.

-D -r Reattach a session. If necessary detach and logout remotely
first.

-D -R Attach here and now.
详细说明如下:
如果当前会话处于运行状态,则进行重新连接。
必要时先断开会话并远程注销。
如果未运行则创建会话并通知用户。
这是作者的偏好设置。

-D -RR Attach here and now. Whatever that means, just do it.

Note that it is advisable to examine the session status through screen-list commands.

The -e xy sequence designates that the command character is set to x, while y represents another key used for generating literal commands when pressed after x. By default, it uses "C-a" and a, but users can customize it by entering "-e^Aa". When initiating a new screen session, this setting establishes the default command character. In a multiuser environment, any newly added users begin with this designated command character. However, when connecting to an existing session, only the attaching user's command character is modified. This option essentially mirrors either 'defescape' or 'escape'.

The commands -f, -fn, and -fa enable flow-control to be either turned on or off or set to automatic switching mode. Additionally, these settings can be configured using the 'defflow' .screenrc command.

-h num
Specifies the history scrollback buffer to be num lines high.

when this key (typically C-c) is pressed, it will immediately trigger a system interrupt if flow control is active. For detailed instructions on how to configure this behavior, refer to the 'defflow' documentation in your screen configuration file. The application strongly advises against using this option due to potential negative consequences.

-l and -ln toggle the login mode on or off (for /run/utmp updating). This can also be configured via the 'deflogin' .screenrc command.

-ls [match]
-list [match]
does not start screen, but prints a list of pid.tty.host strings
and creation timestamps identifying your screen sessions. Ses‐
sions marked detached' can be resumed with "screen -r". Those marked attached' are running and have a controlling terminal. If
the session runs in multiuser mode, it is marked multi'. Sessions marked as unreachable' either live on a different host or are
dead'. An unreachable session is considered dead, when its name matches either the name of the local host, or the specified param‐ eter, if any. See the -r flag for a description how to construct matches. Sessions marked as dead' should be thoroughly checked
and removed. Ask your system administrator if you are not sure.
Remove sessions with the -wipe option.

-L tells screen to turn on automatic output logging for the windows.

指定日志文件的命令为-logfile

-m is responsible for disregarding the $STY environment variable. By using 'screen -m', session creation is enforced, regardless of whether screen is called from within another screen session or not. This flag serves a unique purpose in relation to the -d option:

Upon launching the application in a detached manner, the system generates a new session that remains independent of the existing one. This ensures that each session operates without interference from others, which is particularly advantageous for automation tasks executed at startup.

-D -m This also launches the screen in "detached" mode when no new process is forked. The command will exit upon session termination.

-O sets the best output configuration for your terminal instead of true emulation of the VT100 terminal (specifically impacts auto-margin terminals without LP). This can be configured by setting the OP parameter in its configuration file, specifying it within a termcap entry.

选择一个窗口。此功能在需要将终端重新连接到特定窗口或通过'-X'选项发送命令到特定窗口时非常有用。与屏幕上的select命令类似地,在空窗口中使用'-'会切换到空窗口。作为重新连接的特殊情形,在空窗口中使用'=‘会显示窗口列表;而使用'+'则会创建一个新的窗口。如果无法找到指定的窗口,则该命令不会执行。

  • q命令禁止错误提示信息的显示。当与"-ls"选项结合使用时,返回状态码如下:9表示没有与会话相关的文件夹。10表示包含可运行但无法连接会话的文件夹。从11及以上表示存在一个或多个可用会话实例。当与"-r"选项结合使用时,返回状态码如下:10意味着没有会话可用于恢复。从12及以上表示存在两个或更多个可恢复的会话,并应指定选择哪一个。在所有其他情况下'-q'无Effect。

-Q 现在可以通过远程会话使用此标志来执行一些命令。例如,“screen -Q windows”。被查询的命令将有其响应发送到查询进程的标准输出中。如果一个命令出现错误,则查询进程将终止并返回非零状态。

The set of commands currently available for querying includes echo(info), lastmsg(number), select(time), title(windows), and others like info(lastmsg).

-r [pid.tty.host]

r sessionowner/[pid.tty.host] 创建了一个脱节屏幕会话。除了通过 -d 或 -D 选项组合之外,其他选项不可用(尽管可能需要添加一个前缀 [pid.] tty.host 来区分数个脱节屏保会话)。另一种形式则用于连接到另一个用户在多用户模式下的屏幕会话。这表明 screen 应该在另一个用户目录中查找会话。这要求 setuid-root。

Only when there's no ambiguity in determining which screen to attach will R resume the session; typically this occurs when only one detached screen remains. Otherwise, R lists available sessions. RR tries to resume the youngest (by creation time) detached screen session it identifies. If RR successfully resumes that session, it disregards all other command-line options. If no detached sessions are found during this process, R creates a new session using the specified options, effectively behaving as if -R wasn't used. This behavior is enabled by default when running R as a login-shell (in such cases, R actually employs "-xRR"). For scenarios involving the -d or -D options, consult elsewhere. Note: Time-based session selection was introduced by Debian.

  • --program
    -- assigns the default shell to an application specified by
    the user, rather than using the value from
    the SHELL environment variable ($SHELL or "/bin/sh" if not set).
    This configuration can also be adjusted via
    the 'shell' option in .screenrc. Please refer to [here] for further details.

-S sessionname
When establishing a new session, this feature allows users to assign a meaningful identifier. This identifier uniquely associates with specific screen operations such as "screen -list" and "screen -r". Assigning this name enables identification of the current screen context during these operations. Rather than using the default [tty.host] suffix, this option allows substitution of a custom identifier.

The command -t name assigns the title alias to either the default shell or a specified program. Additionally, you can consult the 'shelltitle' screen configuration file for further details.

-T term
Configure the $TERM environment variable as configured by a specified term rather than the default configuration for screen.

Change the terminal's encoding to UTF-8 mode. The setting instructs the terminal to interpret and send UTF-8 encoded data. Additionally, this configuration sets the default encoding for newly created windows to utf8.

-v Print version number.

  • wipe [match]
    Functions akin to "screen -ls", yet it eradicrates terminated sessions rather than labeling them as "dead". An unattainable session is deemed dead when its identifier aligns with either the local machine's hostname or an explicitly provided parameter, provided such a parameter exists. Refer to "-r" for an elucidation on crafting these matches.

  • x should be attached to a non-detached screen session in multi-display mode. The screen refuses to auto-append itself but will handle the connection correctly when using cascaded multiple displays; ensure proper configuration before proceeding.

Issue a specified instruction in an active screen session. If you have multiple screen sessions active, utilize the -S option to designate which one you wish to interact with. The -d or -r options allow you to configure screen's search scope, limiting its search to only those attached or detached sessions. It's important to note that this command cannot function if the session is password protected.

-4 Resolve hostnames only to IPv4 addresses.

-6 Resolve hostnames only to IPv6 addresses.

CUSTOMIZATION

根据上述说明,默认键绑定通常由一个"C-a"键后跟一个其他字符组成。为了方便您的使用,请注意以下几点:所有标有小写字母的命令也与它们的控制字符互相对应(除了"C-a a"这一例外情况,请参阅第【CUSTOMIZATION

Below is a table that lists the default key bindings. In boxes containing multiple keystroke entries, the trailing commas serve as separators and should not be considered as part of the key bindings.

C-a ' (select): 显示一个窗口提示,请选择其名称或编号以切换窗口。
C-a " (windowlist -b): 列出所有可选窗口供您选择。
C-a digit (select 0-9): 切换到编号为 0-9 的窗口。
C-a -(select '-'): 切换到编号为 0-9 的窗口之一;或者切换到空白窗口。
C-a tab (focus): 将输入焦点切换到下一个区域;有关更多操作,请参见 split、remove 和 only 命令。
C-a C-a (other): 拷贝上一个显示的窗口内容(除非有特殊设置)。
C-a a (meta): 发送当前键(即 C-A)至目标窗口;有关逃脱操作,请参见相关帮助信息。

C-a A (title) Enable the user to request
a name for
the current window.
C-a b, (break) Issue an operation
to send a break command
to the C-a C-b window.
C-a B (pow_break) Reopen the terminal
line and send a
break command.
C-a c, (screen) Issue an operation
to create a new
window with
a shell application and
switch to that
window.
C-a C (clear) Erase the screen content.

C-a d, (detach) Detach screen from
C-a C-d this terminal.
─────────────────────────────────────────────────────────
C-a D D (pow_detach) Detach and logout.
─────────────────────────────────────────────────────────
C-a f, (flow) Toggle flow on, off
C-a C-f or auto.
─────────────────────────────────────────────────────────
C-a F (fit) Resize the window
to the current re‐
gion size.
─────────────────────────────────────────────────────────
C-a C-g (vbell) Toggles screen's
visual bell mode.
─────────────────────────────────────────────────────────
C-a h (hardcopy) Write a hardcopy of
the current window
to the file "hard‐
copy.n".
─────────────────────────────────────────────────────────
C-a H (log) Begins/ends logging
of the current win‐
dow to the file
"screenlog.n".
─────────────────────────────────────────────────────────
C-a i, (info) Show info about
C-a C-i this window.
─────────────────────────────────────────────────────────
C-a k, (kill) Destroy current
C-a C-k window.
─────────────────────────────────────────────────────────

C-a l, (重显) 完成了对当前窗口的完全刷新。
C-a C-l 重新显示窗口。
─────────────────────────────────────────────────────────
C-a L (登录) 切换当前窗口的状态。
该功能仅在屏幕配置允许更新utmp数据库时可用。
─────────────────────────────────────────────────────────
C-a m, (lastmsg) 显示上一条消息。
该消息将被显示在消息行中。
──
C-a M (监视器) 开启/关闭对当前窗口的监视功能。
──
C-a space, (下一项) 转到下一个窗口。
C-a n, (下一个) 显示下一个窗口内容。
──
C-a C-n
──
C-a N (编号) 显示当前窗口的编号及名称。
──
C-a backspace, (上一项) 转到上一个窗口(与下一个相对应)。
使用上箭头键←或Ctrl-P切换回上一个窗口。
──
C-a C-h, vprevious window(相反位置).
──
C-a p, (prev) 跳转至前一个窗口(与下一个相对应).
使用上箭头键←或Ctrl-P切换回上一个窗口.
──
C-a C-p
──
C-a q, (XON) 向当前窗口发送XON信号.

C-a Q (only) Delete all regions
but the current
one. See also
split, remove, fo‐
cus.
─────────────────────────────────────────────────────────
C-a r, (wrap) Toggle the current
C-a C-r window's line-wrap
setting (turn the
current window's
automatic margins
on and off).
─────────────────────────────────────────────────────────
C-a s, (xoff) Send a control-s to
C-a C-s; the current window.
─────────────────────────────────────────────────────────

C-a S (split) 将当前区域水平分割为两个新的区域。
见:仅、re-移动、聚焦。
C-a t, (time) 显示系统信息。
C-a v (version) 显示版本号以及构建日期。
C-a C-v (digraph) 输入双字符指令。
C-a w, (windows) 显示可用窗口列表。
C-a C-w 显示可用窗口名称。
C-a W (width) 切换80列或132列模式。
C-a x 或 C-a C-x (lockscreen) 锁定当前终端。
见:分割、仅、聚焦。
C-a X (remove) 删除当前区域;见:分割、仅、聚焦。
C-a z, (suspend) 悬停屏幕;需确认目标系统支持BSD风格作业控制功能(Job control)。
C-a Z (reset) 将虚拟终端恢复至开机初始状态;见:清除虚拟终端内容(termcap).
C-a . (dumptermcap) 生成并输出".termcap"文件.
C-a ? (help) 显示绑定信息快捷键列表.
见:帮助信息快捷键.
C-a \ (quit) 关闭所有窗口并终止屏幕服务;需注意该指令会永久关闭终端并终止其服务进程.
见:退出当前窗口并终止屏幕服务.
按 colon进入命令行模式

C-a [, (copy) Enter copy/scroll‐
C-a C-[, back mode.
C-a esc

C-a ] 将剪贴板中的内容传输至当前窗口的标准输入队列。
C-a {, (历史记录) 执行上一条历史命令。
C-a } 复制并粘贴上一条命令。
C-a > 将剪贴板中的内容输出到指定文件。
C-a < 从屏幕上提取交换文件到剪贴板中。
C-a = 删除最近使用的文件。
C-a , 显示屏幕的历史来源、目的地及其使用许可信息。
C-a _ 启用/停止当前窗口的 inactive 监控(仅适用于 X 环境)。
C-a | 垂直分割当前区域为两个新区域。
C-a * 列出当前连接的所有显示器信息。

示例:

复制代码
 C-a [ -> 进入 copy mode,在 copy mode 下可以回滚、搜索、复制就像用使用 vi 一样

    
     C-b Backward,PageUp
    
     C-f Forward,PageDown
    
     H(大写) High,将光标移至左上角
    
     L Low,将光标移至左下角
    
     0 移到行首
    
     $ 行末
    
     w forward one word,以字为单位往前移
    
     b backward one word,以字为单位往后移
    
     Space 第一次按为标记区起点,第二次按为终点
    
     Esc 结束 copy mode
    
 C-a ] -> paste,把刚刚在 copy mode 选定的内容贴上
    
    
    
    
    代码解释
复制代码
 编辑/etc/screenrc

    
 添加logfile /tmp/screenlog_%t.log
    
 创建一个窗口为windows_name 的会话为session_name
    
 screen -L -t windows_name -dmS session_name
    
 此时会在tmp目录创建一个screenlog_windows_name.log的文件记录session_name打印内容
    
    
    
    
    代码解释
复制代码
 screen -dmS yourname -> 强制新建一个叫yourname的session

    
 screen -ls         -> 列出当前所有的session
    
 screen -r yourname -> 回到yourname这个session
    
 screen -d yourname -> 远程detach某个session
    
 screen -d -r yourname -> 结束当前session并回到yourname这个session
    
    
    
    
    代码解释

stty

修改终端命令行的相关设置

[-]icanon 启用特殊字符:erase, kill, werase, rprnt

min N with -icanon, set N characters minimum for a completed read

time N with -icanon, set read timeout of N tenths of a second

screen调用命令

复制代码
 参数:

    
 调用命令
    
 bash -c  在后台调用命令
    
 调用脚本
    
 -S 指定会话
    
 -X  发送命令 
    
 stuff  输出命令执行 (例如stuff $'\n' 执行回车)
    
 stuff 'command'`echo -ne '\015'` (执行command命令后回车)
    
 -p 指定屏幕(例如-p 0 指定屏幕)
    
 -x 连接断开会话(可忽略) 
    
  
    
    
    
    
    代码解释
复制代码
 screen -dmS test;

    
 screen -S test -p 0 -X stuff $'stty -icanon min 0 time 30 \n\n';
    
 screen -S test -p 0 -X stuff $'\n';
    
 screen -S test -p 0 -X stuff $'bash /0/sh/init.sh\n';
    
 screen -ls;
    
    
    
    
    代码解释
复制代码
    crontab配置
    
    
    代码解释

""代表所有的取值范围内的数字,如月份字段为,则表示1到12个月;

"/"代表每一定时间间隔的意思,如分钟字段为*/10,表示每10分钟执行1次。

“-” 表示从某个区间范围(闭区间)。例如,“a - b" 即代表 ab 之间的所有整数;在时间字段中,默认设置为每隔两小时触发任务,在 023 点的时间段内执行。

","分散的数字(不一定连续),如1,2,3,4,7,9。

示例:

复制代码
 crontab -e

    
 # 每隔2天的上午8-11点的第3和15分钟执行command
    
 3,15  8-11  */2  *   *  command
    
  
    
 /etc/init.d/cron status
    
 ● cron.service - Regular background program processing daemon
    
      Loaded: loaded (/lib/systemd/system/cron.service; enabled; vendor preset: enabled)
    
      Active: active (running) since Fri 2021-07-30 20:57:27 CST; 2h 24min ago
    
    Docs: man:cron(8)
    
    Main PID: 568 (cron)
    
       Tasks: 1 (limit: 4488)
    
      Memory: 967.0M
    
      CGroup: /system.slice/cron.service
    
          └─568 /usr/sbin/cron -f
    
  
    
 crontab -l
    
    
    
    
    代码解释
复制代码
 示例代码:

    
 # m h  dom mon dow   command
    
 0 10,22 * * *   /usr/sbin/reboot
    
 3 10,22 * * *   cd /home/aio && screen -L -t btc -dmS btc bash -c '/usr/bin/python3 src/main.py config.json'
    
 4 10,22 * * *   cd /home/aio && screen -L -t eth -dmS eth bash -c '/usr/bin/python3 src/main.py config.json'
    
    
    
    
    代码解释

全部评论 (0)

还没有任何评论哟~