From the python docs:
The various exec*() functions take a list of arguments for the new program loaded into the process. In each case, the first of these arguments is passed to the new program as its own name rather than as an argument a user may have typed on a command line. For the C programmer, this is the argv[0] passed to a program’s main(). For example, os.execv('/bin/echo', ['foo', 'bar']) will only print bar on standard output; foo will seem to be ignored.
Can someone please help me understand this? What do I need to do if I want to run my own program with some parameters?
UNIX, where all these exec
things come from, separated the program executable file from the program name, so that your process could have any arbitrary name.
The first argument is the program that will run. This must exist. The next argument is what your process running the program will be called, what will be in argv[0]
, and what comes up in the ps
(process list) output.
So, if I did (in C, but it maps to Python as well):
execl ("/usr/bin/sleep", "notsleep", "60", NULL);
This would run the program /usr/bin/sleep
but it would show up in the process list as notsleep
. argv[0]
would be notsleep
and argv[1]
(the actual argument) would be 60. Often, the first two parameters will be identical but it's by no means required.
That's why the first argument of your list is (seemingly) ignored. It's the name to give to the process, not the first argument to it.
A more correct way to do it would be:
os.execv('/bin/echo', ['echo', 'foo', 'bar'])
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With