Ubuntu 16.04 시작 서비스 설정
Ubuntu 16.04에서 시작 서비스를 등록하는 방법을 알아보자
스크립트 작성
/etc/init.d 경로에 시작할 스크립트의 경로를 작성한다. 서비스를 시작할 때 이 스크립트로 프로세스를 실행한다. 아래는 예시다.
### BEGIN INIT INFO
# Provides: scriptname
# Required-Start: $remote_fs $syslog
# Required-Stop: $remote_fs $syslog
# Default-Start: 2 3 4 5
# Default-Stop: 0 1 6
# Short-Description: Start daemon at boot time
# Description: Enable service provided by daemon.
### END INIT INFO
PATH_TO_APP = /home/apps/app.jar
case $1 in
start)
java -jar $PATH_TO_APP --server.port=8888
;;
stop)
sudo fuser 8888/tcp -k || true
;;
restart)
sudo fuser 8888/tcp -k || true
java -jar $PATH_TO_APP --server.port=8888
;;
*) echo "Run as $0 <start|stop|restart>"; exit 1;;
esac
위 스크립트에서 ### BEGIN INIT INFO ... ### END INIT INFO 사이의 정보들은 run-time dependecy인데, 서비스의 메타데이터라고 생각하면 된다. 이 정보를 작성하는 방법은 아래의 LSB init script에 대한 문서를 참고하자.
스크립트 권한 부여
스크립트 파일을 작성했으면 권한을 부여하자.
chmod 755 /etc/init.d/testservice
서비스 등록
마지막으로 서비스를 등록하면 애플리케이션이 부팅시 자동으로 실행된다.
update-rc.d tomcat7 defaults
서비스 수동 실행 및 상태 확인
서비스를 수동으로 실행하는 방법은 systemctl 명령어를 이용하면 된다.
systemctl start testservice
start 대신 stop이나 restart를 옵션으로 주면 서비스가 멈추거나 재시작한다.
만약 서비스의 상태를 확인하고 싶다면 아래의 옵션으로 systemctl 명령어를 실행하면 된다.
systemctl list-units --type service --all
참고
http://wiki.debian.org/LSBInitScripts
LSBInitScripts - Debian Wiki
Translation(s): none How to LSBize an Init Script Note: Debian discontinued LSB support in 2015, see DebianLsb and for example lwn.net: Debian dropping the Linux Standard Base. This does not affect the Debian requirement to include LSB style dependency inf
wiki.debian.org