systemd 타이머로 작업 스케줄 걸기

  • Post author:
  • Post category:칼럼
  • Post comments:0 Comments
  • Post last modified:February 8, 2020

CoreOS에서 가끔 Cronjob을 돌리고 싶을 때가 있다. Kubernetes 같은 Container Orchestration 도구를 사용하는 회사라면 Kubernetes에 내장된 기능을 쓰면 되지 굳이 운영체제까지 건드려서 일을 복잡하게 만들 이유가 없다. 다만 단일 서버로 가볍게 서비스를 운용하는 경우라면 Cronjob까지 Dockerize하기는 과하다 싶을 수 있다. 그래서 이번 글에서는 CoreOS의 systemd timer를 이용해 WP-Cron Contol 플러그인을 호출하는 법을 사례로 살펴본다.

WP-Cron Contol을 호출하려면 wget으로 지정된 URL에 요청을 보내면 된다. 기본적으로는 wget을 몇 분에 한번씩 실행하면 워드프레스가 그때마다 예정된 작업을 수행하게 된다.

WP-Cron%20Control%20Settings%20%E2%80%B9%20Andromeda%20Rabbit%20%E2%80%94%20WordPress

이제 systemd 파일 두 개를 생성하면 된다.

  • /etc/systemd/system/wp-cron.service: wget 실행을 주관한다.
  • /etc/systemd/system/wp-cron.timer: wp-cron.service을 주기적으로 실행한다.

각 파일은 다음과 같이 기술한다.

# wp-cron.service
[Unit]
Description=Run WP-cron

[Service]
Type=simple
ExecStart=/usr/bin/sh -c '/usr/bin/wget -q "https://andromedarabbit.net/wp-cron.php?doing_wp_cron&702c619152a0d0a447ea553a205a4a48"'
# wp-cron.timer
[Unit]
Description=Run date.service every 15 minutes

[Timer]
# Time to wait after booting before we run first time
OnBootSec=10min
OnCalendar=*:0/15

[Install]
WantedBy=default.target

이렇게 하고 나서 서비스를 등록하고 실행하면 된다.

sudo systemctl enable cron.timer
sudo systemctl start cron.timer

sudo systemctl list-timers --all
sudo systemctl status cron.timer

그리고 DigitalOcean 등에서 인스턴스를 생성하는 시점에 타이머를 등록하고 싶다면 Cloud-config를 다음과 같이 설정하면 된다.

#cloud-config

coreos:
units:
- name: wp-cron.service
content: |
[Unit]
Description=Run WP-cron

[Service]
Type=simple
ExecStart=/usr/bin/sh -c '/usr/bin/wget -q "https://andromedarabbit.net/wp-cron.php?doing_wp_cron&702c619152a0d0a447ea553a205a4a48"'
- name: wp-cron.timer
command: start
content: |
[Unit]
Description=Run date.service every 15 minutes

[Timer]
# Time to wait after booting before we run first time
OnBootSec=10min
OnCalendar=*:0/15

[Install]
WantedBy=default.target

대안

꼭 Docker로 스케줄링까지 관리하겠다 하면 Jobber라는 녀석을 써봐도 좋다. Docker 이미지까지 잘 구성해서 제공하기 때문에 매우 간편하다.

Author Details
Kubernetes, DevSecOps, AWS, 클라우드 보안, 클라우드 비용관리, SaaS 의 활용과 내재화 등 소프트웨어 개발 전반에 도움이 필요하다면 도움을 요청하세요. 지인이라면 가볍게 도와드리겠습니다. 전문적인 도움이 필요하다면 저의 현업에 방해가 되지 않는 선에서 협의가능합니다.
0 0 votes
Article Rating
Subscribe
Notify of
guest

This site uses Akismet to reduce spam. Learn how your comment data is processed.

0 Comments
Inline Feedbacks
View all comments