화면이 연결되거나 연결이 끊어진 경우 명령을 실행하는 다른 방법
대안적인 해결책은 작은 백그라운드 스크립트를 실행하는 것입니다. 백그라운드에서 아래 스크립트를 실행하면 프로세서로드가 증가했는지 측정 할 수 없었습니다.
두 번째 화면이 연결되거나 연결이 끊어 질 때마다 스크립트 나 다른 명령을 실행하는 편리한 방법입니다.
예제 스크립트
- 명령 출력에서 "connected"문자열이 몇 번 나오는지 5 초마다 점검합니다 ( "connected"
xrandr뒤에 공백이 있으면 "disconnected"와 일치하지 않도록 방지). 각 발생은 연결된 화면을 나타냅니다.
- 발생 수가 변경되면 화면이 연결되었거나 연결이 끊어진 것입니다. 변경 사항은 스크립트에 의해 "감지"되며 명령에 연결될 수 있으며 스크립트의 헤드 섹션에서 설정할 수 있습니다.
스크립트
#!/usr/bin/env python3
import subprocess
import time
#--- set both commands (connect / disconnect) below
connect_command = "gedit"
disconnect_command = ""
#---
def get(cmd): return subprocess.check_output(cmd).decode("utf-8")
# - to count the occurrenc of " connected "
def count_screens(xr): return xr.count(" connected ")
# - to run the connect / disconnect command(s)
def run_command(cmd): subprocess.Popen(["/bin/bash", "-c", cmd])
# first count
xr1 = None
while True:
time.sleep(5)
# second count
xr2 = count_screens(get(["xrandr"]))
# check if there is a change in the screen state
if xr2 != xr1:
print("change")
if xr2 == 2:
# command to run if connected (two screens)
run_command(connect_command)
elif xr2 == 1:
# command to run if disconnected (one screen)
# uncomment run_command(disconnect_command) to enable, then also comment out pass
pass
# run_command(disconnect_command)
# set the second count as initial state for the next loop
xr1 = xr2
사용하는 방법
- 스크립트를 빈 파일로 복사하여 다른 이름으로 저장하십시오.
connect_screen.py
head 섹션에서 connect에서 실행되도록 명령을 설정합니다 (예를 들어 "gedit"를 따옴표로 묶었습니다). 또한 마찬가지로 연결 해제시 명령을 설정할 수 있습니다. 그렇지 않으면 그대로 둡니다 disconnect_command = "".
disconnect- 명령을 사용하는 경우 다음 줄도 주석 해제하십시오.
run_command(disconnect_command)
라인을 주석 처리하십시오.
pass
스크립트에 표시된대로
- 터미널에서 스크립트를 테스트 실행하고 화면을 연결 한 후 모든 것이 제대로 작동하는지 확인하십시오.
모두 제대로 작동하면 시작 응용 프로그램에 추가하십시오 : Dash> Startup Applications> 명령 추가 :
/bin/bash -c "sleep 15&&python3 /path/to/connect_screen.py"
는 sleep 15스크립트가 실행을 시작하기 전에 바탕 화면이 완전히 시작하는 것입니다. 그냥 확인하십시오.
편집하다
"스마트 한"방식으로 시작시 스크립트를 실행하는 방법
일반적으로 중단 sleep 15은 작동해야하지만 시작 시간은 시스템마다 다르므로 올바른 중단 시간을 찾으려면 약간의 실험이 필요할 수 있습니다. 약간만 추가하면 스크립트는 "스마트" xrandr가되고 실제 스크립트를 시작하기 전에 명령이 성공하기를 기다립니다 . 아래 버전을 사용하는 경우 다음 명령 만 추가하면됩니다.
python3 /path/to/connect_screen.py
시작 응용 프로그램에. 추가 사용법은 위 버전과 동일합니다.
스크립트
#!/usr/bin/env python3
import subprocess
import time
#--- set both commands (connect / disconnect) below
connect_command = "gedit"
disconnect_command = ""
#---
while True:
time.sleep(5)
try:
subprocess.Popen(["xrandr"])
except:
pass
else:
break
# function to get the output of xrandr
def get(cmd): return subprocess.check_output(cmd).decode("utf-8")
# - to count the occurrenc of " connected "
def count_screens(xr): return xr.count(" connected ")
# - to run the connect / disconnect command(s)
def run_command(cmd): subprocess.Popen(["/bin/bash", "-c", cmd])
# first count
xr1 = None
while True:
time.sleep(5)
# second count
xr2 = count_screens(get(["xrandr"]))
# check if there is a change in the screen state
if xr2 != xr1:
if xr2 == 2:
# command to run if connected (two screens)
run_command(connect_command)
elif xr2 == 1:
# command to run if disconnected (one screen)
# uncomment run_command(disconnect_command) to enable, then also comment out pass
pass
# run_command(disconnect_command)
# set the second count as initial state for the next loop
xr1 = xr2