[other] shell脚本开机自启动异常
Tofloor
poster avatar
Free_Aaron.Li
deepin
2024-03-31 02:24
Author

hello,我最近写了一个脚本用于开机自动选择键盘,内容如下:

#!/bin/bash

# 笔记本默认键盘,通常均为该名
DEFAULT_KEYBOARD_DEV='AT Translated Set 2 keyboard'
# 外设键盘
PERIPHERAL_KEYBOARD_DEV='MIIIW MIIIW 2.4G Receiver Keyboard'
# 外设键盘简称
PERIPHERAL_KEYBOARD_FOR_SHORT='MIIIW ART series'

# 设置设备状态
function setStatus(){
  # 获取传入设备的id,设备id往往不止有一个
	IDS=`xinput list | grep "$2" | awk  -F 'id=' '{print $2}' | awk '{print $1}' `
  # 最终执行命令
	for elem in $IDS 
	do 
		xinput $1 $elem
	done
}

# 外设键盘是否插入
EXIST_PERIPHERAL_KEYBOARD=`xinput list | grep "$PERIPHERAL_KEYBOARD_DEV"`

# 判断该选择哪个键盘
if [ "" != "$EXIST_PERIPHERAL_KEYBOARD" ]
then
  # 若外设键盘存在,则关闭笔记本键盘,并开启外接键盘
	setStatus --disable $DEFAULT_KEYBOARD_DEV
	setStatus --enable $PERIPHERAL_KEYBOARD_DEV
  echo "已禁用默认键盘,启动\"$PERIPHERAL_KEYBOARD_FOR_SHORT\"键盘。"
else
  # 若外设键盘不存在,则开启笔记本键盘,并关闭外接键盘
	setStatus --enable $DEFAULT_KEYBOARD_DEV
	setStatus --disable $PERIPHERAL_KEYBOARD_DEV
  echo "未检测到外设键盘,启动默认键盘。"
fi

exit 0

同时,编写的systemd开机自启动服务keyboard.service内容如下:

[Unit]
Description=Auto select keyboard
After=graphical.target

[Service]
Type=simple
WorkingDirectory=/home/aaron/.dotfiles/system/
ExecStart=/home/aaron/.dotfiles/system/keyboard.sh
User=aaron
RemainAfterExit=yes

[Install]
WantedBy=multi-user.target

但是 当开机时,并不能成功运行。服务信息如下:

● keyboard.service - Auto select keyboard
     Loaded: loaded (/etc/systemd/system/keyboard.service; enabled; preset: enabled)
     Active: active (exited) since Sun 2024-03-31 02:17:13 CST; 11s ago
    Process: 26144 ExecStart=/home/aaron/.dotfiles/system/keyboard.sh (code=exited, status=0/SUCCESS)
   Main PID: 26144 (code=exited, status=0/SUCCESS)
        CPU: 18ms

3月 31 02:17:13 aaron-PC systemd[1]: Started keyboard.service - Auto select keyboard.
3月 31 02:17:13 aaron-PC keyboard.sh[26146]: Unable to connect to X server
3月 31 02:17:13 aaron-PC keyboard.sh[26150]: Unable to connect to X server
3月 31 02:17:13 aaron-PC keyboard.sh[26157]: Unable to connect to X server
3月 31 02:17:13 aaron-PC keyboard.sh[26144]: 未检测到外设键盘,启动默认键盘。

请问各位大佬知道什么原因吗?

Reply Favorite View the author
All Replies
187******07
deepin
2024-03-31 04:37
#1

不知道怎么等待检测到键盘,但粗暴一点,在脚本里先sleep 1分钟,再执行后面的。。。

Reply View the author
Tent
deepin
2024-03-31 12:24
#2

可以试试把这个服务,放到lightdm服务之后启动

Reply View the author
Free_Aaron.Li
deepin
2024-03-31 16:31
#3

我换了一种开机自启动方式,既然它提示我“Unable to connect to X server” 。那么我就选择在图形界面已经启动后再启动脚本。

我选择在 /etc/xdg/autostart 目录下建立一个 keyboard.desktop文件,再在文件中写入:

[Desktop Entry]
Name=Keyboard
Exec=/home/aaron/.dotfiles/system/keyboard.sh
Type=Application
Reply View the author
Tent
deepin
2024-03-31 21:13
#4
Free_Aaron.Li

我换了一种开机自启动方式,既然它提示我“Unable to connect to X server” 。那么我就选择在图形界面已经启动后再启动脚本。

我选择在 /etc/xdg/autostart 目录下建立一个 keyboard.desktop文件,再在文件中写入:

[Desktop Entry]
Name=Keyboard
Exec=/home/aaron/.dotfiles/system/keyboard.sh
Type=Application

这种应该是只能登录之后才能执行吧?在登录界面应该不生效?

Reply View the author
Free_Aaron.Li
deepin
2024-03-31 22:45
#5
Tent

这种应该是只能登录之后才能执行吧?在登录界面应该不生效?

对,但是并没有大碍,仅在开机登陆时所有键盘可用,后续还是依据脚本自定义键盘。

Reply View the author