Thi*_*Not 15
您可以在后台运行这样的作业:
command &
Run Code Online (Sandbox Code Playgroud)
这允许您连续启动多个作业,而无需等待前一个作业完成.
如果您启动这样的多个后台作业,它们将共享相同的stdout(和stderr),这意味着它们的输出可能会交错.例如,采用以下脚本:
#!/bin/bash
# countup.sh
for i in `seq 3`; do
echo $i
sleep 1
done
Run Code Online (Sandbox Code Playgroud)
在后台启动它两次:
./countup.sh &
./countup.sh &
Run Code Online (Sandbox Code Playgroud)
您在终端中看到的内容将如下所示:
1
1
2
2
3
3
Run Code Online (Sandbox Code Playgroud)
但也可能看起来像这样:
1
2
1
3
2
3
Run Code Online (Sandbox Code Playgroud)
您可能不希望这样,因为很难确定哪个输出属于哪个作业.解决方案?将每个作业重定向stdout(和可选stderr)到单独的文件.例如
command > file &
Run Code Online (Sandbox Code Playgroud)
将仅重定向stdout和
command > file 2>&1 &
Run Code Online (Sandbox Code Playgroud)
将重定向既stdout和stderr对command向file而运行command在后台.这个页面很好地介绍了Bash中的重定向.您可以tail通过以下文件查看命令的输出"live" :
tail -f file
Run Code Online (Sandbox Code Playgroud)
我建议使用nohup 或screen作为user2676075 运行后台作业,以便在关闭终端会话后让您的作业继续运行,例如
nohup command1 > file1 2>&1 &
nohup command2 > file2 2>&1 &
nohup command3 > file3 2>&1 &
Run Code Online (Sandbox Code Playgroud)