如何在Unix Shell脚本中进行并行处理?

发布于 2024-09-01 14:34:32 字数 754 浏览 0 评论 0原文

我有一个 shell 脚本,用于将 build.xml 文件传输到远程 unix 计算机 (devrsp02) 并在该计算机上执行 ANT 任务 wldeploydevrsp02)。现在,此 wldeploy 任务大约需要 15 分钟才能完成,并且在运行时,unix 控制台的最后一行是 -

“任务{某些数字}已初始化”。

该任务完成后,我们会收到“任务已完成”消息,并且脚本中的下一个任务仅在此之后执行。

但有时,weblogic 域可能存在问题,并且部署可能在内部失败,但不会影响 wldeploy 任务的状态。 unix 控制台仍将停留在“任务{某些数字}已初始化”处。部署的错误将记录在名为 output.a 的文件中。

所以,我现在想要的是 -

在运行 wldeploy 之前启动一个时间计数器。如果 wldeploy 运行超过 15 分钟,则应运行以下命令 -

tail -f output.a ## 不终止 wldeploy

cat output.a ## 强制终止 wldeploy 后

这里需要注意的是 - 我无法在后台运行 wldeploy 任务,因为在这种情况下,用户将无法知道任务何时完成,这一点至关重要对于这个脚本。

您能提出什么建议来实现这一目标吗?

I have a shell script that transfers a build.xml file to a remote unix machine (devrsp02) and executes the ANT task wldeploy on that machine (devrsp02). Now, this wldeploy task takes around 15 minutes to complete and while this is running, the last line at the unix console is -

"task {some digit} initialized".

Once this task is complete, we get a "task Completed" msg and the next task in the script is executed only after that.

But sometimes, there might be a problem with the weblogic domain and the deployment might be failing internally, with no effect on the status of the wldeploy task. The unix console will still be stuck at "task {some digit} initialized". The error of the deployment will be getting logged in a file called output.a

So, what I want now is -

Start a time counter before running wldeploy. If the wldeploy runs for more than 15 minutes, the following command should be run -

tail -f output.a ## without terminating the wldeploy

or

cat output.a ## after terminating the wldeploy forcefully

Point to be noted here is - I can't run the wldeploy task in background, as in that case the user won't get to know when the task is complete, which is crucial for this script.

Could you please suggest anything to achieve this?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

超可爱的懒熊 2024-09-08 14:34:32

创建此脚本(例如 deploy.sh):

#!/bin/sh
sleep 900 && pkill -n wldeploy && cat output.a &
wldeploy 

然后从控制台

chmod +x deploy.sh

然后运行

./deploy.sh

​​该脚本将启动一个计数器(15 分钟),如果 wldeploy 进程被强制终止,该计数器将强制终止该进程。正在运行,如果该进程正在运行,您将看到 output.a 的内容。

如果脚本已终止,则 pkill 将不会返回 true,并且 output.a 将不会显示。

我会将此任务称为监视而不是“并行处理”:)

Create this script (deploy.sh for example):

#!/bin/sh
sleep 900 && pkill -n wldeploy && cat output.a &
wldeploy 

Then from the console

chmod +x deploy.sh

Then run

./deploy.sh

This script will start a counter (15 minutes) that will forcibly kill the wldeploy process if it's running, and if the process was running you'll see the contents of output.a.

If the script has terminated then pkill will not return true and output.a will not be shown.

I would call this task monitoring rather than "parallel processing" :)

↘人皮目录ツ 2024-09-08 14:34:32

这只会终止它启动的 wldeploy 进程,告诉您 wldeploy 是否返回成功或失败,并在 wldeploy 完成后运行不超过 30 秒。

它应该是 sh 兼容的,但是我现在可以访问的 /bin/sh 似乎有一个损坏的等待命令。

#!/bin/ksh 
wldeploy &
while [ ${slept:-0} -le 900 ]; do
    sleep 30 && slept=`expr ${slept:-0} + 30`
    if [ $ = "`ps -o ppid= -p $!`" ]; then
        echo wldeploy still running
    else
        wait $! && echo "wldeploy succeeded" || echo "wldeploy failed"
        break
    fi
done
if [ $ = "`ps -o ppid= -p $!`" ]; then
   echo "wldeploy did not finish in $slept seconds, killing it"
   kill $!
   cat output.a
fi

This will only kill the wldeploy process it started, tell you whether wldeploy returned success or failure, and run no more than 30 seconds after wldeploy finishes.

It should be sh-compatible, but the /bin/sh I've got access to now seems to have a broken wait command.

#!/bin/ksh 
wldeploy &
while [ ${slept:-0} -le 900 ]; do
    sleep 30 && slept=`expr ${slept:-0} + 30`
    if [ $ = "`ps -o ppid= -p $!`" ]; then
        echo wldeploy still running
    else
        wait $! && echo "wldeploy succeeded" || echo "wldeploy failed"
        break
    fi
done
if [ $ = "`ps -o ppid= -p $!`" ]; then
   echo "wldeploy did not finish in $slept seconds, killing it"
   kill $!
   cat output.a
fi
顾北清歌寒 2024-09-08 14:34:32

对于不终止 wldeploy 的部分很简单,只需在之前执行

  { sleep 900; tail -f output.a; } &

对于终止 wldeploy 的部分,它更复杂,因为您已经确定了 wldeploy 进程的 PID。 pra的答案正是这样做的,所以我就参考一下。

For the part without terminating the wldeploy it is easy, just execute before

  { sleep 900; tail -f output.a; } &

For the part with kill it, it is more complex, as you have determine the PID of the wldeploy process. The answer of pra is exactly doing that, so I would just refer to that.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文