S-T
S-T

Reputation: 489

process spawning in python

I want to use os.spawn* function to make a upstart service call. The reason to use os.spawn over subprocess is because os.spawn calls provides more control over the way the program is launched though with a little complex call signature.

My command is :

sudo start service/new_service db=tmp

I am not sure how to run string command using os.spawn* function family.

Upvotes: 1

Views: 2091

Answers (1)

lvc
lvc

Reputation: 35059

The only 'more control' that os.spawn gives you over subprocess is the mode argument - but on Unix, this can only control whether the call blocks waiting for the subprocess to finish, which is also doable with subprocess.

In any case, the best way to turn your command into a list of arguments is to use the shlex.split function, as recommended by the subprocess docs:

command = 'sudo start service/new_sevice db=tmp'
subprocess.call(shlex.split(command))

If you really want to use os.spawn* family (and you probably don't), you can also use shlex.split - it gives its result in the form subprocess expects which differs slightly from the form os.spawn* expect, but you can get around this easily enough using the spawnl* variants and Python's argument unpacking:

os.spawnlp(os.P_WAIT, *shlex.split(command))

Upvotes: 1

Related Questions