1️⃣ 현상황

crontab 작업을 진행했으나, 인식하지 못하고 있음

[popoo@localhost ~]$ crontab -l
no crontab for popoo

[popoo@localhost ~]$ cat /etc/crontab
SHELL=/bin/bash
PATH=/sbin:/bin:/usr/sbin:/usr/bin
MAILTO=root

# For details see man 4 crontabs

# 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
echo '00 02 * * * popoo logger hello' | sudo tee -a /etc/crontab

2️⃣ 해결원인 _ (시스템 내부)

적절한 포맷 형태를 사용하지 않아서 발생한 문제

1) /etc/crontab 작성
시스템 내부 포맷 형태는 다음과 같음

# 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

2) 재부팅

systemctl restart crond

3️⃣ 출력 확인

1) 실시간으로 로그 모니터링

[root@localhost ~]# tail -f /var/log/messages
Mar 16 08:45:01 localhost popoo[3411]: hello
Mar 16 08:45:01 localhost systemd[1]: Starting Fingerprint Authentication Daemon...
Mar 16 08:45:02 localhost systemd[1]: Started Fingerprint Authentication Daemon.
Mar 16 08:45:04 localhost systemd[1]: session-6.scope: Deactivated successfully.
Mar 16 08:45:04 localhost systemd[1]: session-5.scope: Deactivated successfully.
Mar 16 08:45:32 localhost systemd[1]: fprintd.service: Deactivated successfully.
Mar 16 08:45:56 localhost su[3424]: (to popoo) root on pts/0
Mar 16 08:45:56 localhost systemd[1]: Starting Hostname Service...
Mar 16 08:45:56 localhost systemd[1]: Started Hostname Service.
Mar 16 08:46:26 localhost systemd[1]: systemd-hostnamed.service: Deactivated successfully.

2) logger로 기록된 메시지 확인

[root@localhost ~]# grep "hello" /var/log/messages
Mar 16 08:45:01 localhost popoo[3411]: hello

4️⃣ 기존 코드를 활용하고 싶을 경우 _ (시스템 외부)

1) 시스템 외부에서 사용 가능

[root@localhost ~]# echo '11 09 * * * popoo logger bye' | sudo tee -a /etc/crontab
11 09 * * * popoo logger bye

2) 새로운 크론 작업이 추가됨

vim /etc/crontab

SHELL=/bin/bash
PATH=/sbin:/bin:/usr/sbin:/usr/bin
MAILTO=root

# For details see man 4 crontabs

# 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
45 08 * * * popoo logger hello 
45 08 * * * popoo touch /tmp/cron_test01.txt
45 08 * * * popoo touch /tmp/cron_test02.txt 



11 09 * * * popoo logger bye

3) 출력 확인

cat /var/log/messages

Mar 16 09:11:01 localhost popoo[3621]: bye

5️⃣ 코드 상세 설명

echo '11 09 * * * popoo logger bye'

echo : 해당 크론 작업을 출력하여 파일에 추가하기 위해서
'11 09 * * * popoo logger bye' : crontab 내부 포맷 형식

| sudo tee -a /etc/crontab 

주로 스크립트나 자동화된 방법으로 크론 작업을 추가

6️⃣ 개념

자동화된 작업을 설정할 때 사용
주기적으로 실행할 명령이나 스크립트를 예약하여
정해진 시간에 자동으로 실행되도록 설정하는 데 사용

profile
가즈아😘

0개의 댓글

Powered by GraphCDN, the GraphQL CDN