jupyter notebook,设置自启动

一、Debian

(base) root@debian:/etc/systemd/system# cat jupyter.service

[Unit]
Description=Jupyter Notebook
After=network.target
[Service]
Type=simple
ExecStart=/root/anaconda3/bin/jupyter-notebook –config=/root/.jupyter/jupyter_notebook_config.py –no-browser

WorkingDirectory=/root/anaconda3/bin/
#文件路径名
Restart=always
RestartSec=10
ExecReload=/bin/kill -HUP $MAINPID
KillMode=process
Restart=on-failure
RestartPreventExitStatus=255
Type=notify
#RuntimeDirectory=sshd
#RuntimeDirectoryMode=0755

 

[Install]
WantedBy=multi-user.target

3、添加权限并设置开机自启chmod +x /etc/jupyter.service

systemctl enable jupyter

systemctl start jupyter.service

检查状态:systemctl status jupyter.service

返回Active:active信息,则成功。

最后我们就可以在/etc/rc.local里,添加开机的自启命令什么的了。记住添加在exit 0之前。

注意:更改自启脚本后,需要重新运行一下命令已使其生效。systemctl enable jupyter

如果有更改服务配置文件test.service,需要执行以下命令

刷新:systemctl daemon-reload

启动:systemctl start jupyter.service

***另启动脚本

###########################自己改的jupyter启动程序 /etc/init.d/jupyter
#!/bin/sh
start()
{
echo “=========================”
echo “Jupyter notebook AutoStart”
echo “=========================”
}

status()
{
echo “jupyter notebook stauts”
systemctl status jupyter.service | grep “Active:”

}
stop()
{
echo “Stopping jupyter notebook”
kill $(ps aux|grep -m 1 ‘jupyter-notebook –allow-root’|awk ‘{ print $2}’)

}

case “$1” in
start)
echo “starting jupyter notebook”
jupyter notebook –allow-root &
;;
stop)
stop
;;
status)
status
;;
restart)
stop
start
;;
*)
echo “Usage: jupyter {start|stop|restart|status}”
;;
esac
exit 0

四、设置自启动

1 update-rc.d jupyter defaults

步骤二(Level 3 启动项后面增加软连接)

1 cd /etc/rc3.d/
2 ln -s ../init.d/jupyter S01jupyter

reboot 重启测试

二、树莓派安装

树莓派安装1> 树莓派原生OS不带pip工具,安装pip管理工具,原生OS上有两个python版本,需指定版本(python3-pip/python2-pip)

 

sudo apgget install python3-pip

2>切换root用户
notes:直接切换会报没权限错误,需先设置root密码,执行下列命令,然后设置密码;

 

sudo passwd root

执行“su 用户名”命令切换用户,后面执行所有的命令都是在Root管理者权限下执行,(在pi下不能实现自启动)

 

su root

安装配置jupyter

1 安装jupyter,国内镜像源下载会更块,-i参数指定 https://pypi.douban.com/simple/, 其中pyzmq-22.0.3-cp37-cp37m-linux_armv7l.whl包下载容易报错,建议电脑本地安装

 

pip3 install -i https://pypi.douban.com/simple/ jupyter

2配置jupyter
安装完成后,执行下列命令

 

  jupyter notebook --generate-config

此时在/root/下会生成一个/.jupyter文件夹

 

root@raspberrypi:~# ls -al
total 56
drwx------  6 root root  4096 Mar  8 07:17 .
drwxr-xr-x 18 root root  4096 Jan 11 05:15 ..
-rw-------  1 root root  3288 Mar  8 07:18 .bash_history
-rw-r--r--  1 root root   570 Dec  9 14:40 .bashrc
drwx------  3 root root  4096 Mar  8 06:14 .cache
drwx------  2 root root  4096 Mar  8 07:14 .jupyter
drwxr-xr-x  3 root root  4096 Mar  8 06:22 .local
-rw-r--r--  1 root root   148 Dec  9 14:40 .profile
-rw-------  1 root root    49 Mar  8 06:29 .python_history
-rw-------  1 root root 13803 Mar  8 07:17 .viminfo
drwx------  3 root root  4096 Mar  8 01:20 .vnc
root@raspberrypi:~# cd .jupyter/
root@raspberrypi:~/.jupyter# ls -al

终端输入,

 

jupyter notebook password

提示输入密码,此处会生成密钥保存json文件中,此处后面会用到

 

root@raspberrypi:~/.jupyter# ls -al
total 60
drwx------ 2 root root  4096 Mar  8 07:14 .
drwx------ 6 root root  4096 Mar  8 07:17 ..
-rw------- 1 root root   129 Mar  8 06:50 jupyter_notebook_config.json
-rw-r--r-- 1 root root 48533 Mar  8 07:14 jupyter_notebook_config.py

打开/.jupyter 文件后编辑“jupyter_notebook_config.py”

 

c.NotebookApp.ip='*'                                              //‘*’表示局域网内任意IP都可以访问,
c.NotebookApp.allow_remote_access=True         //表示接受远程连接
c.NotebookApp.password=u'argon2:$argon2id$v=19$m=10240,t=10,p=8$QImcaSHkIZuOzmZwNgduTQ$ZSHvDSq4OZNX8ZonXgnbuA'      //保存在Json文件中的密钥
c.NotebookApp.open_browser=False                   //默认不打开浏览器
c.NotebookApp.port=48888                                  //默认端口
c.NotebookApp.notebook_dir='/home/pi/'             //默认工作目录

设置自启动

此处有多种方法,
方法1>编辑/etc.rc.local 文件,在exit 0 前加入 “jupyter notebook –allow-root”

 

#!/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.
# Print the IP address
_IP=$(hostname -I) || true
if [ "$_IP" ]; then
  printf "My IP address is %s\n" "$_IP"
fi
jupyter notebook --allow-root
exit 0

方法二
在/etc/init.d/文件夹下增加执行文件jupyter.init

 

#!/bin/bash
case "$1" in
     start)
         echo "starting jupyter notebook"
         jupyter notebook --allow-root &
         ;;
     stop)
        echo "stopping jupyter notebook"
        kill $(ps aux|grep -m 1 'jupyter-notebook --allow-root'|awk '{ print $2}')
        ;;
     *)
        echo "Usage: service jupyter notebook start|stop"
        exit 1
        ;;
esac
exit 0

添加执行权限

chmod +x /etc/init.d/jupyter.init

重启设备

#运行jupyter.init 文件
sudo service jupyter.init start
#终止jupyter.init 文件
sudo service jupyter.init stop

设置开机启动

sudo update-rc.d jupyter.init defaults

设置好后重启设备

三、WINDOWS脚本启动 jupyter notebok

在桌面新建文本文件 “jupyter.bat” (以bat结尾就行),输入以下代码,倒数第二行的D:\Jupyter_workspace修改为你想要作为根目录的路径

%隐藏cmd窗口%
@echo off
if “%1″==”h” goto begin
start mshta vbscript:createobject(“wscript.shell”).run(“””%~nx0″” h”,0)(window.close)&&exit
:begin
cd /d D:\Jupyter_workspace
jupyter notebook
双击该文件即可启动jupyter notebok,并且不会弹出命令行
————————————————

脚本关闭 jupyter notebok
新建文本文件 “Kill Jupyter.bat” (以bat结尾就行),输入以下代码:

%隐藏cmd窗口%
@echo off
if “%1″==”h” goto begin
start mshta vbscript:createobject(“wscript.shell”).run(“””%~nx0″” h”,0)(window.close)&&exit
:begin
taskkill /f /im jupyter-notebook.exe
双击该文件即可关闭 jupyter notebok
————————————————