【问题标题】:Run Jupyter-notebook on boot on Ubuntu在 Ubuntu 上启动时运行 Jupyter-notebook
【发布时间】:2017-10-29 03:08:52
【问题描述】:

我有一个安装了 anaconda 的 Ubuntu 16.04 虚拟机, 我希望它在启动时使用正确的配置文件(IP 地址、端口、密码……)启动 Jupyter-notebook

此配置在 /home/user/.jupyter/jupyter_notebook_config.py 中指定

当我以 user 身份登录并在主目录 (/home/user/) 中时,它会启动正确的配置文件。

但是当使用命令时

jupyter-notebook

在使用 rc.local 或使用 crontab 启动期间,它不会加载我的配置文件,并且没有正确的运行目录。

【问题讨论】:

    标签: ubuntu server jupyter-notebook startup boot


    【解决方案1】:

    非常相似的问答:How to start ipython notebook server at boot as daemon

    您可以将以下行添加到您的 /etc/rc.local 文件中

    su <username> -c "jupyter notebook --config=/location/of/your/config/file/.jupyter/jupyter_notebook_config.py --no-browser --notebook-dir=/location/of/yournotebooks" &
    

    例如

    su simon -c "jupyter notebook --config=/home/simon/.jupyter/jupyter_notebook_config.py --no-browser --notebook-dir=/home/simon/notebooks" &
    

    su &lt;username&gt; -c 确保笔记本不是以 root 身份执行,而是使用指定的用户帐户执行。``

    --config--notebook-dir 指定配置文件和笔记本文件夹的位置 (http://jupyter-notebook.readthedocs.io/en/latest/config.html)


    对于使用 systemd(Ubuntu 16 及更高版本)的系统,以下方法也适用:

    • /etc/systemd/system/ 中创建一个服务文件,例如jupyter.service 包含以下内容(将YourUserName 替换为您的用户名)

      [Unit]
      After=network.service
      
      [Service]
      ExecStart=jupyter notebook
      User=YourUserName
      
      [Install]
      WantedBy=default.target
      
    • 使用sudo systemctl enable jupyter.service启用服务

    • sudo systemctl start jupyter.service启动服务

    您应该为 Jupyter 服务器设置密码,因为您将无法访问令牌。

    【讨论】:

    • @CarlosSouza 你能把更多细节作为一个单独的问题发布吗?
    • 当然:我完全按照你的描述做了: - 创建了一个 /etc/rc.local - 包括你在 Ubuntu 18.04、EC2 实例上显示的行。什么都没有发生。
    • @CarlosSouza:最好打开一个新问题或在聊天中讨论它:chat.stackoverflow.com/rooms/205822/…
    • @CarlosSouza: 你能尝试通过执行你的/etc/rc.local 文件并检查是否有任何错误消息吗?
    • @CésarArquero:刚刚安装了 Ubuntu 20.04 并更新了答案。
    【解决方案2】:

    就我而言,在 ubuntu 20.04 中,“/etc/rc.local”方式不起作用。我分两步解决了:(1)创建可执行文件,只需双击或输入即可; (2) 在 gnome 中添加启动应用程序的执行。

    这里是详细的:

    1. 在您想要的位置创建文件并添加可执行选项。这是在 USER 文件夹中创建文件的一行代码:cd /home/USER &amp; touch jupyterlab.sh &amp; sudo chmod u+x jupyterlab.sh

    2. 将相应的执行脚本添加到文件中。在我的情况下,我正在运行 jupyter-lab(使用which jupyter-lab 定位程序),使用 ip 和端口作为服务器。

    这是文件包含的内容:

    #!/bin/bash
    /home/USER/anaconda3/bin/jupyter-lab --ip 192.168.1.32 --port 9000 --no-browser & exit
    
    1. (可选)也可以通过双击使文件可执行,然后在 dolphin (like here) 中输入 goint to preferences。

    2. 将文件和 .sh 扩展名添加到启动应用程序中。

    可能看起来很长,但它具有获得可执行文件的优点,只需单击几下即可初始化(或不初始化)。

    【讨论】:

    • 在我的例子中,启动脚本来启动 jupyter 需要一点时间,但是这个解决方案真的很有效。我猜延迟时间取决于计算机。该解决方案易于执行,重要的是不涉及使用sudo 权限。所以不太可能因为一个愚蠢的错误而搞砸系统。为了使脚本可执行chmod +x jupyterlab.sh 也可以。
    • 我有一些建议可以改进答案,但无法添加,因为“编辑队列已满”。您能否通过此链接添加它们:pastebin.com/QcPfVcKG
    【解决方案3】:

    这个对我有用。把它放在你的/etc/rc.local

    sudo -u <username> nohup /home/<username>/.local/bin/jupyter-notebook --ip 0.0.0.0 --port 8888 --no-browser --notebook-dir=/home/<username>/<notebook_dir>&
    

    在我的情况下,/etc/rc.local 第一次是不可用的,所以你需要先创建这个。然后,使其可执行。

    sudo chmod +x /etc/rc.local
    

    这是我rc.local的内容样子。

    #!/bin/sh -e
    #
    # rc.local
    #
    # This script is executed at the end of each multiuser runlevel.
    # Make sure that the script will "exit 0" on success or any other
    # value on error.
    #
    # In order to enable or disable this script just change the execution
    # bits.
    #
    # By default this script does nothing.
    
    sudo -u my_username nohup /home/my_username/.local/bin/jupyter-notebook --ip 0.0.0.0 --port 8888 --no-browser --notebook-dir=/home/my_username/notebook&
    
    exit 0
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2019-01-20
      • 2018-09-25
      • 1970-01-01
      相关资源
      最近更新 更多