Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/linux/27.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/5/bash/16.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Linux 如果为true,则在新屏幕中运行脚本_Linux_Bash_Shell_Gnu Screen - Fatal编程技术网

Linux 如果为true,则在新屏幕中运行脚本

Linux 如果为true,则在新屏幕中运行脚本,linux,bash,shell,gnu-screen,Linux,Bash,Shell,Gnu Screen,我有一个脚本,它将检查background\u logging是否为true,如果是,那么我希望脚本的其余部分在新分离的屏幕中运行 我试过使用这个:exec屏幕-dmS“alt logging”/bin/bash“$0”。这有时会创建屏幕等,但其他时候什么也不会发生。当它创建一个屏幕时,它不会运行脚本文件的其余部分,当我尝试恢复屏幕时,它会说它是(死了??) 这是整个脚本,我添加了一些注释,以更好地解释我想做什么: #!/bin/bash # Configuration files confi

我有一个脚本,它将检查
background\u logging
是否为
true
,如果是,那么我希望脚本的其余部分在新分离的屏幕中运行

我试过使用这个:
exec屏幕-dmS“alt logging”/bin/bash“$0”。这有时会创建屏幕等,但其他时候什么也不会发生。当它创建一个屏幕时,它不会运行脚本文件的其余部分,当我尝试恢复屏幕时,它会说它是
(死了??)

这是整个脚本,我添加了一些注释,以更好地解释我想做什么:

#!/bin/bash

# Configuration files
config='config.cfg'
source "$config"

# If this is true, run the rest of the script in a new screen.
# $background_logging comes from the configuration file declared above (config).
if [ $background_logging == "true" ]; then
    exec screen -dmS "alt-logging" /bin/bash "$0";
fi

[ $# -eq 0 ] && { echo -e "\nERROR: You must specify an alt file!"; exit 1; }

# Logging script
y=0
while IFS='' read -r line || [[ -n "$line" ]]; do
    cmd="screen -dmS alt$y bash -c 'exec $line;'"
    eval $cmd
    sleep $logging_speed
    y=$(( $y + 1 ))
done < "$1"

此脚本的目的是循环遍历文本文件中的每一行,并将该行作为命令执行。当
$background\u logging
false
时,它工作得非常好,因此
while
循环不会出现问题。

如前所述,这不完全可能。具体来说,脚本中发生了什么:当您将正在运行的脚本代码替换为screen的代码时

不过,您可以做的是启动screen,了解它的一些细节,并将控制台脚本重定向到它/输出到它,但您将无法将正在运行的脚本重新分配到screen进程,就像从那里开始一样。例如:

#!/bin/bash

# Use a temp file to pass cat's parent pid out of screen.
tempfile=$(tempfile)
screen -dmS 'alt-logging' /bin/bash -c "echo \$\$ > \"${tempfile}\" && /bin/cat"

# Wait to receive that information on the outside (it may not be available
# immediately).
while [[ -z "${child_cat_pid}" ]] ; do
        child_cat_pid=$(cat "${tempfile}")
done

# point stdin/out/err of the current shell (rest of the script) to that cat
# child process

exec 0< /proc/${child_cat_pid}/fd/0
exec 1> /proc/${child_cat_pid}/fd/1
exec 2> /proc/${child_cat_pid}/fd/2

# Rest of the script
i=0
while true ; do
    echo $((i++))
    sleep 1
done
#/bin/bash
#使用临时文件将cat的父pid传递到屏幕外。
tempfile=$(tempfile)
屏幕-dmS'alt logging'/bin/bash-c“echo\$\$\$>\”${tempfile}\“&&&/bin/cat”
#等待从外部接收该信息(该信息可能不可用
#立即)。
而[[-z“${child_cat_pid}]”;做
子类\u cat\u pid=$(类“${tempfile}”)
完成
#将当前shell(脚本的其余部分)的stdin/out/err指向该cat
#子进程
执行0/proc/${child\u cat\u pid}/fd/1
exec 2>/proc/${child\u cat\u pid}/fd/2
#脚本的其余部分
i=0
虽然真实;做
echo$((i++)
睡眠1
完成
远非完美,相当混乱。使用第三方工具可能会有所帮助,比如从屏幕内部抓取脚本的控制台。但是更干净/更简单的方法是(在需要时)在屏幕会话建立后启动应该在该屏幕会话中执行的代码


也就是说。实际上,我建议退一步问问,你们到底想实现什么,为什么你们想在屏幕上运行你们的脚本。您是否计划附加/分离到它?因为如果您所追求的是使用独立的控制台运行长期进程,那么这可能是一种更简单的方法。

我找到了一种更简单的方法来实现这一点,可能效率不高,但它确实可以完成任务。我所做的只是检查
$STY
变量是否已使用
if[-z“$STY”]设置
,如果这是
false
,那么我将只执行以下命令:
eval“screen-dmS alt logging bash-c'exec./alt logging.sh;”
。由于这不是一个特别密集的脚本,我认为这可能是最好的前进方向。但不管怎样,我真的很感谢你的帮助。将来很可能会使用您的方法。
#!/bin/bash

# Use a temp file to pass cat's parent pid out of screen.
tempfile=$(tempfile)
screen -dmS 'alt-logging' /bin/bash -c "echo \$\$ > \"${tempfile}\" && /bin/cat"

# Wait to receive that information on the outside (it may not be available
# immediately).
while [[ -z "${child_cat_pid}" ]] ; do
        child_cat_pid=$(cat "${tempfile}")
done

# point stdin/out/err of the current shell (rest of the script) to that cat
# child process

exec 0< /proc/${child_cat_pid}/fd/0
exec 1> /proc/${child_cat_pid}/fd/1
exec 2> /proc/${child_cat_pid}/fd/2

# Rest of the script
i=0
while true ; do
    echo $((i++))
    sleep 1
done