I am trying to write a script file and invoke it using XFCE global shortcut. The script should continue the Audacity application if it is in the stopped state and stop it (pause recording) if it is in the run state. This will be useful when I have defined the same keyboard shortcut for VLC's playback/pause. This way, I can do playback/pause (in VLC) and record/pause (in Audacity) at the same time using the same keyboard shortcut. Getting some ideas from this post I have written the following script and added it to XFCE's custom keyboard settings. But it does not work.

if pgrep -f "audacity" ;
       pkill -stop audacity && notify-send "Recording stopped"
       pkill -cont audacity
  • 3
    You are missing a fi to close the if statement. After you fix it, try to run this script in the terminal to see if it outputs any messages. – giusti Jan 7 '17 at 18:32
  • It does not work yet after fixing the typo. It only executes the same command every time (only stops audacity) – codezombie Jan 7 '17 at 19:39
  • That's because pgrep only verifies if there is a process named audcity, not if it is running or stopped. So it will always enter the then case. – giusti Jan 7 '17 at 21:11
  • 1
    But honestly I'm not sure that this should work. I don't know how Audacity will handle a SIGSTOP, but the most likely scenario is that you'll stop Audacity as a whole, not just its recording. A better solution would be to use Audacity tools for automation. I think they do exist: wiki.audacityteam.org/wiki/Automation – giusti Jan 7 '17 at 21:15

To be able to know whether to send the STOP or the CONT signal to the process, you will have to figure out its current state first.

  • If it's stopped, you should send CONT.
  • If it's not stopped, you should send STOP.

The state of a running process may be found with ps. If a process is stopped, then it its state, as reported by ps -ostate= will contain the letter T (the equal sign will stop ps from outputting a header).



pids="$( pgrep "$command" )"

if [[ -z "$pids" ]]; then
    printf '"%s" is not running\n' "$command" >&2
    exit 1

for pid in $pids; do
    state="$( ps -ostate= -p "$pid" )"


    case "$state" in
        *T*)    stopped=1 ;;

    if (( stopped )); then
        kill -s CONT "$pid"
        printf '"%s" (%d) has been unpaused\n' "$command" "$pid"
        kill -s STOP "$pid"
        printf '"%s" (%d) has been paused\n' "$command" "$pid"

There is a theoretical race-condition in the script whereby the command may have quit between the call to pgrep and the call to kill. All that will happen in that case is that kill will complain about "no such process".

  • Thanks I was kind of dying for a bash solution. By the last part, do you mean I cannot pause and unpause two apps at tge same time? – codezombie Jan 7 '17 at 21:36
  • @JasonStack It's a simple limitation that I didn't notice as I was writing the script. If two processes with the same name are running, $pid will get the value of both of them (a string with two numbers), and the ps call will probably fail, as will the kill call. Hold on, I'll fix it... Done. – Kusalananda Jan 7 '17 at 22:24
  • Thanks! I just tested this. It works. However there is some delay between when the script is run and when vlc pauses stops playing or when resumes playing. I experienced the same issue when running a similar script but written in Python by user Jacob Vlijm for this post on AskUbuntu. – codezombie Jan 8 '17 at 18:56

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.