Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

os.execv without args argument

Tags:

python

I want to replace the current process with a new one using os.execv, this works fine unless you don't have any arguments.

How can I call this even if I don't have an arguments to pass to the process I want to launch ?

# Works fine, unless the arguments tuple wouldn't exist or be empty
os.execv('process.exe', ('arg1', 'arg2'))
like image 749
Not Available Avatar asked Aug 01 '10 22:08

Not Available


3 Answers

These three variants can resolve the problem:

cmd = '/usr/bin/vi'
os.execv(cmd, (' ',))
os.execv(cmd, [' '])
os.execl(cmd, '')

Usually, the first parameter of an argument list (sys.argv) is the command which had been used to invoke the application. So it is better to use one of those:

cmd = '/usr/bin/vi'
os.execv(cmd, (cmd,))
os.execv(cmd, [cmd])
os.execl(cmd, cmd)

os.exec* documentation on python.org

like image 138
phobie Avatar answered Nov 13 '22 18:11

phobie


Okay, after asking on IRC they pointed out why it works this way.

The first argument (arg0) is normally the filename of what you're executing (sys.argv[0] for example), so the first argument should always be the filename.

This explains why the arguments aren't optional, on IRC they said that arg0 is what the app will think its name is.

like image 45
Not Available Avatar answered Nov 13 '22 18:11

Not Available


This works for me

os.execv('process',())

are you sure your process work without arguments?

Or try execl

os.execl('process')
like image 32
Vinko Vrsalovic Avatar answered Nov 13 '22 20:11

Vinko Vrsalovic