我在Linux服务器上的屏幕会话中有一个长期运行的服务器进程。 这有点不稳定(可悲的是不是我的软件,所以我不能解决这个问题!),所以我想脚本夜间重新启动的过程,以帮助稳定。 使其正常关机的唯一方法是进入屏幕进程,切换到正在运行的窗口,并在其控制台上inputstring“stop”。
是否有任何智能redirect扭曲,我可以做一个cronjob每天在固定的时间发送停止命令?
这个答案并不能解决问题,但是因为有30多人觉得这个问题很有用 ,所以还是留在这里 ,否则我很可能会在很久以前删除它。
写入程序的/proc/*pid of the program*/fd/0
。 fd
子目录包含所有打开文件的描述符,文件描述符0
是标准input(1是标准输出,2是标准错误)。
1号航站楼
[ciupicri@hermes ~]$ cat xxx
2号航站楼:
[ciupicri@hermes ~]$ pidof cat 7417 [ciupicri@hermes ~]$ echo xxx > /proc/7417/fd/0
像这样启动服务器:
# screen -d -m -S ServerFault tr az AZ # replace with your server
屏幕将以分离模式启动,因此如果您想查看正在进行的操作,请运行:
# screen -r ServerFault
像这样控制服务器:
# screen -S ServerFault -p 0 -X stuff "stop^M" # screen -S ServerFault -p 0 -X stuff "start^M" # screen -S ServerFault -p 0 -X stuff "^D" # send EOF
(这个答案是基于从Unix和Linux兄弟网站发送文本input到分离的屏幕 )
参数说明 :
-d -m Start screen in "detached" mode. This creates a new session but doesn't attach to it. This is useful for system startup scripts. -S sessionname When creating a new session, this option can be used to specify a meaningful name for the session. -r [pid.tty.host] -r sessionowner/[pid.tty.host] resumes a detached screen session. -p number_or_name|-|=|+ Preselect a window. This is useful when you want to reattach to a specific window or you want to send a command via the "-X" option to a specific window. -X Send the specified command to a running screen session eg stuff.
东西 [string]
Stuff the string string in the input buffer of the current window. This is like the "paste" command but with much less overhead. Without a parameter, screen will prompt for a string to stuff.
像这样启动服务器:
# tmux new-session -d -s ServerFault 'tr az AZ' # replace with your server
tmux将以独立模式启动,所以如果你想看看发生了什么,运行:
# tmux attach-session -t ServerFault
像这样控制服务器:
# tmux send-keys -t ServerFault -l stop # tmux send-keys -t ServerFault Enter # tmux send-keys -t ServerFault -l start # tmux send-keys -t ServerFault Enter # tmux send-keys -t ServerFault Cd # send EOF
参数说明 :
new-session [-AdDP] [-c start-directory] [-F format] [-n window-name] [-s session-name] [-t target-session] [-x width] [-y height] [shell-command] Create a new session with name session-name. The new session is attached to the current terminal unless -d is given. window-name and shell-command are the name of and shell command to execute in the initial window. If -d is used, -x and -y specify the size of the initial window (80 by 24 if not given). send-keys [-lR] [-t target-pane] key ... (alias: send) Send a key or keys to a window. Each argument key is the name of the key (such as `Ca' or `npage' ) to send; if the string is not recognised as a key, it is sent as a series of characters. The -l flag disables key name lookup and sends the keys literally.
尝试这个开始:
# screen # cd /path/to/wd # mkfifo cmd # my_cmd <cmd CA d
而这个杀了:
# cd /path/to/wd # echo "stop" > cmd # rm cmd