×

    Ubuntu 16.04 system service 등록하기

    Ubuntu 16.04 system service를 등록해 보자

    Background

    machine에서 server를 실행시키기 위해서는 system 동작 시에 자동으로 해당 server application이 실행될 필요가 있다.

    예를 들어 cloud9이나 jupyter notebook을 ubuntu server에 설치하고 외부에서 사용한다고 가정해보자.

    설치한 뒤에 수동으로 실행시켜도 되지만, 그러면 system을 reboot 한 뒤에 또 수동으로 동작시켜 주어야만 한다.

    system에 service로 등록시켜 system 시작 시에 자동으로 동작시키면 이런 수고를 덜 수 있다.

    Ubuntu 16.04를 기준으로 정리해 본다. jupyter notebook을 예로 든다.

    ※ 정확히는 모르겠지만 14.04 때는 이런 방법을 사용하였는데, 16.04부터는 달라진 듯하다.

    Overview

    1. service file 만들기
    2. service 등록하기

    Step by Step

    service file 만들기

    /etc/systemd/system/ 위치에 원하는 service file을 만들고 다음과 같은 내용을 넣어 준다.

    service의 이름이 jupyterNotebook라면 file이름은 jupyterNotebook.service으로 한다.

    # bash
    sudo nano /etc/systemd/system/jupyterNotebook.service
    
    [Unit]
    Description=Jupyter Notebook Server
    
    [Service]
    Type=simple
    User=<username>
    ExecStart=/home/<username>/.local/bin/jupyter-notebook
    WorkingDirectory=/your/working/dir
    
    [Install]
    WantedBy=multi-user.target
    

    <username> 등을 포함해서 해당 내용은 자신의 환경에 맞게 적당히 바꾸어야 한다.

    service 등록하기

    등록 방법은 간단하다. 다음을 bash에서 실행해 주면 된다.

    # bash
    systemctl daemon-reload
    systemctl enable jupyterNotebook
    systemctl start jupyterNotebook
    

    service 상태를 알고 싶다면 다음과 같이 확인해 볼 수 있다.

    # bash
    sudo systemctl status jupyterNotebook
    

    설정 등을 바꾸어서 다시 시작해야 할 때는 다음과 같이 한다.

    # bash
    sudo systemctl restart jupyterNotebook
    

    멈추거나 등록을 해제하고 싶다면 다음과 같이 하면 된다.

    #bash
    sudo systemctl stop jupyterNotebook
    sudo systemctl disable jupyterNotebook
    

    services 확인하기

    service 전체 목록을 보기 위해서는 아래 명령을 실행 시킨다.

    # show all services that has been loaded at boot and are active now
    systemctl list-units --type service
    
    # all services no matter they are active or not
    systemctl list-units --type service --all
    

    Summary

    어떻게 작동하는지, service file은 어떤 의미인지 정확하게는 알 수 없지만, 일단 된다.

    14.04에서 쓰던 방법은 실패하는경우도 많았는데, 이 방법은 아직 그런 예가 없다.

    REF

    ... ... ... ...
    Back