简体   繁体   English

我们如何防止 CTRL-C 终止屏幕?

[英]How can we prevent CTRL-C from screen terminating?

I'm currently writing a bash script that would create multiple shell instances (with screen command) and execute a subprogram .我目前正在编写一个bash脚本,它将创建多个 shell 实例(使用screen命令)并执行一个subprogram The problem is when I try to interrupt the subprogram, it interrupts the screen instance too.问题是当我尝试中断子程序时,它也会中断屏幕实例。 I already searched for trap command on internet with SIGINT but I don't really know how to use it in this case.我已经使用SIGINT在互联网上搜索了陷阱命令,但我真的不知道在这种情况下如何使用它。

Here is my code to show you how do I create the screen:这是我的代码,向您展示如何创建屏幕:

#!/bin/bash
#ALL PATHS ARE DECLARED HERE.
declare -A PATHS; declare -a orders;
PATHS["proxy"]=/home/luna/proxy/HydraProxy; orders+=( "proxy" )
PATHS["bot"]=/home/luna/bot; orders+=( "bot" )

#LAUNCH SERVERS
SERVERS=/home/luna/servers
cd "$SERVERS"
for dir in */; do
    d=$(basename "$dir")
    PATHS["$d"]="$(realpath $dir)"; orders+=( "$d" )
done

for name in "${orders[@]}"; do
    if ! screen -list | grep -q "$name"; then
        path="${PATHS[$name]}"
        cd "$path"
        screen -dmS "$name" ./start.sh
        echo "$name CREATED AT $path"
        sleep 2
    else
        echo "SCREEN $name IS ALREADY CREATED"
    fi
done

Could you help me more to find a solution please?你能帮我更多地找到解决方案吗? Thank you very much for your time.非常感谢您的宝贵时间。

Each of your screen instances is created to run a single command, start.sh .创建每个屏幕实例以运行单个命令start.sh When this command terminates, for instance when you interrupt it, the screen will have done its job and terminate.当此命令终止时,例如当您中断它时,屏幕将完成其工作并终止。 The reason for this is that screen runs shell scripts directly in a non-interactive shell, rather than spawning a new interactive shell and running it there.原因是屏幕直接在非交互式 shell 中运行 shell 脚本,而不是生成新的交互式 shell 并在那里运行它。 If you wanted to run start.sh inside an interactive shell in each screen, you'd do something like this:如果您想在每个屏幕的交互式 shell 中运行start.sh ,您可以执行以下操作:

screen -dmS "$name" /bin/bash -i
screen -S "$name" -X stuff "./start.sh^M"

The ^M is needed as it simulates pressing enter in your shell within screen. ^M是必需的,因为它模拟了屏幕内 shell 中的按 Enter 键。

If you use this, then when you interrupt a script within screen, you will still be left with an interactive prompt afterward to deal with as you see fit.如果你使用它,那么当你在屏幕中中断一个脚本时,你仍然会留下一个交互式提示来处理你认为合适的事情。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM