连接监视器时运行脚本

当我将外接显示器连接到笔记本电脑时,我正在尝试运行位于usr/local/bin/的脚本。 我试图添加一个新的udev规则,但这不起作用。 我在/etc/udev/rules.d创建了一个名为vga-monitor-connect.rules的新文件。 该文件的内容是

 SUBSYSTEM=="drm", ACTION=="change", RUN+="/usr/local/bin/panel-fix" 

我接受了这个答案

在线搜索后,我也尝试了以下规则

 KERNEL=="card0", SUBSYSTEM=="drm", ENV{DISPLAY}=":0", ENV{XAUTHORITY}="/home/rumesh/.Xauthority", RUN+="/usr/local/bin/panel-fix" 

然而,这也没有用。

我已经手动运行脚本,我可以确认它是有效的,所以它不是我的脚本的问题。

我也想明确表示我对udev了解不多,所以我使用的规则可能是错的。 如果有人知道我的问题的正确规则,请留下答案。

我的显卡是英特尔GM965集成芯片组

如果连接或断开屏幕,则运行命令的另一种方法

另一种解决方案是运行一个小的后台脚本。 在后台运行下面的脚本,我无法测量处理器负载的任何增加。

无论何时连接或断开第二个屏幕,都可以轻松方便地运行脚本或任何其他命令。

示例脚本

  • 只需检查字符串“连接”出现在命令xrandr的输出中的每五秒钟多少次(注意“连接”后的空格以防止与“断开连接”的错误匹配)。 每次出现代表一个连接的屏幕。
  • 如果出现次数发生变化,则连接或断开屏幕。 脚本“注意到”更改并且可以连接到命令,您可以在脚本的head部分进行设置。

剧本

 #!/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 

如何使用

  1. 将脚本复制到空文件中,将其另存为connect_screen.py
  2. 在head部分中,将命令设置为在connect上运行(我将“gedit”设置为示例,请注意引号)。 同样,也可以在断开连接时设置命令。 否则请将disconnect_command = ""保留原样。

    如果您使用disconnect-命令,也要取消注释该行:

     run_command(disconnect_command) 

    并注释掉该行:

     pass 

    如脚本中所示

  3. 从终端测试脚本,连接屏幕,看看是否一切正常。
  4. 如果一切正常,请将其添加到启动应用程序:Dash> Startup Applications>添加命令:

     /bin/bash -c "sleep 15&&python3 /path/to/connect_screen.py" 

    sleep 15是在脚本开始运行之前使桌面完全启动。 只想确认一下。


编辑

如何以“智能”方式启动脚本。

sleep 15中断sleep 15应该可以正常工作,但由于每个系统的启动时间不同,可能需要进行一些实验才能找到正确的rest时间。 通过少量添加,脚本变为“智能”,并在启动实际脚本之前等待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