简体   繁体   English

当进程通过脚本运行时,jobs 命令结果为空

[英]jobs command result is empty when process is run through script

I need to run rsync in background through shell script but once it has started, I need to monitor the status of that jobs through shell.我需要通过 shell 脚本在后台运行 rsync,但是一旦它启动,我需要通过 shell 监视这些作业的状态。

jobs command return empty when its run in shell after the script exits.脚本退出后, jobs命令在 shell 中运行时返回空。 ps -ef | grep rsync ps -ef | grep rsync shows that the rsync is still running. ps -ef | grep rsync显示 rsync 仍在运行。

I can check the status through script but I need to run the script multiple times so it uses a different ip.txt file to push.我可以通过脚本检查状态,但我需要多次运行脚本,因此它使用不同的ip.txt文件进行推送。 So I can't have the script running to check jobs status.所以我不能运行脚本来检查jobs状态。

Here is the script:这是脚本:

for i in `cat $ip.txt`; do
     rsync -avzh $directory/ user@"$i":/cygdrive/c/test/$directory 2>&1 > /dev/null &
done
jobs; #shows the jobs status while in the shell script.
exit 1

Output of jobs command is empty after the shell script exits: shell 脚本退出后, jobs命令的输出为空:

root@host001:~# jobs
root@host001:~# 

What could be the reason and how could I get the status of jobs while the rsync is running in background?可能是什么原因以及如何在rsync在后台运行时获取jobs状态? I can't find an article online related to this.我在网上找不到与此相关的文章。

Since your shell (the one from which you execute jobs ) did not start rsync , it doesn't know anything about it.由于您的外壳程序(您从中执行jobs的外壳程序)没有启动rsync ,因此它对此一无所知。 There are different approaches to fixing that, but it boils down to starting the background process from your shell.有不同的方法来解决这个问题,但归结为从你的 shell 启动后台进程。 For example, you can start the script you have using the source BASH command instead of executing it in a separate process.例如,您可以使用source BASH 命令启动您拥有的脚本,而不是在单独的进程中执行它。 Of course, you'd have to remove the exit 1 at the end, because that exits your shell otherwise.当然,您必须在最后删除exit 1 ,否则会退出您的 shell。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM