Shell:如何使用屏幕并在Shell脚本中等待几个后台进程 [英] Shell: How to use screen and wait for a couple of background processes in a shell script

查看:450
本文介绍了Shell:如何使用屏幕并在Shell脚本中等待几个后台进程的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在为几个长时间运行的进程编写一个shell脚本. 首先,我需要在屏幕会话管理器中运行所有命令,以便在用户断开连接的情况下进程的执行不会结束. 稍后,我需要等待一些之前创建的后台进程结束,以便可以启动以下进程.

I am writing a shell script for a couple of long running processes. First of all I need to run all commands in the screen session manager, so that the execution of a process does not end if a user has been disconnected. Later I need wait for some background processes, which has been created before, to end, so that the following process can start.

我的问题是如何在shell脚本中启动屏幕会话并等待后台进程结束.

My question is how to start a screen session in a shell script and wait for the background processes to end.

推荐答案

您不能在运行的进程上调用screen(或nohup),必须执行screen script.但是,您可以执行nohup的操作,捕获SIGHUP并将输出重定向到文件.

You can't invoke screen (or nohup) on the running process, you have to do screen script. You could however do what nohup does, trap SIGHUP and redirect output to a file.

exec > OUT 2>&1
trap '' 1

要等待后台进程,请在创建pid时保存它,然后调用wait

To wait for background processes, save the pid when you create it and then call wait

foo&
PID1=$!
bar&
PID2=$1
wait $PID1 $PID2

或者只是等待所有事情完成.

Or alternatively just wait for everything to finish.

foo&
bar&
wait

这篇关于Shell:如何使用屏幕并在Shell脚本中等待几个后台进程的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