서버/Linux I 2016. 1. 26. 18:03

Linux I - 13. 작업 관리 (at & cron)

 

 

 

login as: root
 root@192.168.1.2's password:
 Last login: Tue Jan 26 18:12:18 2016 from 192.168.1.1
 [root@CentOS /root]# cd /test

 

 

login as: user1
 user1@192.168.1.2's password:
 Last login: Tue Jan 26 18:12:26 2016 from 192.168.1.1
 [user1@CentOS /home/user1]$

 

 

 

atd 데몬과 crond 데몬을 이용하여 시스템 작업 스케쥴을 관리할 수 있다.

 

 

1. atd 데몬

 

atd 데몬은 관리자가 지정한 시간에 작업이 실행될 수 있도록 작업 스케쥴을 처리하는 기능을 수행하며, at 내용은 '/var/spool' 디렉토리에 파일로 저장하여 관리한다. atd 데몬을 사용하기 위해서는 다음과 같이 atd 데몬을 활성화해야 한다.

 

 

[root@CentOS /root]# service atd restart
atd 를 정지 중:                                            [  OK  ]
atd (을)를 시작 중:                                       [  OK  ]

 

[root@centos /root]# pgrep -l atd


 

Ex1) 'at' 명령어 이해 I

 

[root@CentOS /root]# mkdir /test
[root@CentOS /root]# cd /test
[root@CentOS /test]#


[root@CentOS /test]# date 01262100
2016. 01. 26. (화) 21:00:00 KST


[root@CentOS /test]# at 21:03
at> touch /test/at1
at> touch /test/at2
at> (ctrl+d)
job 1 at 2016-01-26 21:03


[root@CentOS /test]# at now + 5min
at> touch /test/at3
at> touch /test/at4
at> (ctrl+d)
job 2 at 2016-01-26 21:05


[root@CentOS /test]# at teatime
at> touch /test/at5
at> (ctrl+d)
job 3 at 2016-01-27 16:00

[root@CentOS /test]# atq
1       2016-01-26 21:03 a root
2      2016-01-27 16:00 a root
3       2016-01-26 21:05 a root

[root@CentOS /test]# ls -l /test
합계 0


[root@CentOS /test]# date
2016. 01. 26. (화) 21:02:45 KST

[root@CentOS /test]# ls -l /test
합계 0
-rw-r--r--. 1 root root 0 2016-01-26 21:03 at1
-rw-r--r--. 1 root root 0 2016-01-26 21:03 at2

[root@CentOS /test]# date
2016. 01. 26. (화) 21:04:55 KST

[root@CentOS /test]# ls -l /test
합계 0
-rw-r--r--. 1 root root 0 2016-01-26 21:03 at1
-rw-r--r--. 1 root root 0 2016-01-26 21:03 at2
-rw-r--r--. 1 root root 0 2016-01-26 21:05 at3
-rw-r--r--. 1 root root 0 2016-01-26 21:05 at4

[root@CentOS /test]# atrm 1
[root@CentOS /test]# atrm 2
[root@CentOS /test]# atrm 3

[root@CentOS /test]# rm -f at*
[root@CentOS /test]# ls

 

 

 

Ex2) 'at' 명령어 이해

 

 - user1 putty 창

 

[user1@CentOS /home/user1]$ tty
/dev/pts/1

 

 

 - root putty 창


[root@CentOS /test]# date
2016. 01. 26. (화) 21:22:32 KST

 

[root@CentOS /test]# at 2124                          21시24분에 user1에게 'echo hello' 작업 예약
at> echo hello > /dev/pts/1
at> (ctrl+d)
job 4 at 2016-01-26 21:24


[root@CentOS /test]# at 2130                          21시30분에 user1에게 'echo hello' 작업 예약
at> echo hi > /dev/pts/1
at> (ctrl+d)
job 5 at 2016-01-26 21:30


[root@CentOS /test]# at -l       (# atq 동일)      작업 예약 내용 확인
5       2016-01-26 21:30 a root
4       2016-01-26 21:24 a root

 

 

 - user1 putty 창


[user1@CentOS /test]$ date                            user1에서 현재 시간 확인
2016. 01. 26. (화) 21:23:54 KST


[user1@CentOS /test]$
[user1@CentOS /test]$ hello                           root에서 21시24분에 예약한 작업 내용 처리됨

 

 

 - root putty 창


[root@CentOS /test]# atrm 5                             21시30분 예약한 작업 예약 취소
[root@CentOS /test]# atq

 

 

