我在 /etc/systemd/system/microphone_disable_startup.service 中有一个自定义服务
[Unit]
Description=Disable microphone on startup
After=alsa-restore.service
[Service]
ExecStart=/opt/bin/check_mic_startup
执行的脚本是位于 /opt/bin 中的 check_mic_startup
#!/bin/bash
# DEBUG: touch /home/GeorgeMP/Desktop/testfile_works
i_toggle=$(amixer get Capture toggle | grep -c "\[on\]")
if [ $i_toggle = 2 ] ; then
amixer -q set Capture toggle
fi
amixer -q set Capture 0%
echo Microphone turned OFF
exit 0
如您所见,我尝试使用 touch 命令调试服务,它运行完美,甚至在服务使用时也是如此。问题在于脚本中更复杂的命令。
对我的自定义服务执行 systemctl status,我可以检查服务是否成功且没有错误。
microphone_disable_startup.service - Disable microphone on startup
Loaded: loaded (/etc/systemd/system/microphone_disable_startup.service; enabled; vendor preset: enabled)
Active: inactive (dead) since Tue 2020-12-01 17:36:39 CET; 8min ago
Process: 610 ExecStart=/opt/bin/check_mic_startup (code=exited, status=0/SUCCESS)
Main PID: 610 (code=exited, status=0/SUCCESS)
Dec 01 17:36:39 GeorgeMP systemd[1]: Started Disable microphone on startup.
Dec 01 17:36:39 GeorgeMP systemd[1]: microphone_disable_startup.service: Succeeded.
但是,重启/关闭后,即使执行成功,命令也没有执行,或者没有任何效果,因为
amixer get Capture
Simple mixer control 'Capture',0
Capabilities: cvolume cswitch
Capture channels: Front Left - Front Right
Limits: Capture 0 - 63
Front Left: Capture 63 [100%] [30.00dB] [on]
Front Right: Capture 63 [100%] [30.00dB] [on]
They are 100% and turned [on].
它们应该是 0% 和 [off]
如果我从终端执行 /opt/bin/check_mic_startup,它运行完美。即使我执行 systemctl start microphone_disable_startup.service,它仍然无法正常工作。当我的自定义服务 microphone_disable_startup.service 使用时,它无法正常工作。问题是什么?
编辑1:
我将脚本更改为此以包含 /opt/bin 目录
#!/bin/bash
# DEBUG: touch /home/GeorgeMP/Desktop/testfile_works
# USE THESE IF NEEDED SOMEWHERE ELSE
#DBUS_SESSION_BUS_ADDRESS="unix:path=/run/user/1000/bus"
#DISPLAY=":0.0"
export PATH="$PATH:/opt/bin"
i_toggle=$(/usr/bin/amixer get Capture toggle | grep -c "\[on\]")
if [ $i_toggle = 2 ] ; then
/usr/bin/amixer -q set Capture toggle
fi
/usr/bin/amixer -q set Capture 0%
echo Microphone turned OFF
env > /home/GeorgeMP/Desktop/environment
exit 0
经过检查,每个命令都可以通过新环境中的脚本访问。
顺便说一下,我在桌面上创建的环境文件是
LC_ADDRESS=en_US.UTF-8
LC_NAME=en_US.UTF-8
LC_MONETARY=en_US.UTF-8
PWD=/
LC_PAPER=en_US.UTF-8
LANG=en_US.UTF-8
INVOCATION_ID=549485b12a4149bb889e55d42711e923
LC_IDENTIFICATION=en_US.UTF-8
SHLVL=1
LC_TELEPHONE=en_US.UTF-8
LC_MEASUREMENT=en_US.UTF-8
LC_TIME=en_US.UTF-8
JOURNAL_STREAM=9:26568
PATH=/usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin:/sbin:/bin:/snap/bin:/opt/bin
LC_NUMERIC=en_US.UTF-8
_=/usr/bin/env
因此它包含 /opt/bin 路径。它仍然不起作用...但是当我在终端中运行脚本时它起作用了...