cli_set_process_title / cli_get_process_title

cli_set_process_title(string $title): bool · cli_get_process_title(): ?string

CLI process title read/write: cli_set_process_title rewrites the process name ps/top displays (giving each worker a recognizable identity), cli_get_process_title reads it back; the demo follows the "set, then set back" discipline — no residue left on the host process.

C C23

C23
/* Darwin exposes setprogname/getprogname in stdlib.h (the argv[0]-derived
   name); Linux needs prctl(PR_SET_NAME) instead - set, read back, restore */
#include <stdio.h>
#include <stdlib.h>
#include <string.h>

int main(void) {
    const char *old = getprogname(); /* snapshot before any change */
    setprogname("codeabc-a51");
    printf("%s\n", getprogname()); /* readback: the name changed */
    setprogname(old); /* restore: leave the host process as we found it */
    printf("%d\n", strcmp(getprogname(), old) == 0);
    return 0;
}
Output
codeabc-a51
1

C++ C++26

C++26
#include <cstdio>
#include <cstdlib>
#include <cstring>

// same as C: Darwin setprogname/getprogname; Linux prctl(PR_SET_NAME)
int main() {
    const char *old = getprogname(); /* snapshot */
    setprogname("codeabc-a51");
    std::printf("%s\n", getprogname());
    setprogname(old); /* restore */
    std::printf("%d\n", std::strcmp(getprogname(), old) == 0);
    return 0;
}
Output
codeabc-a51
1

C# v11.0.100

v11.0.100
using System.Diagnostics;

// no title setter in .NET either (P/Invoke prctl on Linux is the real route);
// the std-visible facts: process name derived from the executable, and the pid
using Process current = Process.GetCurrentProcess();
Console.WriteLine(current.ProcessName.Length > 0);
Console.WriteLine(current.Id > 0);
Output
True
True

Go v1.27.4

v1.27.4
package main

import (
	"fmt"
	"os"
)

// std cannot set the process title: on Linux use prctl(PR_SET_NAME) via
// golang.org/x/sys/unix (or setproctitle); the std-visible fact is argv[0]
func main() {
	fmt.Println(len(os.Args) > 0) // what ps shows as the command line
	fmt.Println(os.Getpid() > 0)  // the pid ps would list
}
Output
true
true

Java v27.0.1

v27.0.1
import java.lang.management.ManagementFactory;

public class Main {
    public static void main(String[] args) {
        // the JVM offers no title setter; what ps shows is the java binary
        // with your classpath - the std-visible self-identification:
        String name = ManagementFactory.getRuntimeMXBean().getName(); // pid@host
        System.out.println(name.matches("\\d+@.+")); // the pid ps would list, host attached
        System.out.println(name.split("@")[0].matches("\\d+")); // the pid part is numeric
    }
}
Output
true
true

JavaScript v26.13.1

v26.13.1
// Node HAS the setter: process.title - argv0 memory swap, ps-visible
const old = process.title;
process.title = "codeabc-a51";
console.log(process.title === "codeabc-a51"); // get/set roundtrip
process.title = old; // restore: leave the host process as we found it
console.log(process.title.length > 0); // restored to a real title
Output
true
true

PHP v8.6.0

v8.6.0
// cli_set_process_title(name): bool / cli_get_process_title(): ?string -
// what `ps` shows for this CLI process. Set, read back, RESTORE:
// a demo must not hijack the host process it runs in.
$old = cli_get_process_title();
var_dump(cli_set_process_title("codeabc-a51"));
echo cli_get_process_title(), "\n"; // readback: the title changed
cli_set_process_title($old); // restore - leave the host process as we found it
echo cli_get_process_title() === $old ? "restored" : "changed", "\n";
Output
bool(true)
codeabc-a51
restored

Python v3.15.2

v3.15.2
# CPython exposes no process-title API: third-party setproctitle wraps
# prctl(PR_SET_NAME) on Linux and the Darwin title call on macOS.
# std-visible self-identification: sys.argv[0] and the pid
import os
import sys

