5

I may not be looking hard enough but I am trying to replace a running python script with another python script. Based on the research that I have done, using the os.execl function may be what I am looking for. I am a little bit confused about the arguments that should be used with the function. Can anyone please help explain to me how to just replace the currently running python script with another.

user3286192
  • 95
  • 1
  • 7

1 Answers1

4

exec*() familiy replaces the whole process, keeping the process number (PID). If this is what you want...

One example inside Python interpreter. I replace the interpreter by echo. The first item in 'args' becomes the argv[0] which is the process name that is seen in ps or top.

>>> import os
>>> args=['process_name', 'bla', 'ble']
>>> os.execlp("/bin/echo", *args)
bla ble
/Users/epx $ 
epx
  • 1,066
  • 9
  • 17
  • 1
    Thanks for the help but I am still a little confused. If I had a script called test.py and it was in the /home/pi directory, how would I replace the currently running script with the test.py script? – user3286192 Feb 08 '14 at 03:30
  • You would have /home/pi/test.py instead of /bin/echo. The script must have execute permission and the appropriate #!/usr/bin/env python header. (Make sure it executes in prompt before trying to execl() it.) I guess ./test.py would work if your current directory was /home/pi, or simply 'test.py' would work if PATH included /home/pi. – epx Feb 08 '14 at 03:33
  • 1
    Thank you so much, I think a have a tiny grasp on how this thing works now! – user3286192 Feb 08 '14 at 03:47