cron 사용기

THXX·2023년 9월 18일
0

cron 이란 무엇인가! 크론병과는 무관하다. 찾아봤는데 역시 시간의 신 크로노스와 관련이 있는 듯 하다. 멋지다.

거두절미하고 이것은 UNIX 시스템에서 일정을 정해서 작업을 시킬 수 있는 수단이며 운영체제에 상주하는 데몬 프로그램 (Daemon Program)이다.

작업 기전은 다음과 같다.

  1. /etc/crontab 수정 (아니면 /etc/cron.d에 자신만의 cron 스크립트를 작성해도 좋다.)
  2. cron restart (systemctl restart cron)

너무 쉽다.

# /etc/crontab: system-wide crontab
# Unlike any other crontab you don't have to run the `crontab'
# command to install the new version when you edit this file
# and files in /etc/cron.d. These files also have username fields,
# that none of the other crontabs do.

SHELL=/bin/sh
# You can also override PATH, but by default, newer versions inherit it from the environment
#PATH=/usr/local/sbin:/usr/local/bin:/sbin:/bin:/usr/sbin:/usr/bin

# Example of job definition:
# .---------------- minute (0 - 59)
# |  .------------- hour (0 - 23)
# |  |  .---------- day of month (1 - 31)
# |  |  |  .------- month (1 - 12) OR jan,feb,mar,apr ...
# |  |  |  |  .---- day of week (0 - 6) (Sunday=0 or 7) OR sun,mon,tue,wed,thu,fri,sat
# |  |  |  |  |
# *  *  *  *  * user-name command to be executed
17 *    * * *   root    cd / && run-parts --report /etc/cron.hourly
25 6    * * *   root    test -x /usr/sbin/anacron || ( cd / && run-parts --report /etc/cron.daily )
47 6    * * 7   root    test -x /usr/sbin/anacron || ( cd / && run-parts --report /etc/cron.weekly )
52 6    1 * *   root    test -x /usr/sbin/anacron || ( cd / && run-parts --report /etc/cron.monthly )
0 0    * * *   root    cd /home/#/pythonScript && python3 imagegenEdit.py

이것이 나의 crontab이다.

리눅스 개발자의 친절에 감동의 눈물을 흘릴 수 밖에 없는 직관성을 가진다. minute, hour, day 등 초급 영어 단어를 읽을 수 있다면 그대는 리눅스에서 작업을 예약할 수 있는 사람이 된다.

분/시/일/월/요일/유저명/커맨드

유저명은 해당 커맨드를 작동시키는 유저를 정한다. 따라서 crontab 등 cron 관련 스크립트는 모두 root 권한으로 수정되어야 할 것이다. 안그러면 악의적인 커맨드를 root 권한으로 실행시키는 보안해저드가 발생하리라.

커맨드를 살짝 들여다 보자. 마지막것만 내가 추가했고 위 4개는 전부 원래 있었던 것이다.

나머지 test 명령어나 run-parts 는 무시하고 중요한 사실만을 캐내자.
cron.daily, weekly, monthly, hourly
이들은 전부 폴더이다. 위 cron 스크립트들은 전부 각각의 폴더 안에 있는 스크립트를 실행할 것이다.
따라서 굳이 crontab을 수정하고 싶지 않다면 원하는 주기를 가지는 폴더에 커스텀 스크립트를 삽입하도록 하자.
권한은 755 root:root.

참고로 아래 스크립트는 https://twitter.com/2023progressKST 이 친구를 돌리는 스크립트다. 팔로우를 해주면 좋겠다.
트위터 API의 사용법에 대해서도 간단히 글을 올려볼 생각이다.

profile
THXX FOR EVERYTHING

0개의 댓글