Magicode logo
Magicode
0
11 min read

【2022】ユニットファイルを作ってsystemdでサービス化してみる

https://cdn.apollon.ai/media/notebox/eb465e55-cefb-40d9-87e0-aed680769b03.jpeg

1. はじめに

PythonとLinuxのサービスを扱ってみたので,その備忘録です.

2. 準備

2.1. コマンドを作成

▼unit_py.sh
#!/bin/bash

python /home/makilab-share/kali_projects/2006_Unit_sample/unit_py_sample.py
▼unit_py_sample.py
import datetime
import time


for i in range(30):

    dt_now = datetime.datetime.now()
    dt_str = dt_now.strftime('%Y%m%d%H%M%S')
    
    f = open('./tmp/{}.txt'.format(dt_str), 'w')
    f.close()

    time.sleep(2)
権限を変更しておきます.
(base) ┌──(maki㉿makilab)-[/home/makilab-share]
└─$ chmod 755 unit_py.sh

2.2. Unitファイルの作成

(base) ┌──(maki㉿makilab)-[/home/makilab-share]
└─$ sudo emacs /etc/systemd/system/unit_py-test.service
[Unit]
Description=unit_py-test
After=network.service

[Service]
Type=simple
ExecStart=/home/makilab-share/kali_projects/2006_Unit_sample/unit_py.sh
ExecStop=/bin/kill -WINCH ${MAINPID}
Restart=always

[Install]
WantedBy=multi-user.target

3. サービスの実行

3.1. Serviceとして認識されているか確認

(base) ┌──(maki㉿makilab)-[/home/makilab-share]
└─$ systemctl list-unit-files --type=service | grep unit
unit_py-test.service                       disabled        disabled
232 unit files listed.

3.2. serviceを起動させる

状態を確認します.
(base) ┌──(maki㉿makilab)-[/home/makilab-share/kali_projects/2006_Unit_sample]
└─$ sudo systemctl status unit_py-test.service
○ unit_py-test.service - qiita-test
     Loaded: loaded (/etc/systemd/system/unit_py-test.service; disabled; vendor preset: disabled)
     Active: inactive (dead)
サービスを起動します
(base) ┌──(maki㉿makilab)-[/home/makilab-share/kali_projects/2006_Unit_sample]
└─$ sudo systemctl start unit_py-test.service
Activeなら問題ありません.
(base) ┌──(maki㉿makilab)-[/home/makilab-share/kali_projects/2006_Unit_sample]
└─$ sudo systemctl status unit_py-test.service
● unit_py-test.service - unit_py-test
     Loaded: loaded (/etc/systemd/system/unit_py-test.service; disabled; vendor preset: disabled)
     Active: active (running) since Mon 2022-07-11 23:00:01 JST; 3s ago
   Main PID: 5409 (unit_py.sh)
      Tasks: 2 (limit: 2123)
     Memory: 9.1M
        CPU: 65ms
     CGroup: /system.slice/unit_py-test.service
             ├─5409 /bin/bash /home/makilab-share/kali_projects/2006_Unit_sample/unit_py.sh
             └─5410 python /home/makilab-share/kali_projects/2006_Unit_sample/unit_py_sample.py

3.3. プロセスを確認

(base) ┌──(maki㉿makilab)-[/home/makilab-share/kali_projects/2006_Unit_sample]
└─$ ps aux | grep [u]nit
root        5648  0.0  0.1   5056  3204 ?        Ss   23:01   0:00 /bin/bash /home/makilab-share/kali_projects/2006_Unit_sample/unit_py.sh
root        5649  0.2  0.5  14656  9656 ?        S    23:01   0:00 python /home/makilab-share/kali_projects/2006_Unit_sample/unit_py_sample.py

3.4. 自動起動の設定

(base) ┌──(maki㉿makilab)-[/home/makilab-share/kali_projects/2006_Unit_sample]
└─$ sudo systemctl enable unit_py-test.service
Created symlink /etc/systemd/system/multi-user.target.wants/unit_py-test.service → /etc/systemd/system/unit_py-test.service.

3.5. 再起動

(base) ┌──(maki㉿makilab)-[/home/makilab-share/kali_projects/2006_Unit_sample]
└─$ sudo reboot
Connection to makilab closed by remote host.
Connection to makilab closed.

4. 処理の確認

きちんとファイルが増えているので,問題なく自動起動できています.
(base) ┌──(maki㉿makilab)-[/home/makilab-share/kali_projects/2006_Unit_sample]
└─$ ls /tmp/
20220711230001.txt  20220711230015.txt  20220711230029.txt  20220711230043.txt
20220711230003.txt  20220711230017.txt  20220711230031.txt  systemd-private-222f087707304ec595ce3acd1bbb2057-ModemManager.service-lq2Ext
20220711230005.txt  20220711230019.txt  20220711230033.txt  systemd-private-222f087707304ec595ce3acd1bbb2057-haveged.service-N6oXcm
20220711230007.txt  20220711230021.txt  20220711230035.txt  systemd-private-222f087707304ec595ce3acd1bbb2057-systemd-logind.service-7C8aXK
20220711230009.txt  20220711230023.txt  20220711230037.txt  systemd-private-222f087707304ec595ce3acd1bbb2057-systemd-timesyncd.service-30PbZe
20220711230011.txt  20220711230025.txt  20220711230039.txt
20220711230013.txt  20220711230027.txt  20220711230041.txt

