Bash脚本运行顺序启动背景进程的分离循环
问题描述:
我试图在通过ssh连接到的远程Linux服务器上运行一系列测试。Bash脚本运行顺序启动背景进程的分离循环
- 我不希望一直留在运行期间登录SSH会话- > nohup的
- 我不希望有一个是否运行做继续检查(?) - > for循环
- 因为许可问题,我只能一次运行一个测试过程(?) - >连续
- 我要继续工作,而测试组正在处理- >背景
这里是我的尝试:
#!/usr/bin/env bash
# Assembling a list of commands to be executed sequentially
TESTRUNS="";
for i in `ls ../testSet/*`;
do
MSG="running test problem ${i##*/}";
RUN="mySequentialCommand $i > results/${i##*/} 2> /dev/null;";
TESTRUNS=$TESTRUNS"echo $MSG; $RUN";
done
#run commands with nohup to be able to log out of ssh session
nohup eval $TESTRUNS &
但它看起来像nohup的不EVAL状况不佳。 有什么想法?
答
你可以看看screen
,这是nohup的一个替代方案,带有附加功能。我将用while [ 1 ]; do printf "."; sleep 5; done
替换您的测试脚本以测试screen
解决方案。
命令screen -ls
是可选的,只是显示正在发生的事情。
prompt> screen -ls
No Sockets found in /var/run/uscreens/S-notroot.
prompt> screen
prompt> screen -ls
prompt> while [ 1 ]; do printf "."; sleep 5; done
# You don't get a prompt. Use "CTRL-a d" to detach from your current screen
prompt> screen -ls
# do some work
# connect to screen with batch running
prompt> screen -r
# Press ^C to terminate the batch (script printing dots)
prompt> screen -ls
prompt> exit
prompt> screen -ls
Google for screenrc查看如何自定义界面。
你可以改变你的脚本到像
#!/usr/bin/env bash
# Assembling a list of commands to be executed sequentially
for i in ../testSet/*; do
do
echo "Running test problem ${i##*/}"
mySequentialCommand $i > results/${i##*/} 2> /dev/null
done
上面的脚本可以nohup scriptname &
开始,当你不使用screen
或简单scriptname
里面的画面。
答
nohup
是需要的,如果你想你的脚本运行,甚至在壳后关闭。所以是的。
和&
在RUN
中没有必要,因为您执行的命令为&。
现在,您的脚本在for循环中构建命令,但不执行它。这意味着你只有最后一个文件在运行。如果要运行所有文件,则需要执行nohup
命令作为循环的一部分。但是 - 你不能用&
运行命令,因为它会在后台运行命令并返回到脚本,该脚本将执行循环中的下一个项目。最终这将并行运行所有文件。
在for循环中移动nohup eval $TESTRUNS
,但是不能用&
运行它。你需要做的是run the script itself with nohup
,并且脚本将在后台循环遍历所有文件,即使在shell关闭后也是如此。
不要分析'ls'的输出,只需在'../ testSet/*'中替'输入',那么你不必担心它们中包含特殊字符的文件名,直到你尝试稍后执行。 。 。 –