|  support |  documentation |  report a bug |  advanced search |  search howto |  statistics |  random bug |  login
Request #29479 changing current process name
Submitted: 2004-08-01 10:43 UTC Modified: 2009-01-30 05:31 UTC
Avg. Score:4.4 ± 0.7
Reproduced:15 of 15 (100.0%)
Same Version:1 (6.7%)
Same OS:13 (86.7%)
From: black at scene-si dot org Assigned:
Status: Closed Package: Feature/Change Request
PHP Version: 6CVS OS: linux
Private report: No CVE-ID: None
View Add Comment Developer Edit
Anyone can comment on a bug. Have a simpler test case? Does it work for you on a different platform? Let us know!
Just going to say 'Me too!'? Don't clutter the database with that please !
Your email address:
Solve the problem:
19 - 10 = ?
Subscribe to this entry?

 [2004-08-01 10:43 UTC] black at scene-si dot org
With linux it is sometimes useful to be able to change the process name (by identifying a process in the system error logs or for debugging for example)..

The c(++) or the perl way doesnt work in php as far as i tried, and so i pressume that it is not possible itself.

You can consult yourself with - an extensive example of how the title should be changed

Reproduce code:
$argv[0] = "progname-debugval";

Expected result:
I expect that the programs process title would be changed by modifying $argv[0], or by introducing a new function which would change the process title respectively.

Actual result:
The process name in `ps` output of the respective program should change accordingly to the change of $argv[0];


Add a Patch

Pull Requests

Add a Pull Request


AllCommentsChangesGit/SVN commitsRelated reports
 [2004-08-01 13:25 UTC]
setproctitle() is only implemented on BSD; other systems that emulate this use a non-portable dangerous hack that makes certain assumptions about how the process will be run.

Suspending until this situation changes.
 [2005-10-06 15:41 UTC] unclemonty at gmail dot com
Did the situation ever change with this? How about a work-around?
 [2005-10-06 15:44 UTC]
Feel free to provide one.
 [2006-02-18 10:39 UTC] dan812 at hotmail dot com
This would be a very nice feature. Using perl you can change $0 variable. PHP do lack this functionality.

---From perlvar doc-----
$0 Contains the name of the program being executed.

On some (read: not all) operating systems assigning to $0 modifies the argument area that the "ps" program sees.  On some platforms you may have to use special "ps" options or a different "ps" to see the changes.

Modifying the $0 is more useful as a way of indicating the current program state than it is for hiding the program you're running.  (Mnemonic: same as sh and ksh.)

Note that there are platform specific limitations on the the maximum length of $0.  In the most extreme case it may be limited to the space occupied by the original $0.

In some platforms there may be arbitrary amount of padding, for example space characters, after the modified name as shown by "ps".  In some platforms this padding may extend all the way to the original length of the argument area, no matter what you do (this is the case for example with Linux 2.2).

Note for BSD users: setting $0 does not completely remove "perl" from the ps(1) output.  For example, setting $0 to "foobar" may result in "perl: foobar (perl)" (whether both the "perl: " prefix and the " (perl)" suffix are shown depends on your exact BSD variant and version).  This is an operating system feature, Perl cannot help it.

In multithreaded scripts Perl coordinates the threads so that any thread may modify its copy of the $0 and the change becomes visible to ps(1) (assuming the operating system plays along).  Note that the the view of $0 the other threads have will not change since they have their own copies of it.

<a href="">venda</a>
 [2006-05-15 16:56 UTC] jerj at coplanar dot net
It appears someone has implemented this:

I'm going to try and compile it now.
 [2006-08-22 21:22 UTC] jdoak at google dot com
Is there currently a way to do this in php 5?  It doesn't appear that the pecl module listed below is actually around any longer:

and a search for it turned up nothing.  I'm just curious if this has been implemented or is this possible now on linux systems?
 [2007-06-06 12:54 UTC] xdecock at gmail dot com
I've tried to make a port of the apache thing used for wikipedia to a more generic one.

If it can be usefull for anyone (only tested it on cli, as i search the way of doing this for cli primary)

Not sure how good is this method, at first seen, it works

proctitle.h & config.m4 are almost the same (except for MREQUEST_INIT & MREQUEST_SHUTDOWN), but it is not usefull for CLI

