시작 시 명령 실행

시작 시 명령 실행

나는 팔로우하고 있다이 튜토리얼에서는 휴대전화의 가속도계를 사용하는 방법을 설명합니다..

이것이 작동하려면 부팅할 때마다 세 가지 명령을 실행해야 합니다.

rfkill unblock bluetooth
killall bluetoothd
hciconfig hci0 up

매번 수동으로 수행하는 대신 시작할 때 스크립트를 사용하여 이 작업을 수행할 수 있는 방법이 있습니까?

답변1

대부분의 시스템은 /etc/rc.local시작 시 자동으로 이를 로드하므로 거기에 명령을 입력하기만 하면 됩니다.

루트로 실행 가능한지 확인해야 하지만 이미 실행 가능할 수도 있습니다.

답변2

시스템이 systemd를 사용하는 경우 시스템이 시작될 때 명령을 한 번 실행하는 일회성 서비스를 작성할 수 있습니다.

먼저 파일을 만들고 /opt/scripts/configure-bluetooth.sh다음 명령을 입력합니다.

#!/bin/bash
rfkill unblock bluetooth
killall bluetoothd
hciconfig hci0 up

그리고 파일을 실행 가능하게 만듭니다.chmod +x /opt/scripts/configure-bluetooth.sh

다음을 포함하는 새 서비스 단위를 만듭니다 /etc/systemd/system/configure-bluetooth.service.

[Unit]
Description=Configure bluetooth

[Service]
Type=oneshot
ExecStart=/opt/scripts/configure-bluetooth.sh

[Install]
WantedBy=multi-user.target

이제 systemctl daemon-reloadsystemd가 새 서비스를 감지하도록 실행해야 합니다. 테스트하려면 를 실행하세요 systemctl start configure-bluetooth.service.

작동한다고 확신하면 시작 시 활성화할 수 있습니다.systemctl enable configure-bluetooth.service

관련 정보