개발자/라즈베리파이4

/etc/rc.local 파일 활성화 하는 방법, 자동 실행하도록 설정

지구빵집 2021. 8. 3. 10:27
반응형

 

 

/etc/rc.local 파일 활성화하는 방법, 자동 실행하도록 설정 

 

"/etc/rc.local"파일은 시스템이 부팅하고 나서 맨 마지막에 실행되는 스크립트입니다. 그래서 사용자가 부팅 시 자동 실행하는 프로그램을 설정할 때 여기에 넣어서 실행시킬 수 있습니다. 파일 내용을 보면 다음과 같습니다.

 

#!/bin/sh -e
#
# rc.local
#
# This script is executed at the end of each multiuser runlevel.
# Make sure that the script will "exit 0" on success or any other
# value on error.
#
# In order to enable or disable this script just change the execution
# bits.
#
# By default this script does nothing.

# Print the IP address
_IP=$(hostname -I) || true
if [ "$_IP" ]; then
printf "My IP address is %s\n" "$_IP"
fi

exit 0

 

 중간 부분에 보면 이런 내용이 있습니다.

 

# In order to enable or disable this script just change the execution
# bits.
#
# By default this script does nothing.

이 스크립트를 실행하려면 실행권한이 있어야하고 그리고 자체적으로 이 스크립트는 비활성화 되어있어서 사용하려면 따로 활성화 시키는 과정을 거쳐야 한다.

 

라즈베리파이 4  Model B+ 에서는 rc.local 파일이 자동으로 활성화되지 않아서 사용자가 해주어야 하는 방식으로 변경이 된 것 같습니다. $sudo systemctl status rc.local 명령으로 상태를 보면 아래와 같습니다. 주황색 글씨로 무언가 제대로 되지 않는 것처럼 보입니다. CTRL-C 키를 눌러 나옵니다. 같은 동작을 수행하는 명령어는 

 

$sudo systemctl status rc-local.service

 

pi@raspberrypi:~ $ sudo systemctl status rc.local
Warning: The unit file, source configuration file or drop-ins of rc-local.service changed on disk. Run 'systemctl d
● rc-local.service - /etc/rc.local Compatibility
   Loaded: loaded (/lib/systemd/system/rc-local.service; enabled-runtime; vendor preset: enabled)
  Drop-In: /usr/lib/systemd/system/rc-local.service.d
           └─debian.conf
           /etc/systemd/system/rc-local.service.d
           └─ttyoutput.conf
   Active: failed (Result: exit-code) since Mon 2021-07-26 18:33:09 KST; 29min ago
     Docs: man:systemd-rc-local-generator(8)
  Process: 480 ExecStart=/etc/rc.local start (code=exited, status=1/FAILURE)

 7월 26 18:33:01 raspberrypi systemd[1]: Starting /etc/rc.local Compatibility...
 7월 26 18:33:01 raspberrypi rc.local[480]: wlan0     Interface doesn't support scanning : Network is down
 7월 26 18:33:09 raspberrypi rc.local[480]: Failed to start hostapd.service: Unit hostapd.service is masked.
 7월 26 18:33:09 raspberrypi systemd[1]: rc-local.service: Control process exited, code=exited, status=1/FAILURE
 7월 26 18:33:09 raspberrypi systemd[1]: rc-local.service: Failed with result 'exit-code'.
 7월 26 18:33:09 raspberrypi systemd[1]: Failed to start /etc/rc.local Compatibility.
lines 1-17/17 (END)

 

우선 실행 권한을 아래와 같이 확인합니다. 권한은 다 들어가 있는데 혹시 설정을 하실 때는 $sudo chmod +x /etc/rc.d/rc.local 명령으로 수정합니다.

 

pi@raspberrypi:~ $ ls /etc/rc.local -l
-rwxr-xr-x 1 root root 1758  7월 26 18:27 /etc/rc.local
pi@raspberrypi:~ $

 

다음으로 "/usr/lib/systemd/system/rc-local.service" 파일을 수정합니다. 활성화를 위해서 꼭 필요한 절차라고합니다. 맨 아래에 다음 내용을 추가합니다. 다음과 같은 명령어로 파일을 열어 수정합니다.

 

pi@raspberrypi:~ $ sudo nano /usr/lib/systemd/system/rc-local.service

 

