How o run shell command in Django ?

483 views
Skip to first unread message

Asad ur Rehman

unread,
Aug 8, 2016, 5:33:12 AM8/8/16
to Django users
I want to run fs_cli -x command in django . Can anybody help me ?
How can i create function in django to execute this command ?

Matthias Müller

unread,
Aug 8, 2016, 5:57:01 AM8/8/16
to django...@googlegroups.com
Hi Asad,

run system calls in django means run system calls in python. Python has a library called "os".
See 16.1.6 process management

HTH
--
Viele Grüße / Best Regards
Matthias Müller


2016-08-08 11:33 GMT+02:00 Asad ur Rehman <asadurr...@gmail.com>:
I want to run fs_cli -x command in django . Can anybody help me ?
How can i create function in django to execute this command ?

--
You received this message because you are subscribed to the Google Groups "Django users" group.
To unsubscribe from this group and stop receiving emails from it, send an email to django-users+unsubscribe@googlegroups.com.
To post to this group, send email to django...@googlegroups.com.
Visit this group at https://groups.google.com/group/django-users.
To view this discussion on the web visit https://groups.google.com/d/msgid/django-users/0f9ea28d-37c9-40f2-b67a-b566c3691998%40googlegroups.com.
For more options, visit https://groups.google.com/d/optout.

Erik Cederstrand

unread,
Aug 8, 2016, 5:58:17 AM8/8/16
to Django Users
I've had a lot of success interacting with the command-line by using the 'sh' module: http://amoffat.github.io/sh/ It's extremely simple but allows a lot of advanced features when you need them.

I'm not on a system where fs_cli is avaliable, but you should be able to just do:

from sh import fs_cli
result = fs_cli('-x')


And put that somewhere useful in your Django code, e.g. in a view or a management command.


Erik

> Den 8. aug. 2016 kl. 11.33 skrev Asad ur Rehman <asadurr...@gmail.com>:
>
> I want to run fs_cli -x command in django . Can anybody help me ?
> How can i create function in django to execute this command ?
>
> --
> You received this message because you are subscribed to the Google Groups "Django users" group.
> To unsubscribe from this group and stop receiving emails from it, send an email to django-users...@googlegroups.com.

M Hashmi

unread,
Aug 8, 2016, 6:10:37 AM8/8/16
to django...@googlegroups.com
There are better ways to cut short your time to execute or even schedule tasks or commands.

It won't take you more than an hour to understand Django-celery for scheduling tasks. You need to create a bash script and it should include all the steps in case you need virtualenv loaded while command runs. This way you can schedule tasks for multiple django projects on same machine.

mybashscript.sh:

cd /var/www/website/
source myenv/bin/activate
/var/www/website/manage.py $1 --settings$2

Then in your contrab for cronjobs you can set it up like:

SETTINGSMODULE=yourwebsite.settings_production
5 * * * * /var/www/yourwebsite/mybashscript.sh fs_cli -x $SETTINGSMODULE >> /dev/null

Each time your script runs you will get notified by email about the schedule process. 

Otherwise if you still want to get it executed by the python script. I would suggest you to create a script.py at place of your settings.py and import timezone. Then you can write the way or when you want to get it executed by shell. You can search the best and simplest scheduling script that fits your need.

Hope answer was helpful.

Regards,
Mudassar

On Mon, Aug 8, 2016 at 2:33 AM, Asad ur Rehman <asadurr...@gmail.com> wrote:
I want to run fs_cli -x command in django . Can anybody help me ?
How can i create function in django to execute this command ?

--
You received this message because you are subscribed to the Google Groups "Django users" group.
To unsubscribe from this group and stop receiving emails from it, send an email to django-users+unsubscribe@googlegroups.com.

Asad ur Rehman

unread,
Aug 8, 2016, 6:53:19 AM8/8/16
to Django users
#Mattias muller there is error while i am import sh ... How to sort it ?

