newbie: popen question

  • Thread starter thebiggestbangtheory
  • Start date
T

thebiggestbangtheory

hello everyone :),
I am a newbie to python. I am trying to run a
bash script from within a python program. I would greatly appreciate
any pointers/comments about how to get around the problem I am facing.

I want to run bash script: code.sh from within a python program.
code.sh needs to be run like so from the command line
Code:
$ sudo code.sh arg1 arg2

I read up on some documentation but am not very clear about how to use
popen. I want to relegate the shell to a background process, but it
needs to accept the sudo passwd too!

I have tried
Code:
p = subprocess.Popen(['/bin/bash', 'sudo '+mypath+'code.sh '+arg1+'
'+arg2],
                                    stdout=subprocess.PIPE,
                                    stderr=subprocess.STDOUT)
I tried some code from stackoverflow.com/questions/694000/why-doesnt-
subprocess-popen-always-return

nothing really happens when this executes, the PIPE option pshes it to
the background and I can't push in the sudo passwd. Can someone please
give me an idea of how to go about this.

To recap, I want to run a shell script, which needs to be started with
sudo, and then push it into the background.

Thanks,
-A
 
S

Sean DiZazzo

hello everyone :),
                         I am a newbie to python. I am trying to run a
bash script from within a python program. I would greatly appreciate
any pointers/comments about how to get around the problem I am facing.

I want to run  bash script: code.sh from within a python program.
code.sh needs to be run like so from the command line
Code:
$ sudo code.sh arg1 arg2

I read up on some documentation but am not very clear about how to use
popen. I want to relegate the shell to a background process, but it
needs to accept the sudo passwd too!

I have tried
Code:
p = subprocess.Popen(['/bin/bash', 'sudo '+mypath+'code.sh '+arg1+'
'+arg2],
                                    stdout=subprocess.PIPE,
                                    stderr=subprocess.STDOUT)
I tried some code from stackoverflow.com/questions/694000/why-doesnt-
subprocess-popen-always-return

nothing really happens when this executes, the PIPE option pshes it to
the background and I can't push in the sudo passwd. Can someone please
give me an idea of how to go about this.

To recap, I want to run a shell script, which needs to be started with
sudo, and then push it into the background.

Thanks,
-A

Your best bet is to make sudo not ask for a password. :) If you
don't have the rights, then you can use pexpect to do what you want to
do. http://pexpect.sourceforge.net/pexpect.html

See the second example on that page.

child = pexpect.spawn('scp foo (e-mail address removed):.')
child.expect ('Password:')
child.sendline (mypassword)

~Sean
 
T

thebiggestbangtheory

The sudo password prompt is very configurable, so changing the configuration
to allow execution without password input is really the best option.

Thanks guys for helping out! very good answers :)

Before I saw your answers, I tried the following,

output = subprocess.Popen(["sudo","-b", "code.sh", "arg1"],
stdout=subprocess.PIPE).communicate()[0]

This seemed to push the shell execution process to the background and
because my python program was invoked initially with sudo, it seems I
did not need to enter a passwd again.

Any comments about this..any issues that you see will crop up?

Thanks a ton again.
 
L

Lie Ryan

The sudo password prompt is very configurable, so changing the configuration
to allow execution without password input is really the best option.

Thanks guys for helping out! very good answers :)

Before I saw your answers, I tried the following,

output = subprocess.Popen(["sudo","-b", "code.sh", "arg1"],
stdout=subprocess.PIPE).communicate()[0]

This seemed to push the shell execution process to the background and
because my python program was invoked initially with sudo, it seems I
did not need to enter a passwd again.

Any comments about this..any issues that you see will crop up?

Thanks a ton again.

Is using gksu or kdesu feasible? Or maybe you could run "sudo -v" which
activates sudo then immediately run your "sudo command". This relies on
sudo not configured to not use timestamp though.
 

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
473,755
Messages
2,569,536
Members
45,012
Latest member
RoxanneDzm

Latest Threads

Top