記事:
# 設定ファイルの再読込 sudo systemctl daemon-reload # 起動 sudo systemctl start ${Unit} # 停止 sudo systemctl stop ${Unit} # 再起動 sudo systemctl restart ${Unit} # リロード sudo systemctl reload ${Unit} # ステータス表示 sudo systemctl status ${Unit} # 自動起動有効 sudo systemctl enable ${Unit} # 自動起動無効 sudo systemctl disable ${Unit} # 自動起動設定確認 sudo systemctl is-enabled ${Unit} # サービス一覧 sudo systemctl list-unit-files --type=service # 定期実行(timer)一覧 sudo systemctl list-unit-files --type=timer sudo systemctl list-timers
[Unit] Description=example service After=syslog.target network.target [Service] Type=simple User=example Group=example WorkingDirectory=/opt/example Restart=always TimeoutSec=90 EnvironmentFile=-/etc/sysconfig/example Environment=PID_FILE=/var/run/example.pid ExecStartPre=+/bin/mkdir -p /run/example ExecStartPre=+/bin/chown example:example /run/example ExecStart=/opt/example/bin/node /opt/example/bin/app.js ExecStartPost=/bin/sh -c "echo $MAINPID > $PID_FILE" StandardOutput=append:/var/log/example/example.log StandardError=append:/var/log/example/example.log LimitNOFILE=10240 [Install] WantedBy=multi-user.target
sudo systemctl edit example.service -- [Service] # 優先度を変更する場合 # プロセスの優先度。default 0 Nice=19 # IOの重み。default 100 IOWeight=10 # 重み。1-10000, default 100. 200のほうが優先度が高い。利用可能な CPU リソースが枯渇した場合にのみ適用される。 CPUWeight=10 # 1 CPU coreあたりの使用を制限 CPUQuota=10% # 物理メモリの割当上限 MemoryLimit=128M -- # 以下が自動生成される /etc/systemd/system/example.service.d/override.conf sudo systemctl daemon-reload sudo serivce example restart
記事:
systemctl list-timers NEXT LEFT LAST PASSED UNIT ACTIVATES Fri 2024-11-29 01:46:48 UTC 4min 55s left Fri 2024-11-29 01:41:48 UTC 4s ago nm-cloud-setup.timer nm-cloud-setup.service Fri 2024-11-29 02:40:33 UTC 58min left Fri 2024-11-29 00:47:00 UTC 54min ago dnf-makecache.timer dnf-makecache.service Fri 2024-11-29 06:12:02 UTC 4h 30min left Thu 2024-11-28 06:12:02 UTC 19h ago systemd-tmpfiles-clean.timer systemd-tmpfiles-clean.service Sat 2024-11-30 00:00:00 UTC 22h left Fri 2024-11-29 00:00:01 UTC 1h 41min ago logrotate.timer logrotate.service Sat 2024-11-30 00:00:00 UTC 22h left Fri 2024-11-29 00:00:01 UTC 1h 41min ago mlocate-updatedb.timer mlocate-updatedb.service
/etc/systemd/system/timers.target.wants/logrotate.timer
[Service] Restart=on-failure
記事:
確認方法:
proc_user=chrony proc_name=chronyd pgrep -u $proc_user $proc_name 64195 sudo pkill -u $proc_user $proc_name # systemdで再起動され、pidが変わったか確認 pgrep -u $proc_user $proc_name # ログの確認 sudo service $proc_name status ... # Activeが起動した時間に変わっている Active: active (running) since ... # ログに起動時のログが出ている
[Service] Environment="MYENV=dummy"
sudo systemctl daemon-reload sudo service myservice restart # 値が設定されているか確認 systemctl show myservice.service | grep -i -E "Env"
grep -i umask /proc/$(pgrep -u chrony chrony)/status Umask: 0022
systemctl show chrony.service | grep -i -E "umask" UMask=0022
例: apacheでログファイルのパーミッションを664にしたい。
[Service] UMask=0002
sudo systemctl daemon-reload sudo service httpd restart
[Unit] ... After=network-online.target
/usr/lib/systemd/system/<unit>.service
/etc/systemd/system/<unit>.d/<any>.conf
sudo systemctl edit nginx.service -- [Service] ExecStart= ExecStart=<new config> -- # 以下が自動生成される /etc/systemd/system/nginx.service.d/override.conf
sudo systemctl edit logrotate.timer # 以下が自動生成される /etc/systemd/system/logrotate.timer.d/override.conf
systemctl show nginx # OR systemctl show nginx -p ExecStart
ExecStart=/path/to/program >> /var/log/program.log
sudo journalctl -u logrotate
sudo journalctl -p err
# 昨日からのログを表示 sudo journalctl --since="yesterday"
grep TimeoutStart /etc/systemd/system.conf #DefaultTimeoutStartSec=90s
for pid in $(pgrep mongod); do sudo cat /proc/$pid/limits | grep -P "open"; done Max processes 64000 64000 processes Max open files 64000 64000 files
sudo mkdir /etc/systemd/system/mongod.service.d sudo vim /etc/systemd/system/mongod.service.d/limits.conf -- # https://docs.mongodb.com/manual/reference/ulimit/ # # The settings is what mongoDB officially recommends. # [Service] # Other directives omitted # (file size) LimitFSIZE=infinity # (cpu time) LimitCPU=infinity # (virtual memory size) LimitAS=infinity # (open files) LimitNOFILE=64000 # (processes/threads) LimitNPROC=64000 -- sudo systemctl daemon-reload sudo service mongod restart
systemctl show service -p TimeoutStartUSec TimeoutStartUSec=1min 30s
sudo vim /etc/systemd/system/mongod.service.d/timeout.conf -- [Service] TimeoutStartSec=3m -- sudo systemctl daemon-reload systemctl show mongod -p TimeoutStartUSec TimeoutStartUSec=3min