Asad ur Rehman

unread,
Aug 8, 2016, 6:54:39 AM8/8/16
to Django users
 from sh import fs_cli 
this is also not working ...




On Monday, August 8, 2016 at 2:33:12 PM UTC+5, Asad ur Rehman wrote:

Erik Cederstrand

unread,
Aug 8, 2016, 7:04:12 AM8/8/16
to Django Users

> Den 8. aug. 2016 kl. 12.54 skrev Asad ur Rehman <asadurr...@gmail.com>:
>
> from sh import fs_cli
> this is also not working ...

What's the error message? Did you install the 'sh' module (pip install sh)?

Erik

M Hashmi

unread,
Aug 8, 2016, 7:04:32 AM8/8/16
to django...@googlegroups.com
Install sh with pip.

--
You received this message because you are subscribed to the Google Groups "Django users" group.
To unsubscribe from this group and stop receiving emails from it, send an email to django-users+unsubscribe@googlegroups.com.
To post to this group, send email to django...@googlegroups.com.
Visit this group at https://groups.google.com/group/django-users.

Asad ur Rehman

unread,
Aug 8, 2016, 7:14:00 AM8/8/16
to Django users
sh is already installed


On Monday, August 8, 2016 at 2:33:12 PM UTC+5, Asad ur Rehman wrote:

Asad ur Rehman

unread,
Aug 8, 2016, 7:14:22 AM8/8/16
to Django users
Internal server error is coming..


On Monday, August 8, 2016 at 2:33:12 PM UTC+5, Asad ur Rehman wrote:

M Hashmi

unread,
Aug 8, 2016, 7:20:25 AM8/8/16
to django...@googlegroups.com
Send error.

--
You received this message because you are subscribed to the Google Groups "Django users" group.
To unsubscribe from this group and stop receiving emails from it, send an email to django-users+unsubscribe@googlegroups.com.
To post to this group, send email to django...@googlegroups.com.
Visit this group at https://groups.google.com/group/django-users.

Asad ur Rehman

unread,
Aug 8, 2016, 7:27:03 AM8/8/16
to Django users


On Monday, August 8, 2016 at 2:33:12 PM UTC+5, Asad ur Rehman wrote:
error.PNG

Erik Cederstrand

unread,
Aug 8, 2016, 7:29:18 AM8/8/16
to Django Users

> Den 8. aug. 2016 kl. 13.14 skrev Asad ur Rehman <asadurr...@gmail.com>:
>
> Internal server error is coming..

If you want help, please be much more specific in your replies; post the relevant parts of the code, stacktraces, what have you tried already etc.

I assume you already added the code to your Django project and "Internal Server Error" is what you see in the browser. You need to set DEBUG=True in settings.py so you get better error reports than just an HTTP 500.

But start by verifying that the code snippet I sent previously actually works. Start a Python interpreter from the commandline (make sure you're in the same virtualenv as the server running your Django code) and do:

>>> from sh import fs_cli

If that works, we need a full stacktrace from Django to see what's going on.

Erik

Asad ur Rehman

unread,
Aug 8, 2016, 7:42:57 AM8/8/16
to Django users
tail -f /var/log/newfies/gunicorn_newfies_dialer.log


when i try to find error through this always give no module named urls. Debug is already = True

On Monday, August 8, 2016 at 2:33:12 PM UTC+5, Asad ur Rehman wrote:
Capture1.PNG

M Hashmi

unread,
Aug 8, 2016, 7:45:38 AM8/8/16
to django...@googlegroups.com
Send your root urls.py code.

--
You received this message because you are subscribed to the Google Groups "Django users" group.
To unsubscribe from this group and stop receiving emails from it, send an email to django-users+unsubscribe@googlegroups.com.
To post to this group, send email to django...@googlegroups.com.
Visit this group at https://groups.google.com/group/django-users.
Reply all
Reply to author
Forward
0 new messages