1

I want to use yad system tray notification to terminate an application (vlc for example), I used the following code:

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

vlc & pid=${!}

The code gives the following error:

kill: failed to parse argument: '$pid'

any help is appreciated

muru
  • 193,181
  • 53
  • 473
  • 722

2 Answers2

1

Run vlc first to get its PID correctly into the variable $pid like so:

vlc & pid=${!}

Then use double quotes " around the parameter(not single quotes ') to allow parameter expansion of $pid like so:

yad --notification --no-middle --image=emblem-warning --menu="Cancel ! kill $pid" --text="My Tooltip" &
Raffa
  • 24,905
  • 3
  • 35
  • 79
  • Thanks for your reply. – Mahmoud Alnaanah Jan 08 '23 at 13:45
  • @MahmoudAlnaanah Unless I'm mistaken, Raffa's answer solved the error in your question. The other changes that you made to your script (in your answer) don't seem to have to do with the error that you were receiving. I would recommend marking this as the accepted answer. Thanks! – NotTheDr01ds Jan 09 '23 at 00:00
0

The goal of the code is to launch ffmpeg to resize and encode a video and be able to close it using yad. I found the solution by storing the PID of the process in a temporary file. I spent long time trying to use single quotes inside bash -c 'command'. I finally solved it by replacing single quotes with '"'"'

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
karel
  • 110,292
  • 102
  • 269
  • 299
  • A few notes on your answer -- First, you'll notice that muru took the time to edit your question to improve the formatting and readability. We're happy to do that the first time for you as you get used to site, but we're also hoping that you'll use the example to improve the formatting of your future posts, rather than making the same mistakes again. In this case, your answer has the same formatting issues as your question. – NotTheDr01ds Jan 08 '23 at 23:54
  • Second the suggestion to use double-quotes in place of the single-quotes came from [@Raffa's answer](https://askubuntu.com/a/1449290/1165986) shortly after you posted the question. When you post an answer, we do require that you provide proper attribution for any part of the answer that you didn't come up with yourself. – NotTheDr01ds Jan 08 '23 at 23:59