jupyter-notebook 在centos7下 设置服务开机自启动

jupyter-notebook 在centos7下 设置服务开机自启动_第1张图片

 

在安装好jupyter 并且可以通过命令行启动之后

在opt目录下创建脚本

[root@JupyterNotebook opt]# vim /opt/jupyter.sh

以下是脚本内容

#!/bin/sh
/bin/echo $(/bin/date +%F_%T) >> /home/startup.log
/usr/local/bin/jupyter notebook --allow-root > /home/jupyter_notebook/jupyter/log

# {{ /usr/local/bin/jupyter notebook }} 是jupyter notebook的目录(不同机器可能不同)
# {{ /home/jupyter_notebook/jupyter/log }} 是jupyter notebook的日志输出目录(没有这个目录需要手动创建或者调整到其他目录)

使用which命令可以查看jupyter的目录

[root@JupyterNotebook opt]# which jupyter
/usr/local/bin/jupyter

如果jupyter目录和脚本中的jupyter目录不同需要手动更改

将 ‘/usr/local/bin/jupyter’ 替换为你的juypyter目录

创建好了之后手动尝试运行

cd /opt
./jupyter.sh

如果提示

[root@JupyterNotebook opt]# ./jupyter.sh
-bash: ./jupyter.sh: 权限不够

就执行命令

[root@JupyterNotebook opt]# chmod 777 ./*.sh

再次尝试运行

创建服务

vim /etc/systemd/system/jupyter.service
[Unit]
Description = jupyter.sh
After = network.target

[Service]
Type = simple
ExecStart=/opt/jupyter.sh
ExecReload=/bin/kill -s HUP $MAINPID
RestartSec=2s
Restart=on-failure

[Install]
WantedBy=multi-user.target

如果之前就有创建过jupyter.service在更改完jupyter.service之后需要先执行

systemctl daemon-reload

启动服务

systemctl start  jupyter.service

查看服务状态

systemctl status  jupyter.service
[root@JupyterNotebook opt]# systemctl status jupyter
● jupyter.service - jupyter.sh
   Loaded: loaded (/etc/systemd/system/jupyter.service; disabled; vendor preset: disabled)
   Active: active (running) since 四 2022-07-28 09:10:27 CST; 41s ago
 Main PID: 4941 (jupyter.sh)
    Tasks: 2
   Memory: 47.2M
   CGroup: /system.slice/jupyter.service
           ├─4941 /bin/sh /opt/jupyter.sh
           └─4944 /usr/bin/python3 /usr/local/bin/jupyter-notebook -...

7月 28 09:10:27 JupyterNotebook.lan systemd[1]: jupyter.service hol...
7月 28 09:10:27 JupyterNotebook.lan systemd[1]: Started jupyter.sh.
7月 28 09:10:27 JupyterNotebook.lan systemd[1]: Starting jupyter.sh...
7月 28 09:10:29 JupyterNotebook.lan jupyter.sh[4941]: [I 09:10:29.5…ok
7月 28 09:10:29 JupyterNotebook.lan jupyter.sh[4941]: [I 09:10:29.5...
7月 28 09:10:29 JupyterNotebook.lan jupyter.sh[4941]: [I 09:10:29.5...
7月 28 09:10:29 JupyterNotebook.lan jupyter.sh[4941]: [I 09:10:29.5…。
7月 28 09:11:00 JupyterNotebook.lan jupyter.sh[4941]: [I 09:11:00.4...
Hint: Some lines were ellipsized, use -l to show in full.

设置开机自启

[root@JupyterNotebook opt]# systemctl enable jupyter.service
Created symlink from /etc/systemd/system/multi-user.target.wants/jupyter.service to /etc/systemd/system/jupyter.service.

经测试在开机后可以自动开启jupyter notebook

jupyter-notebook 在centos7下 设置服务开机自启动_第2张图片

 

参考链接

Linux下Jupyter开机启动设置 - 代码先锋网 (codeleading.com)

Linux下报:-bash: ./server.sh: 权限不够_WuGenQiang的博客-CSDN博客_-bash: ./nginx: 权限不够

你可能感兴趣的:(jupyter,python,linux)