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

心已入冬 提交于 2019-12-10 16:55:02

问题


I'm writing a 'simple' script to start up the django server, background it then start up firefox while I dev.

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.

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

回答1:


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 :

python ./manage.py runserver --settings=project.settings.$SETTING_ENV < /dev/null & 
disown

-

nohup python ./manage.py runserver --settings=project.settings.$SETTING_ENV < /dev/null &


来源:https://stackoverflow.com/questions/30237896/allow-commands-to-run-after-backgrounding-django-runserver-command-in-a-bash-scr

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!