LoginSignup
13
11

More than 5 years have passed since last update.

jupyterをsystemdで自動起動する

Last updated at Posted at 2019-02-26

※ 2019/2/28 ユニットファイルがrootでjupyterを起動する設定になっていたので, "user"グループのユーザ名"user"で起動するよう修正

背景

GCPで構築しており(従量課金なので)使っていないときはサーバを落とているが、OS起動のたびにコンソールに入ってjupyter notebookと打つのが面倒だったので

環境

  • OS: debian 9 stretch
  • ユーザ名"user"でAnacondaを導入済み
  • 環境名は"seminar"

構築手順

1. ユニットファイル作成
まずはユニットファイルの作成から。細かい設定は正直わからない部分もあった... (WantedByってなんだ?)

sudo vim /etc/systemd/system/jupyter.service

ファイル内容は以下の通り

/etc/systemd/system/jupyter.service
[Unit]
Description=Jupyter Notebook (seminar)

[Service]
Type=simple
WorkingDirectory=/home/user
ExecStartPre=. /home/user/anaconda3/etc/profile.d/conda.sh; conda activate seminar
ExecStart=/home/user/anaconda3/envs/seminar/bin/jupyter notebook --config=/home/user/.jupyter/jupyter_notebook_config.py
User=user
Group=user

[Install]
WantedBy=multi-user.target

Anacondaの環境を切り替えるためにExecStartPreを指定しているので、素のjupyterならもっと簡単になると思う。設定ファイル(jupyter_notebook_config.py)を生成していなければオプション(--config)は不要。WorkingDirectoryは指定不要な気もしたけど、後で気づいたのでそのままに。。。

2. 起動確認
正常に動作するかsystemctlで確認してみる。

sudo systemctl start jupyter
sudo systemctl status jupyter

これでエラーっぽいログが出力されず、jupyterにアクセスできればOK

3. 自動起動化
以下のコマンドで自動起動を有効化する。

sudo systemctl enable jupyter

一応OSを再起動してみてjupyterにアクセスできればOK

13
11
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
13
11