2

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)?

garima721
  • 323
  • 3
  • 14
  • Why cannot you just rename the file and run? A more detailed requirement would be required :) – Anand S Kumar Jun 30 '15 at 07:41
  • Using procname is the correct answer. Why don't you want to use this module? – Kijewski Jun 30 '15 at 07:43
  • @Kay: This module doesn't come with default python package. This module has to be additionally installed on machine, which cant be done due to some permission reasons. – garima721 Jun 30 '15 at 08:25
  • I had a look at the source for the module. It's using system calls in c - which I'm guessing is the only way to get the behaviour you want. – Aidan Kane Jun 30 '15 at 08:34

1 Answers1

2

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:

Kijewski
  • 25,517
  • 12
  • 101
  • 143