それマグで!

知識はカップより、マグでゆっくり頂きます。 takuya_1stのブログ

習慣に早くから配慮した者は、 おそらく人生の実りも大きい。

systemdのジョブの起動前、起動後に処理を記述する preExec / PostExec

systemd で自動起動するファイルを記述したが pip のアップデートで苦労する

systemd でdaemon を起動しているのですが、pip 関連でアップデートが頻繁にあるモジュールだと、前もって更新してあげないとエラーになることがる。

systemd で事前処理・事後処理を書けないか調べてみたらあった

ExecStartPre を使うと、起動前に実行してくれるようだ。

ExecStartPre=, ExecStartPost= Additional commands that are executed before or after the command in ExecStart=, respectively. Syntax is the same as for ExecStart=, except that multiple command lines are allowed and the commands are executed one after the other, serially.

複数行の処理は?

ExecStartPreを複数書けばいいらしい

書いてみた。

pipenv に入れた pip や requirements.txt をDaemonの起動前に、更新をするように書きました。

[Unit]
Description=Splattoon splatnet2ink

[Service]
WorkingDirectory=/PATH/TO/project
ExecStartPre=/PATH/bin/python /PATH/TO/pipenv/bin/pip install --upgrade pip 
ExecStartPre=/PATH/TO/pipenv/bin/python /PATH/TO/pipenv/bin/pip install --upgrade -r  requirements.txt
ExecStart=/PATH/TO/pipenv//bin/python /PATH/TO/pipenv/daemon.py -M

[Install]
WantedBy=default.target

これで更新処理を別途書く必要がなくなる。

これまでは、ライブラリ更新処理を、別途 timer にしてたり、更新後再起動をタイマーにしてたのですが、不便でした。

これで、PCを起動時、ユーザー systemd 空間なら、ログイン時に更新されるはず。だから timer 関連のsystemd ファイルが減って管理が楽になりそうです。

参考資料

https://www.freedesktop.org/software/systemd/man/systemd.service.html