How to execute linux command in django view

2,700 views
Skip to first unread message

Kashif Azeem

unread,
Dec 11, 2009, 11:28:49 AM12/11/09
to django...@googlegroups.com
Hello,

Can anyone guide me please how to execute Linux command (e.g fdisk) in django view?. I have tried os.system() and commands module of python but the browser goes to busy/loading state and never finishes loading the page for quite a while. How to handle this please?

--
Kashif

Shawn Milochik

unread,
Dec 11, 2009, 12:07:32 PM12/11/09
to django...@googlegroups.com
The proper way to do that is to use the subprocess module.

http://docs.python.org/library/subprocess.html

If you don't want to do much reading, you'll just want to look at section 18.1.3.1, which gives the way to call a simple shell command the way you'd do with backticks in Perl.

However, since you mentioned fdisk, maybe you're actually wanting to interact with the shell command. That's more complicated, but can be done with the "communicate" method of a subprocess.

Shawn


Kashif Azeem

unread,
Dec 11, 2009, 1:40:41 PM12/11/09
to django...@googlegroups.com
Thank you Shawn for the reply now i have another problem i.e. on executing some commands like 'vgs' or 'lvs' it didnt executed and the warning generated as "Running as a non-root user. Functionality may be unavailable". Any idea how to solve this user issue please?

--
Kashif




--

You received this message because you are subscribed to the Google Groups "Django users" group.
To post to this group, send email to django...@googlegroups.com.
To unsubscribe from this group, send email to django-users...@googlegroups.com.
For more options, visit this group at http://groups.google.com/group/django-users?hl=en.





--
-Kashif

Preston Holmes

unread,
Dec 11, 2009, 2:32:16 PM12/11/09
to Django users


On Dec 11, 10:40 am, Kashif Azeem <kashi.az...@gmail.com> wrote:
> Thank you Shawn for the reply now i have another problem i.e. on executing
> some commands like 'vgs' or 'lvs' it didnt executed and the warning
> generated as "Running as a non-root user. Functionality may be unavailable".
> Any idea how to solve this user issue please?

Any subprocess started from django will run as the user of the
webserving process.

mod_wsgi lets you run in daemon mode as a specified user but you would
never run a web process as root.

"Daemon processes may if required also be run as a distinct user
ensuring that WSGI applications cannot interfere with each other or
access information they shouldn't be able to."

If you need to run a set number of commands, you'll have to edit your
sudoers file to allow those commands (out of this list's scope)

Depending on your project, you may want to run django behind a
cherrypy wsgi server that you can run as a user created for the
project.

-Preston

>
> --
> Kashif
>
>
>
>
>
> On Fri, Dec 11, 2009 at 10:07 PM, Shawn Milochik <sh...@milochik.com> wrote:
> > The proper way to do that is to use the subprocess module.
>
> >http://docs.python.org/library/subprocess.html
>
> > If you don't want to do much reading, you'll just want to look at section
> > 18.1.3.1, which gives the way to call a simple shell command the way you'd
> > do with backticks in Perl.
>
> > However, since you mentioned fdisk, maybe you're actually wanting to
> > interact with the shell command. That's more complicated, but can be done
> > with the "communicate" method of a subprocess.
>
> > Shawn
>
> > --
>
> > You received this message because you are subscribed to the Google Groups
> > "Django users" group.
> > To post to this group, send email to django...@googlegroups.com.
> > To unsubscribe from this group, send email to
> > django-users...@googlegroups.com<django-users%2Bunsubscribe@google groups.com>
> > .

Kashif Azeem

unread,
Dec 11, 2009, 3:51:57 PM12/11/09
to django...@googlegroups.com
Thank you Preston for the clarification.
I am thinking to handle this by just making a python daemon process that will be started as root through shell and this daemon will execute all required shell commands. Daemon will check the actions queue (that can be slq table) and will perform the posted action. And this queue will be filled by django views and vice versa.

Anyhow thank you for your response.

--
Kashif

To unsubscribe from this group, send email to django-users...@googlegroups.com.

For more options, visit this group at http://groups.google.com/group/django-users?hl=en.





--
-Kashif
Reply all
Reply to author
Forward
0 new messages