rc.local 활성화

2021. 7. 8. 11:36IT/Linux

문서 하단에 내용 요약 되어있음.

 

먼저, rc-local이 실행 중인지 확인 하자.

$ systemctl status rc-local

    ● rc-local.service - /etc/rc.d/rc.local Compatibility
       Loaded: loaded (/usr/lib/systemd/system/rc-local.service; static; vendor preset: disabled)
       Active: inactive (dead)

 

rc-local이 활성화 되어있지 않아 시작해보려 했지만 오류가 발생했다.

$ sudo systemctl start rc-local

    ● rc-local.service - /etc/rc.d/rc.local Compatibility
       Loaded: loaded (/usr/lib/systemd/system/rc-local.service; static; vendor preset: disabled)
       Active: inactive (dead)
    Condition: start condition failed at 목 2021-07-08 09:32:08 KST; 5s ago
               ConditionFileIsExecutable=/etc/rc.d/rc.local was not met

 

rc.local 파일에 실행 권한이 없어 실행 권한을 추가했다.

$ ll /etc/rc.local
    lrwxrwxrwx. 1 root root 13  2월 29  2020 /etc/rc.local -> rc.d/rc.local
$ ll /etc/rc.d/rc.local
    -rw-r--r--. 1 root root 553  6월 28 16:54 /etc/rc.d/rc.local

$ sudo chmod u+x /etc/rc.d/rc.local
$ ll /etc/rc.d/rc.local
    -rwxr--r--. 1 root root 553  6월 28 16:54 /etc/rc.d/rc.local

 

권한을 부여하고 rc-local 상태를 확인해보니 정상적으로 실행되었다.

$ sudo systemctl start rc-local
$ systemctl status rc-local
    ● rc-local.service - /etc/rc.d/rc.local Compatibility
       Loaded: loaded (/usr/lib/systemd/system/rc-local.service; static; vendor preset: disabled)
       Active: active (running) since 목 2021-07-08 09:43:14 KST; 7s ago
      Process: 26652 ExecStart=/etc/rc.d/rc.local start (code=exited, status=0/SUCCESS)
     Main PID: 26671 (java)
       Memory: 86.6M
       CGroup: /system.slice/rc-local.service
               ├─26671 /wagent/java/jre1.6.0_45/bin/java -Xms16m -Xmx16m -Djava.library.path=/wagent/lib -jar /wagent/WShield-M2.jar
               ├─26704 /wagent/java/jre1.6.0_45/bin/java -Xms128m -Xmx128m -Djava.library.path=/wagent/lib -jar /wagent/WShield-M.jar &
               └─26746 /wagent/WShield-RN.sh "/wagent" &

 

rc-local 자동 실행을 위해 enable을 시도 했더니 아래와 같은 메시지가 발생하고 'enable'이 아니라 'enable-runtime' 상태로 변경된 것을 확인 했다.

$ sudo systemctl enable rc-local
    The unit files have no installation config (WantedBy, RequiredBy, Also, Alias
    settings in the [Install] section, and DefaultInstance for template units).
    This means they are not meant to be enabled using systemctl.
    Possible reasons for having this kind of units are:
    1) A unit may be statically enabled by being symlinked from another unit's
       .wants/ or .requires/ directory.
    2) A unit's purpose may be to act as a helper for some other unit which has
       a requirement dependency on it.
    3) A unit may be started when needed via activation (socket, path, timer,
       D-Bus, udev, scripted systemctl call, ...).
    4) In case of template units, the unit is meant to be enabled with some
       instance name specified.

$ systemctl list-unit-files | grep rc.local
    rc-local.service                           enabled-runtime

 

rc-local 서비스 설정에 '[install]' 섹션 설정을 해야한다.

→ 이 내용은 따로 자세하게 확인해야겠다.

섹션 설정 후에 다시 enable하니 정상적으로 작동한다.

/usr/lib/systemd/system/rc-local.service
    ---------------------------------------------------------------
    # 아래 내용 추가
    [Install]
    WantedBy=multi-user.target
    ---------------------------------------------------------------
$ sudo systemctl enable rc-local
    Created symlink /etc/systemd/system/multi-user.target.wants/rc-local.service → /usr/lib/systemd/system/rc-local.service.

 

여기까지 설정한 후에도 작동하지 않는 스크립트는 해당 스크립트의 실행권한이 없는 것이다. 파일 소유자에 권한을 부여하고, su 명령어로 사용자를 지정하여 사용하도록 하자.

/etc/rc.local
    ---------------------------------------------------------------
    su user -c "/home/user/startup.sh"
    ---------------------------------------------------------------

# 테스트를 위한 스크립트
/home/user/startup.sh
    ---------------------------------------------------------------
    DATE=`date +%Y-%m-%d_%H:%M:%S`
    touch /home/user/$DATE.txt
    ---------------------------------------------------------------

# rc.local 파일을 실행하거나, 서버를 재부팅하여 확인할 수 있다.
$ ll
합계 36056
-rw-r--r--. 1 user user        0  7월  8 11:08 2021-07-08_11:08:30.txt
-rwxrw-r--. 1 user user       60  7월  8 11:01 startup.sh

 

- '[install]' 섹션 설정 관련 참고 사이트

 

How to Enable /etc/rc.local with Systemd - LinuxBabe

If you are running a Linux distro that uses Systemd, then you may find that your command in /etc/rc.local file would not run at system boot time. This guide explains how to enable /etc/rc.local script to run on system startup. Enable /etc/rc.local on Syste

www.linuxbabe.com

 

요약

1. rc-local 환경 설정 및 서비스 실행

2. 스크립트 실행에 필요한 적절한 권한 설정

  • rc-local 환경 설정 및 서비스 실행
/usr/lib/systemd/system/rc-local.service
    ---------------------------------------------------------------
  # 아래 내용 추가
    [Install]
    WantedBy=multi-user.target
    ---------------------------------------------------------------
$ sudo systemctl start rc-local
$ sudo systemctl enable rc-local
  • 스크립트에 실행에 필요한 적절한 권한 설정
/etc/rc.local
    ---------------------------------------------------------------
    su user -c "/home/user/startup.sh"
    ---------------------------------------------------------------

$ chmod u+x /home/user/startup.sh