Home Backend Development PHP Tutorial Positioning of CI script abnormal exit problem

Positioning of CI script abnormal exit problem

Jun 14, 2018 pm 02:40 PM

下面为大家带来一篇浅谈CI脚本异常退出问题定位。内容挺不错的,现在就分享给大家,也给大家做个参考。

背景

在CI脚本中,使用类似如下脚本进行项目编译的计时,但在执行过程中,有时会出现CI脚本(命名为ci.sh)未完全执行的情况:

#!/bin/bash -e

sleep_time=$1

start_time=`date "+%s"`
# do sth, this sleep would simulate project compilation
sleep $sleep_time
end_time=`date "+%s"`

process_time=`expr \( end_time - start_time \)`
echo "---- process time(sec) are: " $process_time "seconds"

# ...
Copy after login

这个脚本,只是模拟我们在CI中的程序,项目编译前计时,项目编译后再次计时,通过sleep休眠来模拟CI中项目编译锁消耗的时间,然后计算出消耗的时间。这个简化的脚本逻辑很简单,我们通过以下命令来调用:

# ./ci.sh
---- process time(sec) are: 2 seconds  
Copy after login

这样执行好像并不会出错,那实际CI中为什么会出错呢?

分析

首先,我们发现,当出现脚本未完全执行完成时,不会打印“process time(sec) are”这一句,也就是说错误是这句之前引起的。

另外,细心的朋友还会发现,在脚本的首行,我们给bash使用了-e参数,这个参数的作用就是,一旦shell脚本中任何一行出现了错误,shell脚本就停止运行。所谓的出现错误,也就是这行语句的返回值为非零。那么,CI脚本未完全执行的原因,很可能就是因为某一行语句出现了错误,导致脚本直接退出。

通过增加打印“echo $?”来打印上一行语句的执行结果,很快定位到报错的语句在计算处理时间的这一行:

process_time=`expr \( end_time - start_time \)` 
Copy after login

这一行看起来十分普通,只是简单的用终止时间减去开始时间,然后赋值给process_time。为什么会返回非0值呢?

原来,expr命令有一个小小的trick,当expr表达式中的计算结果为0时,expr命令就会返回1,而不是通常的0。在我们实际的CI任务中,一旦某个项目编译时间非常短,在1秒钟内完成,那么起止时间系统,其差值也就为0,因此,expr就会返回非零值,而CI脚本也会因此而退出。

以上就是本文的全部内容,希望对大家的学习有所帮助,更多相关内容请关注PHP中文网!

相关推荐:

CI框架中常用的操作类分析

关于CI框架实现ajax分页和全选,反选,不选以及批量删除的代码

The above is the detailed content of Positioning of CI script abnormal exit problem. For more information, please follow other related articles on the PHP Chinese website!

Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn

Hot AI Tools

Undresser.AI Undress

Undresser.AI Undress

AI-powered app for creating realistic nude photos

AI Clothes Remover

AI Clothes Remover

Online AI tool for removing clothes from photos.

Undress AI Tool

Undress AI Tool

Undress images for free

Clothoff.io

Clothoff.io

AI clothes remover

AI Hentai Generator

AI Hentai Generator

Generate AI Hentai for free.

Hot Article

R.E.P.O. Energy Crystals Explained and What They Do (Yellow Crystal)
4 weeks ago By 尊渡假赌尊渡假赌尊渡假赌
R.E.P.O. Best Graphic Settings
4 weeks ago By 尊渡假赌尊渡假赌尊渡假赌
R.E.P.O. How to Fix Audio if You Can't Hear Anyone
4 weeks ago By 尊渡假赌尊渡假赌尊渡假赌
R.E.P.O. Chat Commands and How to Use Them
4 weeks ago By 尊渡假赌尊渡假赌尊渡假赌

Hot Tools

Notepad++7.3.1

Notepad++7.3.1

Easy-to-use and free code editor

SublimeText3 Chinese version

SublimeText3 Chinese version

Chinese version, very easy to use

Zend Studio 13.0.1

Zend Studio 13.0.1

Powerful PHP integrated development environment

Dreamweaver CS6

Dreamweaver CS6

Visual web development tools

SublimeText3 Mac version

SublimeText3 Mac version

God-level code editing software (SublimeText3)

Explain JSON Web Tokens (JWT) and their use case in PHP APIs. Explain JSON Web Tokens (JWT) and their use case in PHP APIs. Apr 05, 2025 am 12:04 AM

JWT is an open standard based on JSON, used to securely transmit information between parties, mainly for identity authentication and information exchange. 1. JWT consists of three parts: Header, Payload and Signature. 2. The working principle of JWT includes three steps: generating JWT, verifying JWT and parsing Payload. 3. When using JWT for authentication in PHP, JWT can be generated and verified, and user role and permission information can be included in advanced usage. 4. Common errors include signature verification failure, token expiration, and payload oversized. Debugging skills include using debugging tools and logging. 5. Performance optimization and best practices include using appropriate signature algorithms, setting validity periods reasonably,

Explain the concept of late static binding in PHP. Explain the concept of late static binding in PHP. Mar 21, 2025 pm 01:33 PM

Article discusses late static binding (LSB) in PHP, introduced in PHP 5.3, allowing runtime resolution of static method calls for more flexible inheritance.Main issue: LSB vs. traditional polymorphism; LSB's practical applications and potential perfo

Framework Security Features: Protecting against vulnerabilities. Framework Security Features: Protecting against vulnerabilities. Mar 28, 2025 pm 05:11 PM

Article discusses essential security features in frameworks to protect against vulnerabilities, including input validation, authentication, and regular updates.

How to send a POST request containing JSON data using PHP's cURL library? How to send a POST request containing JSON data using PHP's cURL library? Apr 01, 2025 pm 03:12 PM

Sending JSON data using PHP's cURL library In PHP development, it is often necessary to interact with external APIs. One of the common ways is to use cURL library to send POST�...

Customizing/Extending Frameworks: How to add custom functionality. Customizing/Extending Frameworks: How to add custom functionality. Mar 28, 2025 pm 05:12 PM

The article discusses adding custom functionality to frameworks, focusing on understanding architecture, identifying extension points, and best practices for integration and debugging.

Describe the SOLID principles and how they apply to PHP development. Describe the SOLID principles and how they apply to PHP development. Apr 03, 2025 am 12:04 AM

The application of SOLID principle in PHP development includes: 1. Single responsibility principle (SRP): Each class is responsible for only one function. 2. Open and close principle (OCP): Changes are achieved through extension rather than modification. 3. Lisch's Substitution Principle (LSP): Subclasses can replace base classes without affecting program accuracy. 4. Interface isolation principle (ISP): Use fine-grained interfaces to avoid dependencies and unused methods. 5. Dependency inversion principle (DIP): High and low-level modules rely on abstraction and are implemented through dependency injection.

How does session hijacking work and how can you mitigate it in PHP? How does session hijacking work and how can you mitigate it in PHP? Apr 06, 2025 am 12:02 AM

Session hijacking can be achieved through the following steps: 1. Obtain the session ID, 2. Use the session ID, 3. Keep the session active. The methods to prevent session hijacking in PHP include: 1. Use the session_regenerate_id() function to regenerate the session ID, 2. Store session data through the database, 3. Ensure that all session data is transmitted through HTTPS.

See all articles