如何检查程序在bash脚本中运行成功?

时间:2021-09-30 11:29:27

I have to run some programs in bash script.Sometimes program may not give result.

我必须在bash脚本中运行一些程序。有时程序可能不会给出结果。

#!/bin/sh
for k in $( seq 1 20)
do
echo -n ${k}' ' >> 2.txt
./s${k}>>2.txt & sleep 5
done

My ideal result like this:

我的理想结果如下:

1 result001
2 result002
3 
4 result004

But after run my code,the result is

但运行我的代码后,结果是

1 result001
2 result002
3 4 result004

So my problem is how to check a program run successful in bash script?Thank you!

所以我的问题是如何检查程序在bash脚本中运行成功?谢谢!

3 个解决方案

#1


This should work for you.

这应该适合你。

 #!/bin/bash


    for k in {1..20}
    do
       result=$(./s${k} & sleep 5)
       printf "%b %b\n" $k $result >> 2.txt
    done

The following output was given when testing this script:

测试此脚本时给出以下输出:

1 result1
2 result2
3 
4 result4

#2


Try to use wait

尝试使用等待

http://en.wikipedia.org/wiki/Wait_%28command%29

wait [n]

where n is the pid or job ID of a currently executing background process (job). If n is not given, the command waits until all jobs known to the invoking shell have terminated.

其中n是当前正在执行的后台进程(作业)的pid或作业ID。如果未给出n,则命令将等待,直到调用shell已知的所有作业都已终止。

wait normally returns the exit status of the last job which terminated. It may also return 127 in the event that n specifies a non-existent job or zero if there were no jobs to wait for.

wait通常会返回上次终止的最后一个作业的退出状态。如果n指定不存在的作业,它也可能返回127,如果没有等待的作业,则返回0。

Because wait needs to be aware of the job table of the current shell execution environment, it is usually implemented as a shell builtin.

因为wait需要知道当前shell执行环境的作业表,所以它通常被实现为shell内置。

Just add wait after this line

只需在此行之后添加等待

./s${k}>>2.txt & sleep 5
wait

In case the process can deadlock - another option is timeout

如果进程可以死锁 - 另一个选项是超时

timeout -s 9 5 ./s${k}>>2.txt # Waits 5 seconds and then kill the process

#3


You could call the jobs sequentially and use timeout to limit execution time:

您可以按顺序调用作业并使用超时来限制执行时间:

#!/bin/sh
for k in $( seq 1 20)
do
  echo -n ${k}' ' >> 2.txt
  timeout -k 8 5 ./s${k}>>2.txt
done

This will terminate each job with the TERM signal (allowing cleanup via signal handlers to run) after 5 seconds and, if unsuccessful, with the KILL signal after 8 seconds.

这将在5秒后使用TERM信号终止每个作业(允许通过信号处理程序进行清理),如果不成功,则在8秒后使用KILL信号终止每个作业。

However from your shell's perspective, no parallel processes are involved and you don't need to solve any concurrency problems.

但是从shell的角度来看,不涉及并行进程,也不需要解决任何并发问题。

#1


This should work for you.

这应该适合你。

 #!/bin/bash


    for k in {1..20}
    do
       result=$(./s${k} & sleep 5)
       printf "%b %b\n" $k $result >> 2.txt
    done

The following output was given when testing this script:

测试此脚本时给出以下输出:

1 result1
2 result2
3 
4 result4

#2


Try to use wait

尝试使用等待

http://en.wikipedia.org/wiki/Wait_%28command%29

wait [n]

where n is the pid or job ID of a currently executing background process (job). If n is not given, the command waits until all jobs known to the invoking shell have terminated.

其中n是当前正在执行的后台进程(作业)的pid或作业ID。如果未给出n,则命令将等待,直到调用shell已知的所有作业都已终止。

wait normally returns the exit status of the last job which terminated. It may also return 127 in the event that n specifies a non-existent job or zero if there were no jobs to wait for.

wait通常会返回上次终止的最后一个作业的退出状态。如果n指定不存在的作业,它也可能返回127,如果没有等待的作业,则返回0。

Because wait needs to be aware of the job table of the current shell execution environment, it is usually implemented as a shell builtin.

因为wait需要知道当前shell执行环境的作业表,所以它通常被实现为shell内置。

Just add wait after this line

只需在此行之后添加等待

./s${k}>>2.txt & sleep 5
wait

In case the process can deadlock - another option is timeout

如果进程可以死锁 - 另一个选项是超时

timeout -s 9 5 ./s${k}>>2.txt # Waits 5 seconds and then kill the process

#3


You could call the jobs sequentially and use timeout to limit execution time:

您可以按顺序调用作业并使用超时来限制执行时间:

#!/bin/sh
for k in $( seq 1 20)
do
  echo -n ${k}' ' >> 2.txt
  timeout -k 8 5 ./s${k}>>2.txt
done

This will terminate each job with the TERM signal (allowing cleanup via signal handlers to run) after 5 seconds and, if unsuccessful, with the KILL signal after 8 seconds.

这将在5秒后使用TERM信号终止每个作业(允许通过信号处理程序进行清理),如果不成功,则在8秒后使用KILL信号终止每个作业。

However from your shell's perspective, no parallel processes are involved and you don't need to solve any concurrency problems.

但是从shell的角度来看,不涉及并行进程,也不需要解决任何并发问题。