我希望当程序potify-tui 关闭时,服务会终止spotifyd。你会如何编写这样的服务?
答案1
本质上是这样的:
while true; do
if ! pgrep spotify-tui; then
pkill spotifyd
exit
fi
sleep 10
done
然后放入您喜欢的任何后台服务。
答案2
您可以使用看门狗来验证程序是否冻结。看看
答案3
这就是我创建服务的方式:
〜/.local/share/systemd/spotifyd-autokill.service- 调用守护进程
[Unit]
Description=spotifyd auto-killer (when spotify-tui is not running)
[Service]
# Full path here is required
ExecStart="/home/zeioth/.local/share/systemd/user/daemons/spotifyd-autokill.sh"
[Install]
WantedBy=multi-user.target
〜/.local/share/systemd/user/daemons/spotifyd-autokill.sh- 做实际工作
#!/bin/bash
while true; do
if `ps -ef | grep "alacritty --title spotify-tui" | awk {'print $2" "$8'} | grep -v grep > /dev/null`; then
echo "Spotify-tui is running: No action taken."
else
echo "Spotify-tui is not running: Closing spotifyd."
killall -9 "spotifyd" 2> /dev/null
fi
sleep 120
done
〜/.local/bin/spotify-tui- 当您打开 Spotify-tui 时启动 Spotify
spotifyd
alacritty --title 'spotify-tui' --config-file ~/.config/alacritty/monokai.yml -e zsh -c 'source ~/.zshrc && cd ~/workspaces/git-forks/spotify-tui && cargo run' &