如果路径包含主目录波浪号〜,则不会执行Python子进程〜 [英] Python subprocess doesn't execute if the path contains home directory tilde ~
问题描述
我正在尝试检测错误并从Django应用程序重新启动服务器。
我正在使用以下代码:
I'm trying to detect an error and restart server from django application. I'm using the following code:
try:
# do something
except:
print('here')
subprocess.call(['/home/my_username/restart.sh'])
restart.sh如下
restart.sh is as follows
#!/bin/sh
/home/my_username/webapps/app/apache2/bin/restart
/home/my_username/webapps/my_db/bin/cron
我正在使用webfaction作为托管服务提供商。
以上代码将打印语句,但不会重新启动服务器,也不会启动位于my_db下的mysql数据库。
I'm using webfaction as hosting provider. Aboved code prints statement, but doesn't restart the server and doesn't start mysql database which is under my_db.
也许我需要提供用户名/通过?
Maybe I need to supply username/pass? How to do that?
推荐答案
subprocess.call
不会将〜
代字号扩展到您的主目录,因此它将在当前工作目录中查找名为〜的文件夹,然后从该目录开始寻找您的应用
The subprocess.call
won't expand the ~
tilde character to your home directory, and therefore it'll look into your current working directory for a folder called ~ and then from there look for your app
您可以使用 os.path.expanduser
函数获取所需的行为,它是会是这样的:
You can use the os.path.expanduser
function to get the desired behaviour, it'll be something like this:
try:
# do something
except:
print('here')
subprocess.call([os.path.expanduser('~/webapps/app/apache2/bin/restart')])
此脚本将查找 / home / user / webapps / app / apache2 / bin / restart
并执行
祝你好运;)
这篇关于如果路径包含主目录波浪号〜,则不会执行Python子进程〜的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!