[Linux] systemd service 등록하기

반응형
반응형

 

systemd로 관리할 테스트 script 작성

#!/bin/bash

echo "start test.sh systemd service " | systemd-cat -p info

while :
do
        echo "Running test.sh service";
        sleep 10;
done

 

작성한 script를 /usr/bin/ 경로로 이동해줍니다

[root@linux-1 system]# ll /usr/bin/test.sh
-rwxr-xr-x 1 root root 134  7월 31 21:20 /usr/bin/test.sh

 

systemd service 등록

[root@linux-1 system]# cat /etc/systemd/system/test.service
[Unit]
Description=test systemd service.

[Service]
Type=simple
ExecStart=/usr/bin/test.sh

[Install]
WantedBy=multi-user.target

 

serivice start

[root@linux-1 system]# systemctl start test

 

serivice 상태 확인

10초마다 script 내용인 "Running test.sh service"가 출력됩니다.

[root@linux-1 system]# systemctl status test
● test.service - test systemd service.
   Loaded: loaded (/etc/systemd/system/test.service; disabled; vendor preset: disabled)
   Active: active (running) since 토 2020-08-01 02:05:30 KST; 1min 19s ago
 Main PID: 10626 (test.sh)
   CGroup: /system.slice/test.service
           ├─10626 /bin/bash /usr/bin/test.sh
           └─10774 sleep 10

 8월 01 02:05:30 linux-1 systemd[1]: Started test systemd service..
 8월 01 02:05:30 linux-1 test.sh[10626]: Running test.sh service
 8월 01 02:05:40 linux-1 test.sh[10626]: Running test.sh service
 8월 01 02:05:50 linux-1 test.sh[10626]: Running test.sh service
 8월 01 02:06:00 linux-1 test.sh[10626]: Running test.sh service
 8월 01 02:06:10 linux-1 test.sh[10626]: Running test.sh service
 8월 01 02:06:20 linux-1 test.sh[10626]: Running test.sh service
 8월 01 02:06:30 linux-1 test.sh[10626]: Running test.sh service
 8월 01 02:06:40 linux-1 test.sh[10626]: Running test.sh service

 

serivice 중지

[root@linux-1 system]# systemctl stop test

 

부팅시 구동되도록 설정

[root@linux-1 system]# systemctl enable test
Created symlink from /etc/systemd/system/multi-user.target.wants/test.service to /etc/systemd/system/test.service.

 

 

참고

- https://www.linode.com/docs/quick-answers/linux/start-service-at-boot/

반응형

댓글

Designed by JB FACTORY

loading