On the localization of the CI script exception exit problem

Source: Internet
Author: User
Here is a brief talk about the localization of CI script abnormal exit problem. Content is very good, now share to everyone, but also for everyone to make a reference.

Background

In a CI script, the timing of Project compilation is done using a script similar to the following, but during execution, there are cases where the CI script (named ci.sh) is not fully executed:

#!/bin/bash-esleep_time=$1start_time= ' Date ' +%s ' # do sth, this sleep would simulate project Compilationsleep $sleep _tim Eend_time= ' Date ' +%s ' process_time= ' expr \ (end_time-start_time \) ' echo '----process time (SEC) is: "$process _time" se Conds "# ...

This script, just simulates our program in CI, timed the project before compiling, timed it again after compiling the project, and simulated the time consumed by the project compile lock in CI through sleep hibernation, and then calculated the elapsed time. This simplified script logic is simple, and we invoke it with the following command:

#./ci.sh----Process Time (sec) Are:2 seconds

This does not seem to be an error, so why is there an error in the actual CI?

Analysis

First, we found that the phrase "process time (SEC) is" is not printed when the script does not complete completely, meaning that the error was caused before this sentence.

In addition, the attentive friend will find that in the first line of the script, we have used the-e parameter for bash, which is the role of the shell script will stop running once any line in the shell script has an error. The so-called error, that is, the return value of this line statement is nonzero. Then, the reason why the CI script is not fully executed is probably because a single line of statements has an error that causes the script to exit directly.

By increasing the print "echo $?" To print the results of the previous line of statements, and quickly navigate to the line where the error statement was calculated for the processing time:

Process_time= ' expr \ (end_time-start_time \) '

This line looks very common, just simply subtracting the start time by terminating time and assigning it to process_time. Why does it return a value other than 0?

It turns out that the expr command has a small trick, and the expr command returns 1 instead of the usual 0 when the result of the expr expression evaluates to 0 o'clock. In our actual CI task, once the compilation time of a project is very short and completed within 1 seconds, the difference between the start and end time system is 0, so expr returns a non-0 value, and the CI script exits.

The above is the whole content of this article, I hope that everyone's learning has helped, more relevant content please pay attention to topic.alibabacloud.com!

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.