Ex3) 'at' 명령어 사용자 제한

 

[root@CentOS /test]# ls -l /etc/at.*
-rw-r--r--. 1 root root 1 2012-01-30 22:56 /etc/at.deny   'at.deny' 파일에 사용자를 추가하면, at 명령어 사용을 제한한다.

[root@CentOS /test]# vi /etc/at.allow                     만약, 'at.allow' 파일이 있으면, 'at.deny' 파일은 사용되지 않는다.

1 user1

 

:wq! 

 

 

[root@CentOS /test]# su - user1
[user1@CentOS /home/user1]$
[user1@CentOS /home/user1]$ at 2150
at> echo hello
at> (ctrl+d)
job 6 at 2016-01-26 21:50


[user1@CentOS /home/user1]$ at -l
6       2016-01-26 21:50 a user1


[user1@CentOS /home/user1]$ exit
logout
[root@CentOS /test]#


[root@CentOS /test]# su - user2
[user2@CentOS /home/user2]$
[user2@CentOS /home/user2]$ at 2155
You do not have permission to use at.

[user2@CentOS /home/user2]$ exit
logout
[root@CentOS /test]#rm -f /etc/at.allow

 

 

 

Ex4) 10시간 뒤에 전원 끄기

 

[root@CentOS /test]# at now + 10 hour -f /usr/bin/poweroff


[root@CentOS /test]# atq
7      2016-01-27 21:29 a root


[root@CentOS /test]# atrm 7

 

 

 

Ex5) 특정 날짜/시간 예약


[root@CentOS /test]# at -t 12251400    (#at 02pm Dec 25 동일)
at> mail user1@localhost < /root/mail.txt
at> (ctrl+d)
job 8 at 2016-12-25 14:00

 

[root@CentOS /test]# atq
8      2016-12-25 14:00 a root

[root@CentOS /test]# atrm 8

 

 

 

Ex6) am/pm, tomorrow 사용 예제

 

[root@CentOS /test]# at 10:30pm tomorrow
at> touch /test/at6
at> (ctrl+d)
job 9 at 2016-01-28 22:30

[root@CentOS /root]#[root@CentOS /test]#

9      2016-01-28 22:30 a root

[root@CentOS /test]# atrm 9

 

 

 

 

2. crond 데몬

 

crond 데몬은 같은 작업을 주기적(분,시,일,주,월,요일)으로 실행될 수 있도록 작업 스케쥴을 처리하는 기능을 수행하며, cron 내용은 '/var/spool' 디렉토리에 파일로 저장하여 관리한다. crond 데몬을 사용하기 위해서는 다음과 같이 crond 데몬을 활성화해야 한다.

 

[root@CentOS /test]# service crond restart
crond 를 정지 중:                                          [  OK  ]
crond (을)를 시작 중:                                     [  OK  ]

 

[root@centos /test]# pgrep -l crond 


 

Ex1) 'crond' 데몬 예제 I
 

 - user1 putty 창

 

[user1@CentOS /home/user1]$ tty
/dev/pts/1

 

 

 - root putty 창

 

[root@CentOS /test]# date 01271040 
2016. 01. 27. (수) 10:40:00 KST

 

[root@CentOS /test]# date
2016. 01. 27. (수) 10:40:45 KST

[root@CentOS /test]# crontab -e

    1 45 10 27 1 3 echo "Hello" > /dev/pts/1

 


[root@CentOS /test]# crontab -l
45   10  27    1      3     echo "Hello" > /dev/pts/1
분  시  일  월  요일 (0부터 일요일)

 

 

 - user1 putty 창


[user1@CentOS /home/user1]$ date
2016. 01. 27. (수) 10:44:46 KST


[user1@CentOS /home/user1]$
[user1@CentOS /home/user1]$ Hello

 

 

 - root putty 창

 

[root@CentOS /test]# crontab -l

 

 

 

Ex2) 'crond' 데몬 예제 II

 

 - root putty 창

 

[root@CentOS /test]# date
2016. 01. 27. (수) 10:58:22 KST

 

[root@CentOS /root]# crontab -e

    1 0 11 27 1 3 cat /etc/passwd | head -n 5 > /dev/pts/1

 


[root@CentOS /test]# crontab -l
0    11    27  1     3 cat /etc/passwd | head -n 5 > /dev/pts/1

분  시  일  월  요일 (0부터 일요일)

 

 - user1 putty 창

 

[user1@CentOS /home/user1]$ date
2016. 01. 27. (수) 10:59:40 KST

 

