How to run jupyter notebook in the background ? No need to keep one terminal for it

后端 未结 11 2224
有刺的猬
有刺的猬 2020-12-02 10:17

Often we run jupyter notebook to pop up a page in browser to use notebook. However, the terminal opening the server remains there. Is there a way that we can cl

11条回答
  •  南方客
    南方客 (楼主)
    2020-12-02 10:41

    Not real sophisticated but it gets the job done:

    #! /bin/bash
    
    #probably should change this to a case switch
    
    if [ "$1" == "end" ]
        then
            echo
            echo "Shutting Down jupyter-notebook"
            killall jupyter-notebook
            echo
          exit 0
     fi
    
    if [ "$1" == "-h" ]
       then
            echo   
            echo "To start  : jnote  [default 8888]" 
            echo "To end    : jnote end"
            echo "This help : jnote -h"
            echo
          exit 0
     fi
    
    #cast from string
    PORT=$(($1))
    
    RETURN=0
    PID=0
    
    if [ "$PORT" == "0" ] || [ "$PORT" == "" ]; then PORT=8888; fi
    
    echo
    echo "Starting jupyter-notebook"
    
    #background and headless, set port, allow colab access, capture log, don't open browser yet
    
    nohup jupyter notebook \
     --NotebookApp.allow_origin='https://colab.research.google.com' \
     --port=$PORT --NotebookApp.port_retries=0 \
     --no-browser >~/jnote.log 2>&1 &
    
    RETURN=$?
    PID=$!
    
    #Wait for bg process to complete - add as needed
    sleep 2
    
    if [ $RETURN == 0 ]
       then
            echo
            echo "Jupyter started on port $PORT and pid $PID"
            echo "Goto `cat ~/jnote.log | grep localhost: | grep -v NotebookApp`"
            echo
          exit 0
       else
            echo 
            echo "Jupyter failed to start on port $PORT and pid $PID with return code $RETURN"
            echo "see ~/jnote.log"
            echo
          exit $RETURN
     fi
    

提交回复
热议问题