情景说明:
树莓派上已经搭建好了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进程被关闭,需要重新启动
后台服务实现
- 在
/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
- 使服务自启动:
systemctl enable jupyter
- 启动服务
service jupyter start