garima721
garima721

Reputation: 323

Change process name while executing a python script

I have some python script name abc.py, but when I execute it, I want to run with some different name, say "def".

Is it possible in python without installing any additional module (procname)?

Upvotes: 2

Views: 2434

Answers (1)

Kijewski
Kijewski

Reputation: 26043

You can use pure ctypes calls:

import ctypes

lib = ctypes.cdll.LoadLibrary(None)
prctl = lib.prctl
prctl.restype = ctypes.c_int
prctl.argtypes = [ctypes.c_int, ctypes.c_char_p, ctypes.c_ulong,
                  ctypes.c_ulong, ctypes.c_ulong]

def set_proctitle(new_title):
    result = prctl(15, new_title, 0, 0, 0)
    if result != 0:
        raise OSError("prctl result: %d" % result)

Compare:

Upvotes: 2

Related Questions