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

后端 未结 11 2189
有刺的猬
有刺的猬 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:38

    You can put the process into the background by using jupyter notebook &. You can close the terminal afterwards and the process will still be running.

    0 讨论(0)
  • Detach Jupyter process from the controlling terminal and send all its input and output data to /dev/null which is a special device file that writes-off any data written to it.

    jupyter notebook </dev/null &>/dev/null &
    

    Lazy people like me would prefer to edit ~/.bash_aliases and create an alias:

    alias jnote='jupyter notebook </dev/null &>/dev/null &'
    

    Reference: https://www.tecmint.com/run-linux-command-process-in-background-detach-process/

    0 讨论(0)
  • 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 <port> [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
    
    0 讨论(0)
  • 2020-12-02 10:42
    jupyter notebook & >> disown  
    

    put the process into the background by using jupyter notebook &
    then type disown or disown <the process id>
    you can close the terminal now

    0 讨论(0)
  • 2020-12-02 10:44

    This works for me when running a jupyter notebook server in the background.

    $> nohup jupyter notebook --allow-root > error.log &

    Stop the nohup jupyter notebook is simple.

    First, find the pid of jupyter:

    $> ps -ef| grep jupyter

    e.g output like:

    root 11417 2897 2 16:00 pts/0 00:04:29 /path/to/jupyter-notebook

    Then kill the process:

    $> kill -9 11417

    You can also simplify this by storing the pid with:

    $> nohup jupyter notebook --allow-root > error.log & echo $!> pid.txt

    i.e, you can stop the notebook with:

    $> kill -9 $(cat pid.txt)


    An alternative way to stop the jupyter notebook is quit from the notebook page.

    0 讨论(0)
提交回复
热议问题