[user1@CentOS /home/user1]$
root:x:0:0:root:/root:/bin/bash
bin:x:1:1:bin:/bin:/sbin/nologin
daemon:x:2:2:daemon:/sbin:/sbin/nologin
adm:x:3:4:adm:/var/adm:/sbin/nologin
lp:x:4:7:lp:/var/spool/lpd:/sbin/nologin

 

 

 - root putty 창

 

[root@CentOS /test]# crontab -l


 

 

Ex3) 'cron' 명령어 사용자 제한

 

 - root putty 창

 

[root@CentOS /test]# ls -l /etc/cron.deny
-rw-------. 1 root root 0 2013-11-23 21:43 /etc/cron.deny


[root@CentOS /test]# vi /etc/cron.deny

1 user1

 

:wq!

 

 

 

[root@CentOS /test]# cat /etc/cron.deny
user1

 

[root@CentOS /test]#


[root@CentOS /test]# su - user1
[user1@CentOS /home/user1]$ crontab -e
You (user1) are not allowed to use this program (crontab)
See crontab(1) for more information


[user1@CentOS /home/user1]$
[user1@CentOS /home/user1]$ exit
logout

[root@CentOS /test]#

[root@CentOS /test]# vi /etc/cron.deny

1 user1 <- 삭제

 

:wq!

 

 

[root@CentOS /test]# rdate -s time.bora.net

 

 

 

Ex4) '/etc/cron' 설정 파일 이해

 

[root@CentOS /test]# date
2016. 01. 27. (수) 13:31:21 KST

 

[root@CentOS /test]# vi /etc/crontab

 1 SHELL=/bin/bash
  2 PATH=/sbin:/bin:/usr/sbin:/usr/bin
  3 MAILTO=root
  4 HOME=/
  5
  6 # For details see man 4 crontabs
  7
  8 # Example of job definition:
  9 # .---------------- minute (0 - 59)
 10 # |  .------------- hour (0 - 23)
 11 # |  |  .---------- day of month (1 - 31)
 12 # |  |  |  .------- month (1 - 12) OR jan,feb,mar,apr ...
 13 # |  |  |  |  .---- day of week (0 - 6) (Sunday=0 or 7) OR sun,mon,tue,wed,t    hu,fri,sat
 14 # |  |  |  |  |
 15 # *  *  *  *  * user-name command to be executed
 16
 17 # run-parts
 18 40 * * * * root run-parts /etc/cron.hourly      (매시간 40분 마다 /etc/cron.hourly 디렉토리 내용 실행)

 19 41 13 * * * root run-parts /etc/cron.daily        (매일 1시 41분마다 /etc/cron.daily  디렉토리 내용 실행)
 20 42 13 * * 3 root run-parts /etc/cron.weekly     (수요일 1시 42분마다 /etc/cron.weekly 디렉토리 내용 실행)
 21 43 13 27 * * root run-parts /etc/cron.monthly   (매월 27일 1시 43분마다 /etc/cron.monthly 디렉토리 내용 실행)


:wq!

 


[참고]' run-parts' 명령어


 - 여러개의 실행 파일을 실행할때, 실행 파일들이 있는 특정 디렉토리를 지정하면 한번에 실행하는 명령어


[root@Centos /test]# cp /bin/date .

[root@Centos /test]# cp /usr/bin/cal .

[root@Centos /test]# cp /usr/bin/who .

[root@Centos /test]#

[root@Centos /test]# run-parts /test

/test/cal:


      1월 2016

일 월 화 수 목 금 토

                1  2

 3  4  5  6  7  8  9

10 11 12 13 14 15 16

17 18 19 20 21 22 23

24 25 26 27 28 29 30

31

/test/date:


2016. 01. 27. (수) 23:25:16 KST

/test/who:


root     pts/0        2016-01-27 23:04 (192.168.1.1)

user1    pts/1        2016-01-27 23:04 (192.168.1.1)



[root@CentOS /test]# cd /

[root@CentOS /]# rm -rf test



'서버 > Linux I' 카테고리의 다른 글

Linux I - 15. 디렉토리 구조  (0) 2016.01.27
Linux I - 14. 파일 타입  (0) 2016.01.27
Linux I - 12. 프로세스 관리  (0) 2016.01.25
Linux I - 11. 압축 & 아카이빙  (0) 2016.01.25
Linux I - 10. 계정 생성 및 관리  (0) 2016.01.20
Posted by 김정우 강사(카카오톡 : kim10322)
,


Q