[Install]
WantedBy=multi-user.target

 

아래와 같이 가장 아래 코드가 변경되었습니다.

 

#  SPDX-License-Identifier: LGPL-2.1+
#
#  This file is part of systemd.
#
#  systemd is free software; you can redistribute it and/or modify it
#  under the terms of the GNU Lesser General Public License as published by
#  the Free Software Foundation; either version 2.1 of the License, or
#  (at your option) any later version.

# This unit gets pulled automatically into multi-user.target by
# systemd-rc-local-generator if /etc/rc.local is executable.
[Unit]
Description=/etc/rc.local Compatibility
Documentation=man:systemd-rc-local-generator(8)
ConditionFileIsExecutable=/etc/rc.local
After=network.target

[Service]
Type=forking
ExecStart=/etc/rc.local start
TimeoutSec=0
RemainAfterExit=yes
GuessMainPID=no

[Install]
WantedBy=multi-user.target

 

다음 서비스를 실행합니다. 재부팅 시에도 활성화 되도록 아래 명령어를 입력합니다.

 

$sudo systemctl enable rc-local.service

 

설정이 제대로 되었는지 확인합니다.

 

$sudo systemctl list-unit-files | grep rc.local

 

아래는 참고 코드이고, rc.local 파일에서 hostapd.service 에서 에러가 나서 수정하는 과정을 보여주는 코드라서 참고하실 필요는 없습니다. ^^

 

pi@raspberrypi:~ $  systemctl status rc-local.service
● rc-local.service - /etc/rc.local Compatibility
   Loaded: loaded (/lib/systemd/system/rc-local.service; enabled; vendor preset: enabled)
  Drop-In: /usr/lib/systemd/system/rc-local.service.d
           └─debian.conf
           /etc/systemd/system/rc-local.service.d
           └─ttyoutput.conf
   Active: failed (Result: exit-code) since Mon 2021-07-26 19:12:49 KST; 2min 16s ago
     Docs: man:systemd-rc-local-generator(8)
  Process: 1248 ExecStart=/etc/rc.local start (code=exited, status=1/FAILURE)

 7월 26 19:12:40 raspberrypi systemd[1]: Starting /etc/rc.local Compatibility...
 7월 26 19:12:49 raspberrypi rc.local[1248]: Failed to start hostapd.service: Unit hostapd.service is masked.
 7월 26 19:12:49 raspberrypi systemd[1]: rc-local.service: Control process exited, code=exited, status=1/FAILURE
 7월 26 19:12:49 raspberrypi systemd[1]: rc-local.service: Failed with result 'exit-code'.
 7월 26 19:12:49 raspberrypi systemd[1]: Failed to start /etc/rc.local Compatibility.
pi@raspberrypi:~ $ ^C
pi@raspberrypi:~ $ sudo systemctl enable rc-local.service
pi@raspberrypi:~ $ systemctl list-unit-files | grep rc.local
rc-local.service                       enabled
rc.local.service                       enabled
pi@raspberrypi:~ $
pi@raspberrypi:~ $
pi@raspberrypi:~ $ sudo systemctl unmask hostapd
Removed /etc/systemd/system/hostapd.service.
pi@raspberrypi:~ $ sudo systemctl enable hostapd
Synchronizing state of hostapd.service with SysV service script with /lib/systemd/systemd-sysv-install.
Executing: /lib/systemd/systemd-sysv-install enable hostapd
pi@raspberrypi:~ $ sudo systemctl start hostapd
pi@raspberrypi:~ $ sudo systemctl enable rc-local.service
pi@raspberrypi:~ $

 

항상 조심스러웠고, 가는 곳 모두가 좋았다. 과천 7단지, 논현동, 도곡동 도심, 판교, 창업보육센터, 상적동, 학교까지 지나고 나서 보니까 좋았던 게 아니라 바로 그때 좋았다. 모든 곳이 처음이었고, 무엇보다 아름다운 곳이었다. 적어도 어디서든 풀죽은 목소리를 내지 않았고, 해 본적이 없다는 말도 하지 않았고, 자신있고 용기있게 덤볐다. 아마도 앞으로도 그럴 것이다. 

 

-끝

 

미스터 장이 준 장미꽃

 

 

 

반응형