시스템 종료 중에 업데이트를 설치하는 방법은 무엇입니까?

시스템 종료 중에 업데이트를 설치하는 방법은 무엇입니까?

이 질문은 RedHat 7, 8, 9(및 그 파생 제품)뿐만 아니라 Ubuntu 20.04 및 22.04(모두 systemd 기반)에도 적용됩니다. 내 생각에는 모든 사람의 대답이 비슷할 것 같았지만 그렇지 않다는 것을 알았습니다.

업데이트를 설치할 수 있도록 시스템 종료 순서를 "연결"하고 싶습니다. 내 생각에는 재부팅을 위한 유지 관리 기간을 맞추기가 어렵고 관리자가 재부팅하기 전에 업데이트를 설치하는 것을 잊어버리는 경우가 있기 때문에 이 작업이 자동으로 수행되기를 원합니다.

또한 shutdown -r, shutdown -h, systemctl restart 또는 기타 방법을 사용하여 종료가 시작된 방법과 이유에 관계없이 이 작업이 수행되기를 원합니다.

systemd를 사용하여 종료 시퀀스에 연결하는 일반적인 개요는 매우 간단합니다.

# This service is will watch for a clean shutdown, and install updates etc.
# before the actual shutdown.

# Credit goes to https://opensource.com/life/16/11/running-commands-shutdown-linux

[Unit]
Description=Run package updates at shutdown
#------------ Ubuntu version ------------
Requires=network.target
BindsTo=network.target multi-user.target systemd-resolved.service rsyslog.service
After=network.target multi-user.target systemd-resolved.service rsyslog.service
#------------ RedHat version ------------
Requires=network.target
BindsTo=network.target multi-user.target boot.mount rsyslog.service
After=network.target multi-user.target boot.mount rsyslog.service
#------------- End distro specific ---------
DefaultDependencies=no
Before=shutdown.target reboot.target

[Service]
Type=oneshot
RemainAfterExit=true
ExecStart=/bin/true
ExecStop=update_script.sh
# Allow enough time to complete an update. Default would be 90 seconds
TimeoutStopSec=3600

[Install]
WantedBy=multi-user.target

update_script.sh는 다음을 수행합니다.

systemstatus=$(systemctl is-system-running)
case "$systemstatus" in
 stopping)
   <do updates>
   ;;
 *)
   # Unknown state, don't do updates
   # This prevents updates when starting/stopping the service manually.
   ;;
esac

여기서 문제는 프로세스가 이미 부분적으로 종료된 상태에서 업데이트가 발생한다는 것입니다. 결과적으로 일부 업데이트가 올바르게 설치되지 않습니다.

그러나 종료 시퀀스에서 너무 일찍 스크립트를 넣으면 systemctl is-system-running이 "종료" 대신 "실행 중"을 보고합니다. 이 경우 스크립트는 서비스가 수동으로 중지되었다고 생각하고 갱신을 실행하지 않습니다.

나에게 필요한 것은:

  • 시퀀스의 다른 부분을 닫기 전에 먼저 이 서비스를 실행하는 메서드입니다.
  • systemd가 동시에 다른 서비스를 종료하는 것을 방지하는 방법입니다.
  • 이는 시스템이 종료 프로세스의 초기 단계에 있을 때("systemd is-system-running"이 올바르게 보고되기 전)에도 안정적으로 감지하는 방법입니다.

또는 다른 시스템이 종료되기 전에 시스템 종료에 연결할 수 있는 다른(깨끗하고 해킹되지 않은) 방법이 필요합니다.

참고: 또한 종료 단계 대신 시작 단계에서 업데이트를 시도했습니다. 그러나 이 경우 두 번째 재부팅이 필요할 수 있으므로 권장되지 않습니다.

관련 정보