怎麼讓 jupyter 當重開機後透過 systemd 自動帶起來?

透過  anaconda 安裝的 Jupyter notebook 實在很懶的每次要用時就要切換用戶再去帶起來,所以找了一下怎麼來自動帶起來,其實很簡單.

先在你的 systemd(可以是在 /etc/systemd/system or /usr/lib/systemd/system)下建立一個 jupyter.service 檔案 並加入下列內容,其中 user/group 要是你用來啟動 jupyter notebook 的使用者,那個 config 可以透過命令去産生

[Unit]
Description=Jupyter Workplace

[Service]
Type=simple
PIDFile=/run/jupyter.pid
ExecStart=/home/jerryw/anaconda3/bin/jupyter-notebook --config=/home/jerryw/.jupyter/jupyter_notebook_config.py
User=jerryw
Group=jerryw
WorkingDirectory=/home/jerryw/MyNotebook
Restart=always
RestartSec=10

[Install]
WantedBy=multi-user.target

透過命令去産生 config

$ jupyter notebook --generate-config

然後在 config 檔內編輯 “c.NotebookApp.token = ‘<你的密碼>'”

然後用 root 權限啟動 jupyter

# systemctl enable jupyter.service 
# systemctl daemon-reload
# systemctl restart jupyter.service

因為我有順便透過 nginx 做反向代理,所以順便也將 nginx 一起設置一下.

server {
    server_name jupyter.example.com 192.168.31.10;
    listen 80;
    listen 443 ssl;
    ssl_certificate /etc/pki/tls/certs/cert.pem;
    ssl_certificate_key /etc/pki/tls/private/key.pem;
    ssl_protocols       TLSv1 TLSv1.1 TLSv1.2;
    ssl_ciphers         HIGH:!aNULL:!MD5;

    location ~ ^/(.*) {
        proxy_pass http://127.0.0.1:8888;
        proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
        proxy_set_header X-Real-IP $remote_addr;
        proxy_set_header Host $http_host;
        proxy_http_version 1.1;
        proxy_redirect off;
        proxy_buffering off;
        proxy_set_header Upgrade $http_upgrade;
        proxy_set_header Connection "upgrade";
        proxy_read_timeout 86400;
    }
}

然後 啟動 nginx

# systemctl enable nginx
# systemctl start nginx

完成~

Author: jerryw1974

learning and focus on computer science, cloud infrastructure, virtualization and information security, technical, networking,platform system and cyber-security related topic.