从运行Django的shell命令 [英] Run a shell command from Django

查看:111
本文介绍了从运行Django的shell命令的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我发展Django的网页(使用Apache服务器)需要调用一个shell命令启用/ dissable一些后台程序。我尝试做

I'm developing a web page in Django (using apache server) that needs to call a shell command to enable/dissable some daemons. I'm try to do it with

使用os.system(服务的httpd重新启动1> $ HOME /出2 - ; $ HOME /错误)

和此命令不返回任何东西。任何想法我怎么能解决这个问题?

and this command doesn't return anything. Any idea how can i fix this?

推荐答案

我将跳过部分,我强烈建议你关于有一个Web应用程序启动和停止的系统进程,并尝试回答这个问题的影响。

I'll skip the part where I strongly advise you about the implications of having a web application starting and stopping system processes and try to answer the question.

您Django应用程序不应该的的用户,这或许应该是需要的启动和停止服务运行。您可以通过大概解决这个问题:

Your django application shouldn't run with root user, which should probably be needed to start and stop services. You can probably overcome this by:


  • 创建一个使用个seteuid
  • 脚本
  • 给该文件中的设置uid属性

该脚本将类似于

#!/usr/bin/python <- or wherever your python interpreter is
import os
os.seteuid(0)
os.system("service httpd restart 1>$HOME/out 2>$HOME/error")

要允许设置有效UID为root(0),你必须运行在一个外壳,以root身份:

To allow setting the effective UID to root (0), you have to run, in a shell, as root:

chown root yourscript.py
chmod u+s yourscript.py
chmod a+x yourscript.py

这应该这样做。在你的Django应用程序,你现在可以调用使用os.system('yourscript.py')用root权限运行该命令。

That should do it. In your Django app you can now call os.system('yourscript.py') to run the command with root permissions.

最后,我相信,你传递给使用os.system()的命令是不是你要找的东西,因为你说说启用和禁用守护程序和你正在做的是重新启动Apache的...这又似乎是在您的Django正在运行,所以在实践中你会杀死你自己的web应用程序。

Finally, I believe that the command you're passing to os.system() isn't what you're looking for, since you talk about enabling and disabling daemons and all you're doing is restarting apache... which in turn seems to be where your django is running, so in practice you'll be killing your own webapp.

这篇关于从运行Django的shell命令的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