Skip to content

Systemd

systemd is a suite of basic building blocks for a Linux system.

Categories

Unit configuration

[Unit]
Requires= # 다른 unit들과 dependency를 정의하는 부분.
          # 다른 unit이 activation이 실패하면 같이 실패 됨.

Wants=    # Requires와 같은 기능을 하지만 다른 unit이 activation 실패해도 
          # 영향을 받지 않음. 따라서 다른 unit이 start-up할 때 hooking 하여
          # 같이 실행 될 때 사용함.

Before=   # dependency 있는 unit들을 ordering하는 속성.
After=    # 만약 foo.service unit에 Before=bar.service를 설정하면
          # foo.service가 다 strat-up 될 때 까지 bar.service unit은 delay 됨.
          # 죽, 내 before=로 설정한 unit 전에 실행 시키라는 속성.

[Install] # systemd의 runtime에 interpret되는 것이 아니라 unit을 설치하는 
          # systemd enable/disable command에 의해 영향을 받는 section

WantedBy=   # 이 option을 설정하면 systemlctl enable시 .wants/ .requires/
RequiredBy= # 폴더에 symbolic link를 생성해줌


# 여러 줄로 명령을 주고 싶다면 "\"을 준다:
ExecStart=java -server -Xms124M -Xmx1024M -XX:+HeapDumpOnOutOfMemoryError \
    -classpath /usr/local/plcGateway/lib/* \
    com.mseo.main.main /usr/local/plcGateway/cfg

Network Targets

Network Configuration Synchronization Points

  • network.target - 네트워크 서비스가 Active 상태인지 확인한다.
  • network-online.target - 네트워크 인터페이스가 UP 상태인지 확인한다.

multi-user.target

run-level-3이 시작됨을 알려준다.

서비스 파일 등록

systemd는 기본적으로 루트 권한으로 실행된다. 해당 디렉토리는/etc/systemd/system/ 이다. 이 곳에 서비스이름.service 파일을 추가하고 다음 명령으로 활성화 할 수 있다.

systemctl daemon-reload
systemctl enable 서비스이름
systemctl start 서비스이름

--user 옵션을 사용하는 일반 사용자 서비는 $HOME/.config/systemd/user 이다.

systemctl --user daemon-reload
systemctl --user enable 서비스이름
systemctl --user start 서비스이름

노트북 화면 꺼짐 조정 (?)

Edit /etc/systemd/logind.conf and make sure you have:

HandleLidSwitch=ignore

그리고 아래 명령을 날린다:

$ systemctl restart systemd-logind

Power management 항목 및 ArchWiki 참조.

또다른 방법 (?)

You can use this to temporarily disable lid-switch events:

$ systemd-inhibit --what=handle-lid-switch sleep 1d

키워드

Hibernate, lid, Power management, acpi, 등 으로 검색...

임베디드 리눅스에서 systemd가 문제가 되는 이유

주로 자원을 많이 사용하느 이슈 등

  • systemd는 많은 문제를 해결하며, 커뮤니티도 매우 응답성이 좋고 도움이 됨
    • 가끔 이상한 문제가 있지만 항상 해결책이 존재함
    • 메모리 사용은 중요하지 않으며, 대부분의 임베디드 Linux 장치는 1GB 이상의 RAM을 가짐
    • 작은 장치에서는 Zypher나 FreeRTOS를 사용함
  • OpenEmbedded/Yocto, Devuan, Gentoo는 여러 init 시스템을 제공함
  • Rustysd는 systemd의 동작을 일부 복제하는 서비스 관리자임
    • core 기능을 제공하며, 다양한 플랫폼에 systemd와 유사한 서비스 관리자를 제공할 수 있음
  • systemd를 선호하는 사람들은 주로 배포 관리자임
    • systemd는 다양한 시스템 유틸리티를 하나의 큰 빌드로 제공함
  • Unix 철학은 작은 도구들이 문제를 해결하는 것임
    • systemd는 초기에는 작았으나, 현재는 웹 기반으로 비디오 회의도 가능함
  • runit는 애플리케이션과 서비스를 감독하는 데 훌륭함
    • BusyBox에 내장되어 있으며 매우 가벼움
    • 설정이 매우 간단하며, 로깅 기능도 포함됨
    • Unix 철학을 거의 완벽하게 구현함
  • udev 없이 임베디드 시스템을 실행할 수 있음
    • 고정된 주변 장치와 커널 모듈을 사용하는 경우 가능함
  • RPi는 산업용 제어 보드의 폼 팩터를 데스크탑으로 변환함
    • 많은 소프트웨어가 이 세그먼트를 염두에 두고 작성되지 않음
  • Chimera Linux는 서비스 관리와 로그인/좌석 관리에서 흥미로운 방향으로 나아가고 있음
    • systemd의 gnulibc와 gcc 확장 사용이 문제임
  • systemd의 설치 크기가 임베디드 Linux에서 가장 큰 문제임
    • 64M RAM과 128M NAND 플래시를 가진 장치에서는 5M의 systemd가 비효율적임
  • systemd는 250MB의 RAM을 사용하여 임베디드 Linux에서 문제가 됨
    • mainstream 배포판을 고려하지 않는 시스템에서는 큰 문제가 아님
  • SysVInit는 외부 유틸리티에 의존하여 프로세스를 시작하고 중지함
    • 최소한의 경우에도 init 스크립트를 실행하기 위해 셀이 필요함

Example

Docker example

[Unit]
Description=apache2
After=mysql.service
Requires=mysql.service

[Service]
TimeoutStartSec=0
ExecStartPre=-/usr/bin/docker kill apache2-server
ExecStartPre=-/usr/bin/docker rm apache2-server
ExecStart=/usr/bin/docker run -p 60004:80 --volumes-from apache2-volume --volumes-from mysql-volume -v /etc/localtime:/etc/localtime:ro --name apache2-server apache2-server
ExecStop=/usr/bin/docker kill apache2-server

[Install]
WantedBy=multi-user.target

JupyterNotebook service

$ sudo nano /etc/systemd/system/jupyterNotebook.service
[Unit]
Description=Jupyter Notebook Server

[Service]
Type=simple
User=<username>
ExecStart=/home/<username>/.local/bin/jupyter-notebook
WorkingDirectory=/your/working/dir

[Install]
WantedBy=multi-user.target
$ systemctl daemon-reload
$ systemctl enable jupyterNotebook
$ systemctl start jupyterNotebook

ANSWER

[Unit]
Description=The ANSWER, No-code development platform

[Service]
Environment=RECC_VERBOSE=2
Environment=RECC_DEVELOPER=true
Environment=PYTHONPATH="/home/yourname/Project/answer/core"
ExecStart="/home/yourname/.pyenv/versions/opy-yourname-3.8.9/bin/python" -m recc core --storage-root "/home/yourname/Project/answer/core/storage"
User=root
Group=root

[Install]
WantedBy=multi-user.target

한번만 실행

[Unit]
Description=Set DNS for Nebula Interface
After=nebula.service

[Service]
ExecStart=/usr/local/bin/set-nebula-dns.sh
Type=oneshot
RemainAfterExit=true

[Install]
WantedBy=multi-user.target

vs chkconfig

What about chkconfig? That changed too? Yes, now you want to use systemctl for the chkconfig commands also..

systemctl enable httpd
chkconfig service on
systemctl disable httpd
chkconfig service off
systemctl is-enabled httpd
chkconfig service (is it set up to start?)
systemctl list-unit-files --type=service
chkconfig –list (shows what is and isn’t enabled)

LogNamespace

Journalctl#LogNamespace 항목 참조.

Troubleshooting

systemctl restart등을 사용하여 서비스 실행에 실패했을 경우 journalctl -xe를 사용하면 로그를 확인할 수 있다.

See also

Favorite site

References


  1. Systemctl_-_PGWiki.pdf