LoginSignup
3
1

More than 3 years have passed since last update.

【番外編】CentOSにPythonの機械学習開発環境を構築する(systemdでのJupyter Notebookの自動起動編)

Last updated at Posted at 2019-10-21

前書き

【第一回】CentOSにPythonの機械学習開発環境を構築する(Anacondaインストール編)
【第二回】CentOSにPythonの機械学習開発環境を構築する(Jupyter Notebook編)
【第三回(最終回)】CentOSにPythonの機械学習開発環境を構築する(Tensorflow + Kerasインストール編)
で、機械学習の開発環境を作成しました。
今回の番外編で、Jupyter Notebooksystemdのサービスとして登録し、systemctlコマンドで起動、停止、及び自動起動をさせる手順を紹介したいと思います。

環境

  • OS:CentOS Linux release 7.7.1908

前提条件

  • CentOSAnaconda3Anaconda3-2019.07-Linux-x86_64.sh)がインストールされていること
  • 機械学習用の環境(ml_env)が作成されていること

「【第一回】CentOSにPythonの機械学習開発環境を構築する(Anacondaインストール編)」で紹介)

  • Jupyter Notebookを使用可能な設定が完了していること

「【第二回】CentOSにPythonの機械学習開発環境を構築する(Jupyter Notebook編)」で紹介)

手順

1. Jupyterのパスを確認

anacondaユーザーでログインまたはスイッチします。
su - anaconda

コマンド実行結果
[root@CENTOS7 ~]# su - anaconda
最終ログイン: 2019/10/21 (月) 18:24:38 JST日時 pts/0
[anaconda@CENTOS7 ~]$

環境設定ファイル.anaconda.envを読み込みます。
source ./.anaconda.env

コマンド実行結果
[anaconda@CENTOS7 ~]$ source ./.anaconda.env
[anaconda@CENTOS7 ~]$

機械学習用環境ml_envをアクティブにします。
conda activate ml_env

コマンド実行結果
[anaconda@CENTOS7 ~]$ conda activate ml_env
(ml_env) [anaconda@CENTOS7 ~]$

Jupyterの実行ファイルのパスを確認します。
which jupyter

コマンド実行結果
(ml_env) [anaconda@CENTOS7 ~]$ which jupyter
~/anaconda3/envs/ml_env/bin/jupyter
(ml_env) [anaconda@CENTOS7 ~]$

ml_envjupyterの実行ファイルのパスが/home/anaconda//anaconda3/envs/ml_env/bin/jupyterであることがわかります。

2. Unit定義ファイルの作成

rootユーザーにスイッチします。
su -

コマンド実行結果
(ml_env) [anaconda@CENTOS7 ~]$ su -
パスワード:
最終ログイン: 2019/10/21 (月) 18:43:05 JST 192.168.0.2から開始日時 pts/0
[root@CENTOS7 ~]#

/etc/systemd/system/配下にjupyter-notebook.serviceファイルを作成します。

/etc/systemd/system/jupyter-notebook.service
[Unit]
Description=Jupyter Notebook

[Service]
User=anaconda
Group=anaconda
Type=simple
PIDFile=/var/run/jupyter-notebook.pid
ExecStart=/home/anaconda/anaconda3/envs/ml_env/bin/jupyter notebook
WorkingDirectory=/home/anaconda/work
Restart=always

[Install]
WantedBy = multi-user.target
  • User:Jupyter(Anaconda3)のユーザー名を指定
  • Group:Jupyter(Anaconda3)のグループ名を指定
  • ExecStart:ml_envでのjupyter notebookコマンドのフルパスを指定
  • WorkingDirectory:jupyter notebookのワーキングディレクトリを指定

3. systemdの登録確認

Unit定義ファイルが登録されていることを確認します。
systemctl list-unit-files --type=service | grep jupyter-notebook

コマンド実行結果
[root@CENTOS7 ~]# systemctl list-unit-files --type=service | grep jupyter-notebook
jupyter-notebook.service                      disabled
[root@CENTOS7 ~]#

4. jupyter-notebookの自動起動設定

jupyter-notebookのステータスをenableにします。
systemctl enable jupyter-notebook

コマンド実行結果
[root@CENTOS7 ~]# systemctl enable jupyter-notebook
Created symlink from /etc/systemd/system/multi-user.target.wants/jupyter-notebook.service to /etc/systemd/system/jupyter-notebook.service.
[root@CENTOS7 ~]#

jupyter-notebookのステータスがenableになっていることを確認します。
systemctl list-unit-files --type=service | grep jupyter-notebook

コマンド実行結果
[root@CENTOS7 ~]# systemctl list-unit-files --type=service | grep jupyter-notebook
jupyter-notebook.service                      enabled
[root@CENTOS7 ~]#

5. systemctlコマンドでjupyter-notebookを起動

systemctlコマンドでjupyter-notebookを起動します。
systemctl start jupyter-notebook

コマンド実行結果
[root@CENTOS7 ~]# systemctl start jupyter-notebook
[root@CENTOS7 ~]#

jupyter-notebookが起動していることを確認します。
ps -ef | grep jupyter-notebook

コマンド実行結果
[root@CENTOS7 ~]# ps -ef | grep jupyter-notebook
anaconda  1442     1  0 19:17 ?        00:00:00 /home/anaconda/anaconda3/envs/ml_env/bin/python /home/anaconda/anaconda3/envs/ml_env/bin/jupyter-notebook
root      1459  1370  0 19:21 pts/0    00:00:00 grep --color=auto jupyter-notebook
[root@CENTOS7 ~]#

6. systemctlコマンドでjupyter-notebookを停止

systemctlコマンドでjupyter-notebookを停止します。
systemctl stop jupyter-notebook

コマンド実行結果
[root@CENTOS7 ~]# systemctl stop jupyter-notebook
[root@CENTOS7 ~]#```

`jupyter-notebook`が停止していることを確認します。
`ps -ef | grep jupyter-notebook`

```console:コマンド実行結果
[root@CENTOS7 ~]# ps -ef | grep jupyter-notebook
root      1469  1370  0 19:24 pts/0    00:00:00 grep --color=auto jupyter-notebook
[root@CENTOS7 ~]#

7. OSを再起動後、jupyter-notebookが起動していることを確認

OSを再起動します。
shutdown -r now

コマンド実行結果
[root@CENTOS7 ~]# shutdown -r now

再起動後、jupyter-notebookが起動していることを確認します。
ps -ef | grep jupyter-notebook

コマンド実行結果
[root@CENTOS7 ~]# ps -ef | grep jupyter-notebook
anaconda   674     1  5 19:27 ?        00:00:02 /home/anaconda/anaconda3/envs/ml_env/bin/python /home/anaconda/anaconda3/envs/ml_env/bin/jupyter-notebook
root      1314  1294  0 19:28 pts/0    00:00:00 grep --color=auto jupyter-notebook
[root@CENTOS7 ~]#

以上です。

参考サイト

10.6. SYSTEMD のユニットファイルの作成および変更

リンク

【第一回】CentOSにPythonの機械学習開発環境を構築する(Anacondaインストール編)
【第二回】CentOSにPythonの機械学習開発環境を構築する(Jupyter Notebook編)
【第三回(最終回)】CentOSにPythonの機械学習開発環境を構築する(Tensorflow + Kerasインストール編)

3
1
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
3
1