如何检查一个命令是否在bash脚本中挂起?

时间:2021-09-24 00:07:43

I have a bash script, in which I call another script and sometimes second script hangs. Is there any way to check if it is hung or not. And I can't make any changes is the second script.

我有一个bash脚本,我在其中调用另一个脚本,有时第二个脚本挂起。有没有办法检查它是否挂掉了?我不能做任何改变,这是第二个脚本。

#!/bin/bash
calling second script(thata might hang)

if hang then do something

1 个解决方案

#1


2  

If you already know a threshold time, that after that script is considered hung. you can use timeout.

如果您已经知道了阈值时间,那么在该脚本之后将被视为挂起。您可以使用超时。

timeout 30 bash script.sh

command bash script.sh will run until it finish in less that 30 seconds or gets killed by timeout. You can adjust the time according to your environment.

命令bash脚本。sh会在30秒内跑完,或者被超时杀死。你可以根据你的环境调整时间。

Command Reference:

命令参考:

timeout
Usage: timeout [OPTION] DURATION COMMAND [ARG]...
  or:  timeout [OPTION]
Start COMMAND, and kill it if still running after DURATION.

Please refer to respective man page for options.

有关选项请参阅各自的手册页。

#1


2  

If you already know a threshold time, that after that script is considered hung. you can use timeout.

如果您已经知道了阈值时间,那么在该脚本之后将被视为挂起。您可以使用超时。

timeout 30 bash script.sh

command bash script.sh will run until it finish in less that 30 seconds or gets killed by timeout. You can adjust the time according to your environment.

命令bash脚本。sh会在30秒内跑完,或者被超时杀死。你可以根据你的环境调整时间。

Command Reference:

命令参考:

timeout
Usage: timeout [OPTION] DURATION COMMAND [ARG]...
  or:  timeout [OPTION]
Start COMMAND, and kill it if still running after DURATION.

Please refer to respective man page for options.

有关选项请参阅各自的手册页。