search
HomeBackend DevelopmentPHP TutorialInstead of bash, write PHP daemon (background program)_PHP tutorial

Bkjia (www.Bkjia.com) Tutorial Recently, a friend at Yahoo told me that I used to use bash csh to write scripts, but now I use PHP to write background programs. I feel that PHP’s functions are the most Complete and easy to use shell_exec to easily call the system kernel.

I exchanged some principles with him about writing background programs, which are summarized as follows.

以下为引用的内容:
function connnect()
{
global $db;
if (is_resource($db)) {
mysqli_close($db);
}
$db = mysqli_connect("122.225.96.142", 'waihui', 'freebsd@fzm', 'waihui');
}

First of all, we need to get rid of some of the previous thinking habits of writing web scripts. After the web script is run once, the memory is released immediately. The daemon program is different. It will run for a year or even several years.

1. As a good habit, you must run the code in an infinite loop like while (1) {}. In this way, the script will not stop as long as the code does not fail.

 2. Echo cannot be used, but use log instead. Use logging instead of echo. Because echo outputs a character to the screen, if there is no output object, a fatal error will be reported.

 3. If it is MYSQL, you need to reconnect to MYSQL every time.

The above is an example of connection. This code was once severely criticized by an expert. This is mainly to prevent the following things:

Mysql has restarted. The $db variable is definitely still a resource, but this resource is no longer valid. It will happen if you execute the code again:

 mysql has go away such an error. This will be output to the screen, even if error reporting is turned off. This leads to the entire

The script execution error is the same as the error generated by echo.

Although it is a waste to connect to the database every time, we can only kill 1000 by mistake rather than let one go. This mistake is made by most friends

Error, many people told me that this is a mysql bug, because originally writing the file was fine, but then an error occurred when connecting to mysql.

Actually it is not a mysql bug.

4. Newly generated variables, if they are not automatically released, must be released immediately. Otherwise, over time, the program will crash. Many PHP programmers don’t have this

Regarding the concept of memory management, I think that memory is unlimited and can be used casually. When writing background programs, you must pay attention to memory management.

5. If you want to access files, you must first clearstatcache, otherwise there is a high risk of inaccurate statistics, or

It is no longer accurate to determine whether a file exists. What's even worse is that if you open files frequently, the handle value of the file will be

Keep increasing until it exceeds the maximum value of the integer, and the program cannot open the file. Many people’s programs fail once every 3 months,

There is no error or memory problem. It is very likely that the statcache is not cleared before each file operation.

Reprinted from: http://www.cnblogs.com/niniwzw/

www.bkjia.comtruehttp: //www.bkjia.com/PHPjc/364310.htmlTechArticleLieHuo.Net Tutorial Recently, a friend at Yahoo told me that I used to use bash csh to write scripts. , it is now using PHP to write backend programs. I feel that PHP has the most comprehensive functions and is easy to use...
Statement
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
php怎么把负数转为正整数php怎么把负数转为正整数Apr 19, 2022 pm 08:59 PM

php把负数转为正整数的方法:1、使用abs()函数将负数转为正数,使用intval()函数对正数取整,转为正整数,语法“intval(abs($number))”;2、利用“~”位运算符将负数取反加一,语法“~$number + 1”。

在 Windows 上运行 shell 脚本文件的不同方法在 Windows 上运行 shell 脚本文件的不同方法Apr 13, 2023 am 11:58 AM

适用于 Linux 的 Windows 子系统第一种选择是使用适用于 Linux 或 WSL 的 Windows 子系统,这是一个兼容层,用于在 Windows 系统上本地运行 Linux 二进制可执行文件。它适用于大多数场景,允许您在 Windows 11/10 中运行 shell 脚本。WSL 不会自动可用,因此您必须通过 Windows 设备的开发人员设置启用它。您可以通过转到设置 > 更新和安全 > 对于开发人员来完成。切换到开发人员模式并通过选择是确认提示。接下来,查找 W

php怎么除以100保留两位小数php怎么除以100保留两位小数Apr 22, 2022 pm 06:23 PM

php除以100保留两位小数的方法:1、利用“/”运算符进行除法运算,语法“数值 / 100”;2、使用“number_format(除法结果, 2)”或“sprintf("%.2f",除法结果)”语句进行四舍五入的处理值,并保留两位小数。

php怎么根据年月日判断是一年的第几天php怎么根据年月日判断是一年的第几天Apr 22, 2022 pm 05:02 PM

判断方法:1、使用“strtotime("年-月-日")”语句将给定的年月日转换为时间戳格式;2、用“date("z",时间戳)+1”语句计算指定时间戳是一年的第几天。date()返回的天数是从0开始计算的,因此真实天数需要在此基础上加1。

php怎么判断有没有小数点php怎么判断有没有小数点Apr 20, 2022 pm 08:12 PM

php判断有没有小数点的方法:1、使用“strpos(数字字符串,'.')”语法,如果返回小数点在字符串中第一次出现的位置,则有小数点;2、使用“strrpos(数字字符串,'.')”语句,如果返回小数点在字符串中最后一次出现的位置,则有。

五个鲜为人知的现代 Bash 脚本编写技术五个鲜为人知的现代 Bash 脚本编写技术Jun 26, 2023 pm 08:36 PM

程序员经常使用Bash命令语言创建Shell脚本来自动化手动任务。例如,他们会为各种配置、文件操作、生成构建结果和各种与DevOps相关的活动创建Bash脚本。几乎所有类Unix或基于Unix的操作系统都为用户提供预安装的Bash解释器,因此我们可以使用Bash编写更具可移植性的自动化脚本。正如我们已经知道的那样,Bash脚本编写是指使用Bash命令语言的语法、内置Bash命令和核心操作系统CLI程序(如GNU核心工具)编写一系列命令。标准且老式的Bash脚本通常执行一些命令并在终端上显示纯文本

php怎么读取字符串后几个字符php怎么读取字符串后几个字符Apr 22, 2022 pm 08:31 PM

在php中,可以使用substr()函数来读取字符串后几个字符,只需要将该函数的第二个参数设置为负值,第三个参数省略即可;语法为“substr(字符串,-n)”,表示读取从字符串结尾处向前数第n个字符开始,直到字符串结尾的全部字符。

php怎么查找字符串是第几位php怎么查找字符串是第几位Apr 22, 2022 pm 06:48 PM

查找方法:1、用strpos(),语法“strpos("字符串值","查找子串")+1”;2、用stripos(),语法“strpos("字符串值","查找子串")+1”。因为字符串是从0开始计数的,因此两个函数获取的位置需要进行加1处理。

See all articles

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)
2 weeks agoBy尊渡假赌尊渡假赌尊渡假赌
Repo: How To Revive Teammates
1 months agoBy尊渡假赌尊渡假赌尊渡假赌
Hello Kitty Island Adventure: How To Get Giant Seeds
4 weeks agoBy尊渡假赌尊渡假赌尊渡假赌

Hot Tools

Zend Studio 13.0.1

Zend Studio 13.0.1

Powerful PHP integrated development environment

Atom editor mac version download

Atom editor mac version download

The most popular open source editor

ZendStudio 13.5.1 Mac

ZendStudio 13.5.1 Mac

Powerful PHP integrated development environment

SublimeText3 Mac version

SublimeText3 Mac version

God-level code editing software (SublimeText3)

Dreamweaver Mac version

Dreamweaver Mac version

Visual web development tools