How to Rename Process Names in Linux for Better Process Management and Identification


2 views

When running multiple Python servers or similar applications on a single Linux machine, they all typically appear with the same name (python) in process listings. This creates several practical issues:

  • Cannot selectively kill specific instances
  • Difficult to monitor individual services
  • Complicates resource tracking per service

Method 1: Using Symbolic Links

The simplest approach is creating dedicated symlinks to the Python interpreter for each service:

# Create symlink for development server
sudo ln -s /usr/bin/python3 /usr/local/bin/dev_server_python

# Create symlink for production server
sudo ln -s /usr/bin/python3 /usr/local/bin/prod_server_python

Then launch your scripts using these custom-named interpreters:

# For development
/usr/local/bin/dev_server_python dev_server.py

# For production
/usr/local/bin/prod_server_python prod_server.py

Method 2: Using argv[0] Modification

Python provides a way to modify how the process appears in ps:

import sys
import os

def rename_process(new_name):
    try:
        from ctypes import cdll, c_char_p
        libc = cdll.LoadLibrary(None)
        libc.prctl(15, c_char_p(new_name.encode()), 0, 0, 0)  # PR_SET_NAME
    except:
        pass
    sys.argv[0] = new_name

# Example usage:
rename_process("my_custom_server")

Method 3: Using exec in Bash Scripts

Create wrapper scripts that use exec to rename the process:

#!/bin/bash
# dev_server_wrapper.sh
exec -a "dev_server_python" /usr/bin/python3 /path/to/dev_server.py

Systemd Unit Customization

When running as systemd services, you can customize the process name:

[Service]
ExecStart=/usr/bin/python3 /path/to/server.py
ExecStartPre=/bin/bash -c 'echo "custom_name" > /proc/self/comm'

Python-specific Solutions

For Python applications, consider these packages:

# Using setproctitle package
pip install setproctitle

import setproctitle
setproctitle.setproctitle("my_custom_server")

After implementing any method, verify with:

ps aux | grep custom_name
# Or for more details
ps -eo pid,comm,cmd | grep custom_name

Common issues to check:

  • Permission problems with symlinks
  • Missing dependencies (like ctypes or setproctitle)
  • SELinux/AppArmor restrictions

When running multiple Python servers on a single machine, you'll notice they all appear simply as "python" in process listings. This becomes problematic when you need to manage individual processes, especially when some are in development while others are in production.


$ ps aux | grep python
user     12345  0.0  0.1 123456 7890 ?        S    Jan01   0:00 python server_prod.py
user     12346  0.0  0.1 123456 7890 ?        S    Jan01   0:00 python server_dev.py

One effective approach is creating symlinks to the Python interpreter with custom names:


$ sudo ln -s /usr/bin/python3 /usr/local/bin/server_prod
$ sudo ln -s /usr/bin/python3 /usr/local/bin/server_dev

Then launch your processes using these symlinks:


$ server_prod server_prod.py &
$ server_dev server_dev.py &

Create wrapper scripts that rename the process during execution:


#!/bin/bash
exec -a "custom_process_name" python3 "$@"

Make it executable and use it to launch your Python scripts:


$ chmod +x custom_launcher.sh
$ ./custom_launcher.sh my_script.py

For Python processes, the setproctitle module provides an elegant solution:


import setproctitle
setproctitle.setproctitle("my_custom_process")

# Rest of your application code...

After implementing any of these solutions, verify with:


$ ps aux | grep custom

Now you can precisely target specific processes for management:


$ pkill -f "my_custom_process"