如何使用 yad 終止應用程式

如何使用 yad 終止應用程式

我想使用 yad 系統托盤通知來終止應用程式(例如 vlc),我使用了以下程式碼:

yad --notification --no-middle --image=emblem-warning --menu='Cancel ! kill $pid' --text="My Tooltip" &

vlc & pid=${!}

程式碼給出以下錯誤:

kill: failed to parse argument: '$pid'

任何幫助表示讚賞

答案1

跑步vlc 第一的將其 PID 正確地放入變數中,$pid如下所示:

vlc & pid=${!}

"然後在參數周圍使用雙引號(不是單引號') 允許$pid像這樣的參數擴展:

yad --notification --no-middle --image=emblem-warning --menu="Cancel ! kill $pid" --text="My Tooltip" &

答案2

該程式碼的目標是啟動 ffmpeg 來調整視訊大小和編碼,並能夠使用 yad 關閉它。我透過將進程的 PID 儲存在臨時檔案中找到了解決方案。我花了很長時間嘗試在 bash -c 'command' 中使用單引號。我最終通過將單引號替換為解決了這個問題 '"'"'

export TMP="$(mktemp)"
yad --notification --no-middle  --command="" --image=emblem-warning --menu='Cancel current job ! bash -c "kill  $(cat $TMP); rm $TMP;"' & pid=$!
export fname="video.mp4"
bash -c 'echo $$>$TMP; exec ffmpeg -n -loglevel warning -i "$fname" -acodec copy -vcodec libx264 -crf 24 -vf "scale='"'"'if(gte(iw,ih),-1,720)'"'"':'"'"'if(gte(iw,ih),720,-1)'"'"'"  "${fname%%.*}_720.${fname##*.}"'
kill $pid

相關內容