the url for those files:

  | PHP Version 4                                                        |
  | Copyright (c) 1997-2003 The PHP Group                                |
  | This source file is subject to version 2.02 of the PHP license,      |
  | that is bundled with this package in the file LICENSE, and is        |
  | available at through the world-wide-web at                           |
  |                                 |
  | If you did not receive a copy of the PHP license and are unable to   |
  | obtain it through the world-wide-web, please send a note to          |
  | so we can mail you a copy immediately.               |
  | Author:                                                              |


#define MAXTITLE 1024

#include "config.h"

#include <syslog.h>

#include "php.h"
#include "php_ini.h"
#include "SAPI.h"
#include "ext/standard/info.h"
#include "php_proctitle.h"
#include <dlfcn.h>

/* If you declare any globals in php_proctitle.h uncomment this:

/* True global resources - no need for thread safety here */
static int le_proctitle;
static char *proctitle_argv=NULL;
static char process_base_name[MAXTITLE+1];
void setproctitle(char *title) {
        if (proctitle_argv){


/* {{{ proctitle_functions[]
 * Every user visible function must have an entry in proctitle_functions[].
function_entry proctitle_functions[] = {
	PHP_FE(setproctitle,	NULL)		/* For testing, remove later. */
	{NULL, NULL, NULL}	/* Must be the last line in proctitle_functions[] */
/* }}} */

/* {{{ proctitle_module_entry
zend_module_entry proctitle_module_entry = {
#if ZEND_MODULE_API_NO >= 20010901
	NULL,				/* Replace with NULL if there's nothing to do at request start */
	NULL,				/* Replace with NULL if there's nothing to do at request end */
#if ZEND_MODULE_API_NO >= 20010901
	"0.1", /* Replace with version number for your extension */
/* }}} */


	sapi_module_struct *symbol;
	if(!proctitle_argv) {
    	symbol=(sapi_module_struct *)dlsym(NULL,"sapi_module");
		if (symbol){
			sprintf(process_base_name,"PHP %s: %%s",symbol->name);
	return SUCCESS;
/* }}} */

	return SUCCESS;
/* }}} */

	php_info_print_table_header(2, "proctitle support", "enabled");
/* }}} */

	char *string = NULL;
	int str_len;

	if (zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC, "s", &string, &str_len) == FAILURE) {
		setproctitle("System: Erreur");
/* }}} */

 * Local variables:
 * tab-width: 4
 * c-basic-offset: 4
 * End:
 * vim600: noet sw=4 ts=4
 * vim<600: noet sw=4 ts=4
 [2008-06-09 22:52 UTC] lindsay dot snider at gmail dot com
Here is a shorter version based off of xdecock's code which has been working well.  I see value in having this available when using PHP in CLI mode.

#include <php.h>
#include <SAPI.h>
#include <dlfcn.h>
#include <string.h>

static char *argv0 = NULL;
#define MAX_TITLE_LENGTH        128

void setproctitle(char *title)
        char    buffer[MAX_TITLE_LENGTH];
        int     tlen = strlen(title);

        memset(buffer, 0x20, MAX_TITLE_LENGTH);
        buffer[MAX_TITLE_LENGTH-1] = '\0';

        if( tlen < (MAX_TITLE_LENGTH-1) )
                memcpy(buffer, title, tlen);

        if( argv0 )
                snprintf(argv0, MAX_TITLE_LENGTH, "%s", buffer);

void set_proctitle_init()
        sapi_module_struct *symbol = NULL;

        symbol = (sapi_module_struct *)dlsym(NULL, "sapi_module");
        if( symbol )
                argv0 = symbol->executable_location;

        char    *title;
        long    tlen;

        if(zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC, "s",&title, &tlen) == FAILURE)

 [2009-01-21 20:56 UTC]
Just for info since a lot of people seems to be looking for this kind of solution (I'm one of them), I've hosted the proctitle extension on a SVN repository, updated and fixed.

Available at:

Please let me know if you want to add things to this extension. I tested it with PHP 5.2.8 and PHP 5.3.0alpha3, without problems.
 [2009-01-21 22:35 UTC]

I would suggest to simply propose it to pecl-dev. That's the place to discussion proposals for PECL.

 [2009-01-30 05:31 UTC]
This feature is now covered by the proctitle pecl extension, which is based on code written by the different

I believe this bug can now be closed.
PHP Copyright © 2001-2024 The PHP Group
All rights reserved.
Last updated: Mon Mar 04 04:01:28 2024 UTC