简体   繁体   English

允许命令在bash脚本中的django runserver命令后台运行后运行?

[英]Allow commands to run after backgrounding django runserver command in a bash script?

I'm writing a 'simple' script to start up the django server, background it then start up firefox while I dev. 我正在编写一个“简单”脚本来启动django服务器,在我开发时将其后台启动,然后启动Firefox。

I've seen the </dev/null & trick and that works on the command line. 我已经看过</dev/null &技巧了,并且可以在命令行中使用。 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. 使用ctrl-zbg命令作为背景只会使脚本成为背景,而不会使命令成为背景。

Is there a way I can pass django a 'Don't hog input' flag? 有什么办法可以让Django传递“ Do n't hog input”标志吗? 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. 当您在括号之间运行命令时,它将创建一个新的子外壳程序,因此当您退出外壳程序时,该命令将继续运行。

Or you can also use disown or nohup : 或者,您也可以使用disown或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 &

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM