본문 바로가기
개발, IT

데몬 서비스 자동 실행

by Nabi™ 2008. 9. 10.

[개요]
mysql 이나 apache 같은 사용자가  설치한 프로그램 들을 부팅시 자동으로  실행 하기 위해서
chkconfig 와 ntsysv를  사용한다.
이런 작업을 하기 위해서 알아야 하는 내용들을 정리 해보았다.

chkconfig  를 알기 전에 runlevel 과 init.d 그리고  setup 같은 로딩 서비스 관리 툴의 연관을 이해 하여야 겠습니다.

가. About Run-level ?  

리눅스나 유닉스는 runlevel이라는것이 존재 한다.
일반적으로 Window 에서 Safe 모드와 prompt모드와 일반 모드로 부팅시 선택 할수 있듯이, unix도 다양한 구동 방식이 존재한다. 이것을 runlevel 이라고 하며 부팅시 선택 되도록 되어있다.

그리고 슈퍼관리자를 위해서 콘솔에서 직접  /sbin/init 라는 명령으로 제어가 가능하다.

> init 6 : 리부팅 시스널을 일으키고
> init 0 : 라고 하면 시스템이 halt 되거나 power를 내린다.

부팅시 선택 되도록 설정한 환경은 /etc/inittab 라는 파일에 다음과 같이 명시되어 있다

>>/etc/inittab

# Default runlevel. The runlevels used by RHS are:
#   0 - halt (Do NOT set initdefault to this)
#   1 - Single user mode
#   2 - Multiuser, without NFS (The same as 3, if you do not have networking)
#   3 - Full multiuser mode
#   4 - unused
#   5 - X11
#   6 - reboot (Do NOT set initdefault to this)

id:3:initdefault:


> id:3:initdefault: 에서 3 이라고 명시 하면  Full multiuser mode 로 text기반 콘솔로 로딩이 된다
> id:5:initdefault:  로 설정 하면 Full multiuser mode  + X11 로 로딩 되며,
>       [GUI] 기본 콘솔이 Xwindow ,그놈 같은 콘솔을 사용 할수 있다.
> /sbin/runlevel  라는 명령은 N 3 과 같이 현재 설정된 runlevel를 알려 줍니다.

그리고 runlevel이 선택 되면 각 레벨에 따라 설정된 프로그램 들이 로딩되게 됩니다.
이들은 각각 디렉 토리로 분류하여 스크립트로 정의 되어 있습니다.
/etc/rc.d/init.d 에는 부팅시 사용 할수 있는 전체 서비스 로딩 스크립트들이 있습니다.
그리고 /etc/rc1.d /etc/rc2.d /etc/rc3.d .... 등으로 runlevel 마다 별도의 디렉 토리가 있고
이들은 구동할수 있는  서비스들을 K10xfs -> ../init.d/xfs 와 같이 init.d의 파일을 링크해 두었습니다.

이 링크들을  관리 하는것이 조금 있다가 설명하게 될 ntsysv 또는 Setup 과  chkconfig  입니다.


나. About ntsysv ?   

리눅스 서비스 관리를 하게 되면 기본적으로  아래 3가지 툴을 알아야 한다.
그리고 이들을 이용해서 관리를 하기 위한 기본적인 지식을 경험 하다보면 아래 chkconfig 를 쓸 일이 생길 것입니다.

Setup    :Text Mode Setup Utility 로 아래 5가지 섹션을 관리 하는 메인이다.

1. Authentication configuration 
2. Firewall configuration
3. Mouse configuration
4. Network configuration
5. System services

netconfig :  위 setup의  4. Network configuration 와 같다
ntsysv    :   위 setup의  5. System services 와 같다

따라서 어렵게 여러게 알기 힘들면 setup 이라는게 있구나. 
ntsysv 같은 것은 일반 사용자들이 chkconfig를 쉽게 쓰도록 만든 프로그램이라고 보면 된다.

서론에 비해 내용이 별로 안된다. 먼저 메뉴얼을 설명 하고 실습을 해보자.

다. About chkconfig  ?

mysql 이나 apache 같은 사용자가  설치한 프로그램 들을 부팅시 자동으로  실행 하기 위해서 이 프로그램을 사용한다.

chkconfig 는  /etc/rc.dinit.d/ 에 명시 되지 않은 서비스는 설정이 안된다.
먼저  /etc/rc.d/init.d 에 해당 스크립트를 생성 하여 두어야 한다.

NAME                
chkconfig  -  updates  and queries runlevel information for system services

시스템 서비스의 runlevel 정보를 수정 하고 조회 하는 유틸리티이다.

SYNOPSIS

다음과 같은 옵션 들을 가지 고 있으며
chkconfig --list [name]
chkconfig --add name
chkconfig --del name
chkconfig [--level levels] name <on|off|reset>
chkconfig [--level levels] name

OPTIONS
--level levels
Specifies the run levels an operation should pertain to.

It  is given  as  a string of numbers from 0 to 7. For example, --level 35 specifies runlevels 3 and 5.

조회 수정 대상이 되는 runlevel을 지정 하는 옵션이다.
0 부터 7 까지 부여 할수 있고, 두개 이상일 경우 --level 35 라고 하면 runlevel 3 과 5를 동시에 지정한다는 의미이다.

--add name

This option adds a new  service  for  management  by  chkconfig.
새로운 서비스 entry 를 추가하기위한 옵션이다.

When  a new service is added, chkconfig ensures that the service
has either a start or a kill entry in  every  runlevel.

새로운 서비스가 추가 될때 모든 runlevel에서 start와 kill이 가능한지 점검하고

If  any  runlevel  is missing such an entry, chkconfig creates the appro-
priate entry as specified by the  default  values  in  the  init script.

chkconfig 는 init 스크립트에 다음과 같은 entry를 검사한다.
그리고 없는 것에 대해서 경고를 하게 된다.

# chkconfig: - 50 50
# description: init file for Apache2 server daemon
# processname: /usr/local/apache2/apachectl
# config:      /usr/local/apache2/conf/httpd.conf
# pidfile:     /usr/local/apache2/logs/httpd.pid

같은 부분이 필요하다.

--del name
The  service  is removed from chkconfig management, and any sym-
bolic links in /etc/rc[0-6].d which pertain to it are removed.

각 runlevel /etc/rc[0-6].d 에 속하는  해당 서비스 링크를 삭제 한다.

--list name
This option lists all of  the  services  which  chkconfig  knows
about, and whether they are stopped or started in each runlevel.
If name is specified, information in only display about  service name.


라.  사용 예제 [apache 자동 수행]

Apached 를 등록 하여 부팅시 자동 수행 되도록 해보자

step#0] chkconfig 로 서비스 확인 하기

chkconfig --list 를 해보면

아래와 같이 각 runlevel 에 설정된 서비스 목록 을 볼수 있다.
구동 하고자 하는 서비스의 등록 상태를 확인하자. 현재 apached 가 없다는 것을 확인 할 수 있다.

>> etc]# chkconfig --list

dkms_autoinstaller      0:off   1:off   2:on    3:off   4:on    5:on    6:off
kudzu           0:off   1:off   2:off   3:off   4:on    5:on    6:off
syslog          0:off   1:off   2:on    3:on    4:on    5:on    6:off
netfs           0:off   1:off   2:off   3:off   4:on    5:on    6:off
network         0:off   1:off   2:on    3:on    4:on    5:on    6:off
random          0:off   1:off   2:on    3:off   4:on    5:on    6:off
rawdevices      0:off   1:off   2:off   3:off   4:on    5:on    6:off
pcmcia          0:off   1:off   2:on    3:off   4:on    5:on    6:off
saslauthd       0:off   1:off   2:off   3:off   4:off   5:off   6:off
keytable        0:off   1:on    2:on    3:off   4:on    5:on    6:off
apmd            0:off   1:off   2:on    3:off   4:on    5:on    6:off
atd             0:off   1:off   2:off   3:off   4:on    5:on    6:off
gpm             0:off   1:off   2:on    3:off   4:on    5:on    6:off
autofs          0:off   1:off   2:off   3:off   4:on    5:on    6:off
iptables        0:off   1:off   2:on    3:off   4:on    5:on    6:off
irda            0:off   1:off   2:off   3:off   4:off   5:off   6:off
nscd            0:off   1:off   2:off   3:off   4:off   5:off   6:off
isdn            0:off   1:off   2:on    3:off   4:on    5:on    6:off
sshd            0:off   1:off   2:on    3:off   4:on    5:on    6:off
portmap         0:off   1:off   2:off   3:off   4:on    5:on    6:off
nfs             0:off   1:off   2:off   3:off   4:off   5:off   6:off
nfslock         0:off   1:off   2:off   3:off   4:on    5:on    6:off
sendmail        0:off   1:off   2:on    3:off   4:on    5:on    6:off
rhnsd           0:off   1:off   2:on    3:on    4:on    5:on    6:off
crond           0:off   1:off   2:on    3:on    4:on    5:on    6:off
anacron         0:off   1:off   2:on    3:off   4:on    5:on    6:off
xfs             0:off   1:off   2:on    3:off   4:on    5:on    6:off
xinetd          0:off   1:off   2:off   3:on    4:on    5:on    6:off
vsftpd          0:off   1:off   2:off   3:on    4:off   5:off   6:off
snmpd           0:off   1:off   2:off   3:on    4:off   5:off   6:off
raidmon         0:off   1:off   2:on    3:on    4:on    5:on    6:off
xinetd based services:
chargen-udp:    off
rsync:  on
chargen:        off
daytime-udp:    off
daytime:        off
echo-udp:       off
echo:   off
services:       off
servers:        off
time-udp:       off
time:   off
sgi_fam:        off
telnet: on

