본문 바로가기
Tech/Linux

systemd를 사용하여 프로세스 재시작하기

by 타이호 2020. 2. 14.
반응형

간단한 nginx를 사용중인데 간혹 프로세스가 죽어서 리스타트를 해줘야 할 때가 있다

서비스가 안될 때 서버에 들어가서 리스타트를 한다던지, 아니면 서비스를 모니터링 해야 하는데

간단하게 systemd 데몬을 이용하여 서비스가 죽으면 다시 서비스를 재시작 할 수 있다.

 

Ubuntu 기준 nginx service의 설정은 아래 위치에 있다

/lib/systemd/system/nginx.service

위의 기본 설정에서 [Service] 항목에 Restart=Always를 삽입하면 된다.

 

Systemd에 대한 보다 자세한 설명은 아래 사이트를 참조한다.

https://www.freedesktop.org/software/systemd/man/systemd.service.html

 

systemd.service

Sometimes, units should just execute an action without keeping active processes, such as a filesystem check or a cleanup action on boot. For this, Type=oneshot exists. Units of this type will wait until the process specified terminates and then fall back t

www.freedesktop.org

여기에서 보면 Restart옵션과 RestartSec옵션이 있는데, 

Restart는 Configures whether the service shall be restarted when the service process exits, is killed, or a timeout is reached.

RestartSec은 Configures the time to sleep before restarting a service (as configured with Restart=)

말 그대로 프로세스가 종료되거나 죽거나 timeout에 해당되면 systemd가 restart를 해준다는 것이다.

위와 같이 설정 한다음 데몬을 다시 reload하고 nginx 프로세스를 띄워본다

systemctl daemon-reload

현재 떠 있는 상황에서 nginx를 kill하고 난 뒤 systemd가 restart를 시키는지 확인해본다.

잘 되는것 같음 :)

반응형