Table of Contents

Request for Comments: PHP CLI changing process title support

Introduction

This RFC proposes a new way of setting a PHP CLI process's title that's visible in top or ps. The implementation is based based off the one in PostgreSQL.

This is very helpful when using PHP as a daemon, for example: GearmanManager. Otherwise, all job workers look the same in top and ps:

php -d display_errors=1 -d display_startup_errors=1 -d error_log=/var/log/gearman/php.log /var/code/bin/GearmanManager/run-gearman.php -c /var/code/bin/GearmanManager/config.php -p JobWorker_ -P /var/run/gearman_manager/gearman_manager.pid -d -x 86400 -w /var/code/phplib/JobWorker/ -l /var/log/gearman/gearman_manager.log -vvv

Currently, there are 2 ways to debug what job a process is doing: attach gdb and introspect the appropriate data structure. Or log the process-pid to a file at job start-up and match it up.

Both are doable, but time consuming and having functionality in the language to set the title would solve the problem nicely.

Implementation

There already exists a PECL extension proctitle that does something similar, but it is incomplete and might lead to memory corruption on Linux (or any OS which does not support setproctitle.

The reason is the extension only has access to original argv[0] (that comes from main()). argv and environ(7) are in contiguous memory space on Linux. The extension presumes that argv[0] can accomodate 128 characters, but usually that is not possible because argv[0] is “php”. When this happens, the extension will scribble on argv[1], argv[2], etc., and maybe even environ and this can have destructive side effects on the running program.

The proposed patch does not suffer from the same issue. It works by hooking into PHP CLI's main(). An init method is the first function call made in main().

Support for setting the title is also built into the cli-server SAPI.

Note that it is not possible to use similar logic for other web-servers like Apache because we'd need to deep-copy and replace argv and environ before any real code has executed and such low-level access isn't possible there.

Example

<?php
 
$title = "This is a test title";
if (!cli_set_process_title($title)) {
    echo "Unable to set title\n";
    exit(1);
}
 
sleep(15);
echo cli_get_process_title() . "\n";

The above example will output:

Specification

Description

bool cli_set_process_title(string $title)

cli_set_process_title() sets title of the process to title and returns TRUE if the operation succeeded. Or FALSE if it fails. On failure, a WARNING is emitted with details as to why it failed, the most common cause being the operating system is not supported.

Parameters

title

Description

string cli_get_process_title()

cli_get_process_title() returns a string containing the title that was set using cli_set_process_title.

Note that this returned string may not match what ps/top would show; for example on FreeBSD in ps, you would see the process name as “php: title (php)” where title is what was passed to cli_set_process_title.

If the method fails for any reason, a WARNING is emitted and NULL is returned; the most common cause of failure being the operating system is not supported.

Parameters

None

Patch

The pull request (including tests) based on PHP-5.5 is here.

Voting

The PHP language is not changed, so a 50% + 1 majority is required.

Voting ends March 4th, 2013.

Accept modifying CLI process title in PHP?
Real name Yes No
ab (ab)  
ashnazg (ashnazg)  
auroraeosrose (auroraeosrose)  
bjori (bjori)  
brianlmoon (brianlmoon)  
cataphract (cataphract)  
cpriest (cpriest)  
frozenfire (frozenfire)  
ifeghali (ifeghali)  
indeyets (indeyets)  
jpauli (jpauli)  
jwage (jwage)  
kalle (kalle)  
keyur (keyur)  
laruence (laruence)  
levim (levim)  
lstrojny (lstrojny)  
marco (marco)  
mike (mike)  
mj (mj)  
pajoye (pajoye)  
pollita (pollita)  
ralphschindler (ralphschindler)  
rasmus (rasmus)  
reeze (reeze)  
stas (stas)  
treffynnon (treffynnon)  
tyrael (tyrael)  
weierophinney (weierophinney)  
Final result: 28 1
This poll has been closed.

Changelog