step#1] init 스크립트 생성하기

apache 나 mysql같은 프로그램이 구동 될수 있도록 init 스크립트를 만들어야 한다.
cp /etc/rc.d/init.d/sendmail /etc/rc.d/init.d/apached 라는 스크립트로 이름을 바꾸어 보자.

apached 라는 스크립트 에 apacehclt start 와 stop restart 를 쓸수 있도록  수정 하자.

>>apached 

#!/bin/bash
#
# chkconfig: - 50 50
# description: init file for Apache2 server daemon

# processname: /usr/local/apache2/apachectl
# config:      /usr/local/apache2/conf/httpd.conf
# pidfile:     /usr/local/apache2/logs/httpd.pid
#
# source function library
. /etc/rc.d/init.d/functions

# pull in sysconfig settings


RETVAL=0
prog="apaceh2"

# Some functions to make the below more readable
APACHE2_HOME=/usr/local/apache2
APACHED=$APACHE2_HOME/bin/apachectl

start()
{
# Create keys if necessary
echo -n $"Starting $prog :"
sh $APACHED start && success || failure
RETVAL=$?
return $RETVAL
}

stop()
{
echo -n $"Stopping $prog:"
sh  $APACHED stop && success || failure
RETVAL=$?
return $RETVAL
}


case "$1" in
start)
start
;;
stop)
stop
;;
restart)
stop
start
;;
*)
echo $"Usage: $0 {start|stop|restart}"
RETVAL=1
esac
exit $RETVAL


[주의 사항]

# chkconfig: - 50 50
# description: init file for Apache2 server daemon

이부분이 반드시 있어야 하며 없으면 service a does not support chkconfig 라는 에러가 난다.

root#>chomd 755 apached
> 실행이 가능 하도록 파일 권한을 변경 한다.

step#2-1] chkconfig 로 서비스 관리 하기

root#>chkconfig --add apached
라고 하면 모든 서비스 목록에 추가 될것이다.

root#>chkconfig --del apached
라고 하면 모든 서비스 목록에 삭제 될것이다.

root#>chkconfig --add apached
root]# chkconfig --list
>apached         0:off   1:off   2:off   3:off   4:off   5:off   6:off
라고 설정 된것을 확인 할수 있다.

root]# chkconfig --level 35 apached on
는  chkconfig --level 3 apached on 와 chkconfig --level 5 apached on 를 동시에 수행 한것과 같습니다.

root]# chkconfig --list
>apached         0:off   1:off   2:off   3:on    4:off   5:on    6:off

step#2-2]ntsysv 로 서비스 관리 하기

ntsysv로도 위의 내용을 변경 가능 하다.
다만 ntsysv의 경우  목록에 보이지 않으면 먼저 chkconfig --add NAME 로 해당 서비스를 각 runlevel에 등록 해야 한다.

root#> ntsysv --level 35
로 화면에서 apached를 '*' 로 선택(스페이스키) 하면 chkconfig --level 35 apached on 와 같은 결과를 같게 됩니다.

좋은정보가 되셨다면 아래 한번 클릭해주세요^^


댓글