how to start a new process while the other ist running on

E

Erik Geiger

Hi,

sorry, my english ist not that got but I'll try.

I have a running python script (capisuit incoming.py). This script shall
start a linux shell script. If I start this script like os.system(/paht/to
shellscipt.sh) the python scipt waits for the exit of the shell script and
then goes on with the rest of the python script.

How to start a shell script without waiting for the exit of that shell
script? It shall start the shell script and immediately execute the next
python command.

Thanks for any hints

Erik
 
H

Harlin Seritt

Quickie:

os.system("/path/to/script.sh &")

More elegant, have a look at threads
 
J

Jean Brouwers

See the os. spawn* functions. For example

os.spawnv(os.P_NOWAIT, /path/to/script, args)

/Jean Brouwers
 
F

Fredrik Lundh

Erik said:
I have a running python script (capisuit incoming.py). This script shall
start a linux shell script. If I start this script like os.system(/paht/to
shellscipt.sh) the python scipt waits for the exit of the shell script and
then goes on with the rest of the python script.

How to start a shell script without waiting for the exit of that shell
script? It shall start the shell script and immediately execute the next
python command.

if you have Python 2.4, you can use the subprocess module:

http://docs.python.org/lib/module-subprocess.html

see the spawn(P_NOWAIT) example for how to use it in your
case:

http://docs.python.org/lib/node236.html

as others have noted, os.spawn(P_NOWAIT) can be used directly, as
can os.system("command&") (where the trailing "&" tells the shell to run
the command in the background).

subprocess is available for Python 2.2 and 2.3 as well, by the way:

http://www.lysator.liu.se/~astrand/popen5/

</F>
 
E

Erik Geiger

Fredrik said:
Erik Geiger wrote:
[...]
How to start a shell script without waiting for the exit of that shell
script? It shall start the shell script and immediately execute the next
python command.

if you have Python 2.4, you can use the subprocess module:

http://docs.python.org/lib/module-subprocess.html

see the spawn(P_NOWAIT) example for how to use it in your
case:

http://docs.python.org/lib/node236.html

Thats what I've tried, but it did not work. Maybe it's because I want to
start something like su -c '/path/to/skript $parameter1 $parameter2' user
I don't understand the syntax of spawn os.spawnlp(os.P_NOWAIT, "/path/to
script", "the script again?", "the args for the script?")
as others have noted, os.spawn(P_NOWAIT) can be used directly, as
can os.system("command&") (where the trailing "&" tells the shell to run
the command in the background).

Thats what I've used in the Shellscript, but I haven't realized how to use
it in thy python script, thanks! :)
subprocess is available for Python 2.2 and 2.3 as well, by the way:

http://www.lysator.liu.se/~astrand/popen5/

</F>

Erik
 
E

Erik Geiger

Jean said:
See the os. spawn* functions. For example

os.spawnv(os.P_NOWAIT, /path/to/script, args)

/Jean Brouwers
Thats what I've tried, but failed.

Thanks anyway ;-)

Greets

Erik
[...]
 
D

Donn Cave

Erik Geiger wrote:
[...]
How to start a shell script without waiting for the exit of that shell
script? It shall start the shell script and immediately execute the next
python command.

if you have Python 2.4, you can use the subprocess module:

http://docs.python.org/lib/module-subprocess.html

see the spawn(P_NOWAIT) example for how to use it in your
case:

http://docs.python.org/lib/node236.html

Thats what I've tried, but it did not work. Maybe it's because I want to
start something like su -c '/path/to/skript $parameter1 $parameter2' user
I don't understand the syntax of spawn os.spawnlp(os.P_NOWAIT, "/path/to
script", "the script again?", "the args for the script?")[/QUOTE]

Unfortunately this particular case kind of dilutes the advantages
of spawnv. In the common case, parameter1 et al. would be submitted
directly as the parameter list. I believe it may be clearer to start
with to think about the spawnv() function -
os.spawnv(os.P_NOWAIT, path, [cmdname, parameter1, parameter2])

If one of the parameters is itself another command, then of course
it has to be rendered as a string
os.spawnv(os.P_NOWAIT, '/bin/su', ['su', '-c', '%s %s %s' % (cmd,
parameter1, parameter2)])
so you have almost as much work to scan the parameters for shell
metacharacters as you would have with system().

Donn Cave, (e-mail address removed)
 
E

Erik Geiger

Donn said:
wrote: [...]
Thats what I've tried, but it did not work. Maybe it's because I want to
start something like su -c '/path/to/skript $parameter1 $parameter2'
user
Unfortunately this particular case kind of dilutes the advantages
of spawnv. In the common case, parameter1 et al. would be submitted
directly as the parameter list. I believe it may be clearer to start
with to think about the spawnv() function -
os.spawnv(os.P_NOWAIT, path, [cmdname, parameter1, parameter2])

If one of the parameters is itself another command, then of course
it has to be rendered as a string
os.spawnv(os.P_NOWAIT, '/bin/su', ['su', '-c', '%s %s %s' % (cmd,
parameter1, parameter2)])
so you have almost as much work to scan the parameters for shell
metacharacters as you would have with system().

Donn Cave, (e-mail address removed)
OK, thats too high for me ;-)

Is the %s the variable for the following commands/parameters? If I have
three parameters, I'll need one more %s? Where to write the user for the su
command?

Well, I've given up ;-)

Thanks for the explanation!

Erik
 
K

Keith Dart

Donn said:
wrote: [...]
Thats what I've tried, but it did not work. Maybe it's because I want to
start something like su -c '/path/to/skript $parameter1 $parameter2'
user
Unfortunately this particular case kind of dilutes the advantages
of spawnv. In the common case, parameter1 et al. would be submitted
directly as the parameter list. I believe it may be clearer to start
with to think about the spawnv() function -
os.spawnv(os.P_NOWAIT, path, [cmdname, parameter1, parameter2])

If one of the parameters is itself another command, then of course
it has to be rendered as a string
os.spawnv(os.P_NOWAIT, '/bin/su', ['su', '-c', '%s %s %s' % (cmd,
parameter1, parameter2)])
so you have almost as much work to scan the parameters for shell
metacharacters as you would have with system().

Donn Cave, (e-mail address removed)
OK, thats too high for me ;-)

Is the %s the variable for the following commands/parameters? If I have
three parameters, I'll need one more %s? Where to write the user for the su
command?

Well, I've given up ;-)

Hey, don't give up. There is help. ;-) guess...

There is a package called pyNMS (http://sourceforge.net/projects/pynms)
That has a module called "proctools", and also one called "sudo". Once
you set up sudo correctly, you can do what you want easily.

However, be aware that proctools spawns a process with the subprocess
stdio connected to _your_ parent process, and does not inherit the stdio
of the parent. So, if the subprocess writes a lot of stuff you must read
it, or the subprocess will block. However, there is some (perhaps buggy)
support for asynchronous operation where those reads happen
automatically (on Linux).
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
474,438
Messages
2,571,699
Members
48,796
Latest member
Greg L.
Top