print(bool(sys.argv))   # argv[0] is what ps shows as the command
print(os.getpid() > 0)  # the pid ps would list
Output
True
True

Rust v1.99.1

v1.99.1
// std has no title API; on macOS libSystem's setprogname/getprogname are
// one extern away (Linux: prctl PR_SET_NAME); set, read back, restore
use std::os::raw::c_char;

extern "C" {
    fn setprogname(name: *const c_char);
    fn getprogname() -> *const c_char;
}

fn as_str(p: *const c_char) -> String {
    unsafe {
        let mut len = 0usize;
        while *p.add(len) != 0 {
            len += 1;
        }
        String::from_utf8_lossy(std::slice::from_raw_parts(p as *const u8, len)).into_owned()
    }
}

fn main() {
    let old_ptr = unsafe { getprogname() };
    let old = as_str(old_ptr); // snapshot before any change
    unsafe { setprogname(b"codeabc-a51\0".as_ptr() as *const c_char) };
    println!("{}", unsafe { as_str(getprogname()) } == "codeabc-a51"); // get/set roundtrip
    unsafe { setprogname(old_ptr) }; // restore: leave the host as we found it
    println!("{}", unsafe { as_str(getprogname()) } == old);
}
Output
true
true

TypeScript v26.13.1

v26.13.1
// Node HAS the setter: process.title - argv0 memory swap, ps-visible
const old: string = process.title;
process.title = "codeabc-a51";
console.log(process.title === "codeabc-a51"); // get/set roundtrip
process.title = old; // restore: leave the host process as we found it
console.log(process.title.length > 0); // restored to a real title
Output
true
true

Language differences

Setter vs reader: cli_set_process_title gives each process in a multi-process PHP setup (the worker pool after pcntl_fork) a recognizable identity (ps -f shows who is who at a glance), cli_get_process_title reads it back; the deterministic assertions use a set/get roundtrip: set returns true, get reads back "codeabc-a51", and after setting the old value back, get equals the snapshot — the whole demo follows "set, then set back" so the host process title is never hijacked.

PHP: CLI SAPI only (php-cli is built separately; the function is absent or a no-op under FPM); returns bool; the title is written into argv memory (replacing argv[0]), directly visible to ps/top/htop; related: cli_get_process_title() may return null (no title set).

Python: CPython has no such API — the third-party setproctitle package is the de facto standard (wrapping Linux prctl(PR_SET_NAME) and the macOS pstitle call); std exposes only sys.argv[0] and os.getpid().

Go: no std setter — on Linux use golang.org/x/sys/unix.Prctl(PR_SET_NAME, ...) or a community setproctitle package; os.Args[0]/os.Getpid() are read-only facts.

Java: the JVM has no title API (ps always shows java plus the long classpath); ManagementFactory.getRuntimeMXBean().getName() gives a "pid@host" self-identification; in practice naming happens at the thread level, not process.

C#: no setter — P/Invoke prctl(PR_SET_NAME) on Linux, or third parties; Process.ProcessName (derived from the executable) and Process.Id are the read-only facts.

JavaScript/TypeScript: Node is the only runtime of the ten that ships a setter directly — process.title = "name" takes effect immediately and is ps-visible (the docs note reads may be incomplete on Windows); the example likewise performs set/read/restore.

C/C++: macOS's libc only has argv[0]-level setprogname/getprogname (stdlib.h, used here); full titles need Linux prctl(PR_SET_NAME) (glibc, _GNU_SOURCE, truncated at 15 bytes) — differing per-platform APIs are the main cross-platform trap of this topic; this toolchain compiles on macOS, so the example uses setprogname and leaves the prctl route to these notes.

Rust: no std API — on macOS extern to libSystem's setprogname/getprogname (same source as C), on Linux prctl(PR_SET_NAME) via the libc crate; the example likewise snapshots, sets, reads back, and restores.