(base) ┌──(maki㉿makilab)-[/home/makilab-share/kali_projects/2006_Unit_sample]
└─$ ls /tmp/
20220711230001.txt  20220711230015.txt  20220711230029.txt  20220711230043.txt
20220711230003.txt  20220711230017.txt  20220711230031.txt  20220711230045.txt
20220711230005.txt  20220711230019.txt  20220711230033.txt  systemd-private-222f087707304ec595ce3acd1bbb2057-ModemManager.service-lq2Ext
20220711230007.txt  20220711230021.txt  20220711230035.txt  systemd-private-222f087707304ec595ce3acd1bbb2057-haveged.service-N6oXcm
20220711230009.txt  20220711230023.txt  20220711230037.txt  systemd-private-222f087707304ec595ce3acd1bbb2057-systemd-logind.service-7C8aXK
20220711230011.txt  20220711230025.txt  20220711230039.txt  systemd-private-222f087707304ec595ce3acd1bbb2057-systemd-timesyncd.service-30PbZe
20220711230013.txt  20220711230027.txt  20220711230041.txt

(base) ┌──(maki㉿makilab)-[/home/makilab-share/kali_projects/2006_Unit_sample]
└─$ ls /tmp/
20220711230001.txt  20220711230013.txt  20220711230025.txt  20220711230037.txt  systemd-private-222f087707304ec595ce3acd1bbb2057-ModemManager.service-lq2Ext
20220711230003.txt  20220711230015.txt  20220711230027.txt  20220711230039.txt  systemd-private-222f087707304ec595ce3acd1bbb2057-haveged.service-N6oXcm
20220711230005.txt  20220711230017.txt  20220711230029.txt  20220711230041.txt  systemd-private-222f087707304ec595ce3acd1bbb2057-systemd-logind.service-7C8aXK
20220711230007.txt  20220711230019.txt  20220711230031.txt  20220711230043.txt  systemd-private-222f087707304ec595ce3acd1bbb2057-systemd-timesyncd.service-30PbZe
20220711230009.txt  20220711230021.txt  20220711230033.txt  20220711230045.txt
20220711230011.txt  20220711230023.txt  20220711230035.txt  20220711230047.txt

(base) ┌──(maki㉿makilab)-[/home/makilab-share/kali_projects/2006_Unit_sample]
└─$ ls /tmp/
20220711230001.txt  20220711230013.txt  20220711230025.txt  20220711230037.txt  systemd-private-222f087707304ec595ce3acd1bbb2057-ModemManager.service-lq2Ext
20220711230003.txt  20220711230015.txt  20220711230027.txt  20220711230039.txt  systemd-private-222f087707304ec595ce3acd1bbb2057-haveged.service-N6oXcm
20220711230005.txt  20220711230017.txt  20220711230029.txt  20220711230041.txt  systemd-private-222f087707304ec595ce3acd1bbb2057-systemd-logind.service-7C8aXK
20220711230007.txt  20220711230019.txt  20220711230031.txt  20220711230043.txt  systemd-private-222f087707304ec595ce3acd1bbb2057-systemd-timesyncd.service-30PbZe
20220711230009.txt  20220711230021.txt  20220711230033.txt  20220711230045.txt
20220711230011.txt  20220711230023.txt  20220711230035.txt  20220711230047.txt

(base) ┌──(maki㉿makilab)-[/home/makilab-share/kali_projects/2006_Unit_sample]
└─$ ls /tmp/
20220711230001.txt  20220711230013.txt  20220711230025.txt  20220711230037.txt  20220711230049.txt
20220711230003.txt  20220711230015.txt  20220711230027.txt  20220711230039.txt  systemd-private-222f087707304ec595ce3acd1bbb2057-ModemManager.service-lq2Ext
20220711230005.txt  20220711230017.txt  20220711230029.txt  20220711230041.txt  systemd-private-222f087707304ec595ce3acd1bbb2057-haveged.service-N6oXcm
20220711230007.txt  20220711230019.txt  20220711230031.txt  20220711230043.txt  systemd-private-222f087707304ec595ce3acd1bbb2057-systemd-logind.service-7C8aXK
20220711230009.txt  20220711230021.txt  20220711230033.txt  20220711230045.txt  systemd-private-222f087707304ec595ce3acd1bbb2057-systemd-timesyncd.service-30PbZe
20220711230011.txt  20220711230023.txt  20220711230035.txt  20220711230047.txt

(base) ┌──(maki㉿makilab)-[/home/makilab-share/kali_projects/2006_Unit_sample]
└─$ ls /tmp/
20220711230001.txt  20220711230013.txt  20220711230025.txt  20220711230037.txt  20220711230049.txt
20220711230003.txt  20220711230015.txt  20220711230027.txt  20220711230039.txt  systemd-private-222f087707304ec595ce3acd1bbb2057-ModemManager.service-lq2Ext
20220711230005.txt  20220711230017.txt  20220711230029.txt  20220711230041.txt  systemd-private-222f087707304ec595ce3acd1bbb2057-haveged.service-N6oXcm
20220711230007.txt  20220711230019.txt  20220711230031.txt  20220711230043.txt  systemd-private-222f087707304ec595ce3acd1bbb2057-systemd-logind.service-7C8aXK
20220711230009.txt  20220711230021.txt  20220711230033.txt  20220711230045.txt  systemd-private-222f087707304ec595ce3acd1bbb2057-systemd-timesyncd.service-30PbZe
20220711230011.txt  20220711230023.txt  20220711230035.txt  20220711230047.txt

Discussion

コメントにはログインが必要です。