Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
289 views
in Technique[技术] by (71.8m points)

linux - Run multiple jobs at once and check which one failed

I am trying to run multiple jobs at once and need to write which one failed to a log file. How to do it? I tried to point the result to a log file but it's not getting populated.

sh test1.sh & test_1_Pid=$!
sh test2.sh & test_2_Pid=$!


wait $test_1_Pid
test_1_Pid=$?
wait $test_2_Pid
test_2_Pid=$?
question from:https://stackoverflow.com/questions/65947425/run-multiple-jobs-at-once-and-check-which-one-failed

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

Start your script in a subshell and let each subprocess handle the errors.

 (sh test1.sh || echo "test1" >> failure.txt)&
 (sh test2.sh || echo "test2" >> failure.txt)&

Or perhaps without a subprocess:

 sh test1.sh || echo "test1" >> failure.txt&
 sh test2.sh || echo "test2" >> failure.txt&

Demo:

touch job1 job2 job4 job5 job7
rm result # when you repeat the demo
for ((j=1; j<8;j++)); do
   (sleep 2; rm job$j 2>/dev/null || echo "job$j failed" >> result)&
done
wait
cat result

The result will show the files that you did not create.

job3 failed
job6 failed

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

1.4m articles

1.4m replys

5 comments

57.0k users

...