Python Forum
changing arguments as seen by ps
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
changing arguments as seen by ps
#1
in POSIX systems i can code in C to change the arguments that would be seen by the ps command. the typical reason for this is to identify what a each process, of many that are forked by a parent process, is doing. in Python, i want to do the same in each child i create with the multiprocessing module. in my current project i can launch 2 to 18 child worker processes, each accessing a different AWS region. i'd like to put that specific region name on that process' command line, as seen in ps. how can i do that in Python?
Tradition is peer pressure from dead people

What do you call someone who speaks three languages? Trilingual. Two languages? Bilingual. One language? American.
Reply
#2
How do you do that in C?
Reply
#3
in C you just update the pointer array passed to main in the 2nd argument to reference the new strings. the new array must be no longer than the old array or stack corruption is possible. a shorter array must end with NULL.
Tradition is peer pressure from dead people

What do you call someone who speaks three languages? Trilingual. Two languages? Bilingual. One language? American.
Reply
#4
It looks to me like borderline hacking. Are you trying to corrupt the stack? Anyway, I don't see how you could get the address of the C argv from the python code.
Reply
#5
The package setproctitle could do this for you.

from subprocess import call                          
                                                     
from setproctitle import (                           
    getproctitle,                                    
    setproctitle,                                    
)                                                    
                                                     
print(f'Current procname is {getproctitle()}')       
print('Setting proctitle to foo.exe')                
setproctitle('foo.exe')                              
call(['ps', 'ux'])                                   
And with multiprocessing:
import time                                    
from multiprocessing import Pool               
from subprocess import call                    
                                               
from setproctitle import (                     
    getproctitle,                              
    setproctitle,                              
)                                              
                                               
def worker(title):                             
    setproctitle(title)                        
    call(['ps', 'ux'])                         
    time.sleep(5)                              
                                               
print(f'Current procname is {getproctitle()}') 
print('Setting proctitle to foo.exe')          
setproctitle('foo.exe')                        
call(['ps', 'ux'])                             
                                               
                                               
print('Running processes in worker')           
with Pool() as pool:
    pool.map(worker, [f'some_name_{i:02d}_.exe' for i in range(10)])
I think the module is using C to change the procname. I guess they do it in the same way, as you described.
Almost dead, but too lazy to die: https://sourceserver.info
All humans together. We don't need politicians!
Reply
#6
i would agree to it being borderline hacking. i did some interesting hacking back in mainframe days. some things i did needed the CPU to be running in "supervisor state" which allowed access to anything, use of more instructions, and bypass virtual address translation. back then we didn't call it hacking. we just called it making it work.
Tradition is peer pressure from dead people

What do you call someone who speaks three languages? Trilingual. Two languages? Bilingual. One language? American.
Reply


Possibly Related Threads…
Thread Author Replies Views Last Post
  changing the process command line arguments in python Skaperen 3 3,025 Aug-19-2019, 02:40 PM
Last Post: wavic
  Functions (Arguments Passing,Changing a mutable ,Assignment to Arguments Names) Adelton 2 3,883 Mar-02-2017, 10:23 PM
Last Post: zivoni

Forum Jump:

User Panel Messages

Announcements
Announcement #1 8/1/2020
Announcement #2 8/2/2020
Announcement #3 8/6/2020