树莓派 3b jupyter notebook 设置为后台服务

情景说明:
树莓派上已经搭建好了jupyter,命令jupyter notebook可以启动,在局域网/外网都可以通过浏览器访问.在终端内运行jupyter notebook会一直占据命令行.
后台实现:jupyter notebook > output.file 2&>1 & 这样可以实现把输出到命令行的信息输出到output.file ps:output.file 是自己新建的一个文件.
这里也有一个问题:当关闭终端或者远程连接的session,任务就会终止
关闭会话不关闭任务实现:

screen -s jupyter #创建一个jupyter窗口 	
jupyter-notebook  #运行jupyter-notebook 或者是jupyter notebook > output.file 2&>1 &

这里的问题是:断电重启之后原来的jupyter进程被关闭,需要重新启动

后台服务实现

  1. /etc/systemd/system 下创建jupyter.service输入如下内容
[Unit]
Description=Jupyter Notebook
After=network.target

[Service]
Type=simple
PIDFile=/run/jupyter.pid #这里在/run/目录下没有jupyter.pid,搜索相关信息发现,这个是进程产生之后出现的,虽然在启动前没有,但是可以使用.
ExecStart=/home/pi/.local/bin/jupyter-notebook --config=/home/pi/.jupyter/jupyter_notebook_config.py 
#ExecStart 是执行文件 --config是配置文件,之前我已经配置过jupyter_notebook_config.py,有的教程是配置.json文件(没试过)
User=pi
Group=pi #查看用户组 groups pi,发现属于pi组
WorkingDirectory=/home/pi/Desktop/jupyter_project #自己设置的工作目录,需要同时在jupyter_notebook_config.py中设置c.NotebookApp.notebook_dir = '/home/pi/Desktop/jupyter_project' 配置自己的工作目录
Restart=always
RestartSec=10

[Install]
WantedBy=multi-user.target
  1. 使服务自启动:systemctl enable jupyter
  2. 启动服务 service jupyter start

引用自链接搭建 Jupyter Notebook 服务器

猜你喜欢

转载自blog.csdn.net/q361949240/article/details/83179560
今日推荐