【问题标题】:Running the shell scripts in Background在后台运行 shell 脚本
【发布时间】:2010-03-09 15:31:37
【问题描述】:

我会为用户提供在后台运行 shell 脚本的功能。

我的 shell 程序实例化了许多其他的 shell 脚本。

这是我脚本的一个小代码sn-p

./main.sh  # Main script

in main.sh 

I call preprocessing.sh
create_dir.sh
handle_file.sh
post_processing.sh
report_generation.sh

我想知道我是否也必须启动所有子脚本。如果我必须在后台启动所有脚本并最后通过在该测试运行中显示消息来通知用户,那么语法是什么完成。

谢谢

基兰

【问题讨论】:

  • 你到底想通过后台实现什么?即使您的连接丢失/终端窗口关闭,您的脚本仍会继续运行吗?当脚本在后台运行并在状态结束时收到通知时,您可以继续工作吗?但是,如果您在输入命令时状态 echo 回来并在您输入的中间显示一个消息字符串怎么办?
  • 是的,我想在后台运行脚本并继续在同一个 shell 中运行其他命令。执行完成后,我可以通过简单的回显消息通知用户它已完成,您可以检查日志文件。我的脚本运行了将近 2 个小时,所以我想在后台运行它。谢谢基兰

标签: linux unix shell scripting


【解决方案1】:

使用& 在后台启动您的进程,然后使用bash 的内置wait 命令:

  wait [n ...] 
          Wait for each specified process and return its termination  sta‐ 
          tus.   Each  n  may be a process ID or a job specification; if a 
          job spec is given, all processes  in  that  job’s  pipeline  are 
          waited  for.  

here 提供了几个示例。例如:

# wait on 2 processes
sleep 10 &
sleep 10 &
wait %1 %2 && echo "Completed!"

【讨论】:

  • 你确定这就是你要找的吗??根据您的脚本名称,似乎子脚本必须按顺序运行,而不是全部并行运行(这是您接受的解决方案所建议的。)
【解决方案2】:

在命令末尾添加“&”

I call preprocessing.sh &
create_dir.sh &
...

【讨论】:

  • 感谢您的快速响应,但是如何在测试运行结束时通过简单的回显消息通知用户.. ??
猜你喜欢
  • 2011-08-27
  • 1970-01-01
  • 1970-01-01
  • 2021-08-11
  • 1970-01-01
  • 2014-04-09
  • 2021-10-26
  • 1970-01-01
相关资源
最近更新 更多