개발/ETC
[ linux ] crontab 특정 유저로 실행하기
쩨이엠
2022. 12. 13. 14:34
728x90
반응형
linux의 스케쥴러인 crontab을 쓰는 방법이다
기본 실행은 root 계정으로 실행되며 crontab 명령어를 사용해 조회 및 추가가 가능하다
기본 crontab 규칙
분 시 일 월 요일 쉘
* * * * * command
- 분 ( 0 ~59 )
- 시 ( 0 ~ 23 )
- 일 ( 1 ~ 31 )
- 월 ( 1 ~ 12 or jan, feb, ...)
- 요일 ( 0 ~ 6 or sun, mon, ... ) : 요일은 일요일(0)부터 시작
ex )
매일 1시에 start.sh 실행
0 1 * * * 위치/start.sh
5분마다 start.sh 실행
*/5 * * * * 위치/start.sh
6시간마다 start.sh 실행
0 */6 * * * 위치/start.sh
root 계정으로 쉘 실행하기 (디폴트)
crontab의 실행계정은 root가 기본이다
crontab 리스트 조회 명령어
$ crontab -l
crontab 추가 / 수정 명령어
$ crontab -e
root 계정이 아닌 특정 계정으로 쉘 실행하기
/etc/crontab 실행
$ vi /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
하면 default로 사용방법과 경로 등이 적혀있다
user-name 부분에 실행할 계정을 넣어준다
예시)
매월 1일 3시에 test 계정으로 start.sh 실행
0 3 1 * * test 위치/start.sh > test.log 2> test_err.log
실행 시 쉘 안에서 발생하는 로그는 test.log에, 에러가 났을 경우는 test_err.log에 입력된다
728x90
반응형