允许命令在bash脚本中使django runserver命令后台运行后运行? [英] Allow commands to run after backgrounding django runserver command in a bash script?
问题描述
我正在编写一个简单"脚本来启动django服务器,在我开发时将其后台启动,然后启动Firefox.
I'm writing a 'simple' script to start up the django server, background it then start up firefox while I dev.
我看过 </dev/null &
的技巧和在命令行上工作.但是,当我在脚本中使用它时,它仍然会挂在服务器上.使用 ctrl-z 和bg
命令的背景仅使脚本而不是命令成为背景.
I've seen the </dev/null &
trick and that works on the command line. But when I use it in my script it still hangs on the server starting. Backgrounding with ctrl-z and the bg
command only backgrounds the script and not the command.
有没有一种方法可以向Django传递请勿输入猪"标志?或者,我可以通过将&
放在脚本末尾以外的其他方式在脚本中使其后台运行?还是只是告诉脚本不要在单独的会话中全部运行?
Is there a way I can pass django a 'Don't hog input' flag? Or I can background it inside the script in some way other than putting &
at the end of it? Or just tell the script not to run it all in a separate session?
这是我的完整脚本(很难理解,如果我能使它正常工作,它可能会很美化):
Here's my script in full (it's full on hard-core ugly, it might get prettified if I can get this to work):
SETTING_ENV=$1
if [ "$PWD" = "/home/$USERNAME/PROJECT/" ]; then
pid=$(for pid in $(pidof -x "python"); do ps -p $pid -o pid,cmd --no-heading; done| grep [m]anage|head -1|cut -d" " -f2)
if [ -z $pid ]; then
python ./manage.py runserver --settings=project.settings.$SETTING_ENV < /dev/null &
else
echo "Server still running on : $pid"
fi
pids=$(pidof -x firefox)
for pid in $pids; do
echo $pid
if [ -z $pid ]; then
echo "starting firefox"
firefox --new-window localhost:8000 &
else ecjp "Firefox already running on pid: $pid"
fi
done
else
echo "$PWD is not /home/$USERNAME/PROJECT";
fi
推荐答案
您可以将所有命令放在括号中,如下所示:
You can put all your command in parenthesis like this :
(python ./manage.py runserver --settings=project.settings.$SETTING_ENV < /dev/null &)
当您在括号之间运行命令时,它将创建一个新的子外壳,因此当您退出外壳时,该命令将继续运行.
When you run a command between parentheses it will create a new sub-shell so when you will quit the shell the command will continues to run.
或者您也可以使用disown或nohup:
Or you can also use disown or nohup :
python ./manage.py runserver --settings=project.settings.$SETTING_ENV < /dev/null &
disown
-
nohup python ./manage.py runserver --settings=project.settings.$SETTING_ENV < /dev/null &
这篇关于允许命令在bash脚本中使django runserver命令后台运行后运行?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!