4

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
AncientSwordRage
  • 7,086
  • 19
  • 90
  • 173
  • Can I just check these updates are from people with a similar problem? This sounds like it should have a simple solution - no lots of upvotes? – AncientSwordRage May 14 '15 at 15:16

1 Answers1

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 &
ar-ms
  • 735
  • 6
  • 14
  • Neither of these (in this example) pass control back to the script. I've used all three methods you've mentioned elsewhere I an know they *normally* work. In the `disown` case when I killed the process for `./manage runserver` is output: `./start_gui.sh: line 21: 4932 Terminated python ./manage.py runderver --settings=project.settings.$SETTING_ENV < /dev/null` – AncientSwordRage May 18 '15 at 08:14