I would like to record audio while Audacity is running in the background, e.g. because I am focused on another window. Is it possible to control the start and stop of a recording using global keyboard shortcuts?

  • You could create a keyboard shortcut which runs an xdotool command which sends a keystroke to a specific window -- the Audacity window, in this case.
    – wjandrea
    Dec 1, 2016 at 19:17
  • Hi orschiro, just curious, but us any of the options usable in your situation? Dec 5, 2016 at 22:31
  • @JacobVlijm I forgot to mark the best answer, sorry. I chose yours because I like its simplicity. Thank you. :-)
    – orschiro
    Dec 7, 2016 at 0:09

3 Answers 3


Does Audacity have cli options?

Unfortunately, looking into man audacity, does not show any interesting or relevant options:

       -help     display a brief list of command line options

       -version  display the audacity version number

       -test     run  self  diagnostics  tests  (only  present  in development

       -blocksize nnn
                 set the audacity block size for writing files to disk to  nnn


simply pausing/continuing Audacity from cli works exactly similarly to pausing/continuing recording

I tested pausing Audacity as a process, with the commands kill -stop <pid> and kill -cont <pid>. It did the job perfectly; it stopped recording immediately, resuming immediately when the process was continued. Even after 6 or seven hours, it stops/resumes recording right away with these commands.

Adding the two commands to shortcut keys does exactly what you describe.

How to use

Add the following two commands to shortcut keys:

/bin/bash -c "kill -stop $(pgrep audacity)"

...to stop (pause) recording, and

/bin/bash -c "kill -cont $(pgrep audacity)"

...to resume, and you have your own shortcut keys to start/stop (actually pause) recording.

Only the initial starting of the recording needs to be done via the Audacity GUI.

Adding to shortcut keys

Choose: System Settings > "Keyboard" > "Shortcuts" > "Custom Shortcuts". Click the "+" and add the two commands above to two different shortcut keys.

Extending options

Of course we can extend options, e.g.

/bin/bash -c "kill -cont $(pgrep audacity) && notify-send Recording"


/bin/bash -c "kill -stop $(pgrep audacity) && notify-send Stopped"

enter image description here

...to show a notification on the current state, or even make a panel icon to show the current state. Anything is possible

Additionally, a toggle script, as requested in a comment

The script below will toggle recording. Add it to either a shortcut key or launch it in any other way.

The script reads the current process state from the file /proc/<pid>/status, and decides what the targeted state should therefore be.

Additionally, the script notifies the user what happens in a notification (using notify-send).

#!/usr/bin/env python3
import subprocess

    pid = subprocess.check_output(["pgrep", "audacity"]).decode("utf-8").strip()
except subprocess.CalledProcessError:
    state = "(stopped)" in open("/proc/"+pid+"/status").read()
    if state == True:
        cmd = ["kill", "-cont", pid]; msg = "Recording"
        cmd = ["kill", "-stop", pid]; msg = "Pausing"
    subprocess.Popen(["notify-send", msg])

To use it

  • Simply copy it into an empty file, save it as toggle_record.py
  • Test run it with the command:

    python3 /path/to/toggle_record.py
  • If all works fine add it to a shortcut key as explained earlier in the answer.

  • 1
    Sending stop signal isnt always good for GUI applications. In case of firefox, if it is stopped for a while it crashes, at least in my experience. But yeah, that is the best option available so far Dec 1, 2016 at 19:52
  • @Serg, yeah, that's why I tested it for a while. until now I didn't get a single error :) Dec 1, 2016 at 19:53
  • how long was it stopped ? Dec 1, 2016 at 19:56
  • Run it for a couple hours, let me know if it works Dec 1, 2016 at 20:03
  • 3
    Hi @Serg 7 hours later, it started right away. We may assume it works. Dec 2, 2016 at 6:29

Can Audacity be controlled when its window not in focus ?

In theory, yes, since it has a scripting module, which apparently can be used with Perl scripting language, but as of right now I've not seen one made, so there is potential for Perl-skilled developers to write one.

But as it is, the answer to your question is "no".

arecord Script

What's great about open source community is that there's always an alternative. In this case, arecord , which works from command-line. What you see below is a very quick script I've made, which is intended to be bound to a keyboard shortcut. Usage is simple: press key combination - recording starts, and press it again to stop recording.

What's "hard-coded" in this script is the default wav type of recording and ~/Music/records folder. Users are free to edit this script as they like to use their own options/filetypes/locations, etc. Please consult arecord man page for additional options.

Eventually I plan rewriting this script in Python, which will allow more fine-grained control of user's directories, add command-line options, perhaps other things. As for now, this does 75% of the job needs to be done.

Below is the source code of the script which is also available on my GitHub repository. Standard rules for creating and using scripts apply: ensure it has executable permissions and is stored in your ~/bin folder

# Author: Serg Kolo
# Date: Dec 1, 2016
# Purpose: simple script for recording audio with arecord
# Written for: https://askubuntu.com/q/855893/295286

    # Set up some variables to control arecord here
    # Please remember to quote the variables
    # and pay attention to slashes in file paths

    filename="record_$(date +%H_%M_%m_%d_%Y)"

    if ! [ -d "$directory" ];
        mkdir "$directory"

    # This part will initiate recording of timestamped
    # please see arecord's man page for other options
    notify-send "Recording started"
    exec arecord -t "$filetype" "$directory""$filename"."$filetype"

    if pgrep -f "arecord" ;
       pkill -f "arecord" && notify-send "Recording stopped"

main "$@"

additional information:


Yes. You can use a combination of a script, xdotool, wmctrl and Ubuntu's hotkey.

First create the file to select the recording window and send the command key to that window.

Then assign a hotkey to activate the script.

The Script


# Specify the Window running your recording by running 
# "./hkrecord.sh getwindowid" from the command line.
# Alternatively you can edit the ~/recordfile and place your own string.  This
# first line of the fie will be used 

# Ensure you have the following "xdotool" and "wmctrl" installed.


    windowid=$(wmctrl -lp | awk '{$2=$3=$4=":"; print $0}'| awk -F: \
'{print $1"\n"$4}' | zenity --list --column="No" --height=800 --width=1000 \
--column="Select your choice" \
--text="Text above column(s)" --hide-column=1 --title="My menu" 2>/dev/null)
    [[ "$windowid" ]] && sed -i "1i @$windowid" $recordfile || \
echo "No Window selected"

if [ ! $# -eq 0 ]; then
    if [[ "x$1" == *"get"* ]]; then     
        echo "Getting the window id"

recordwindow="$(head -n1 $recordfile)"

if [ "$(echo $recordwindow | egrep '@0x')" ]; then
    windowid=$(echo $recordwindow | sed "s/\@//")
    windowid=$(echo $(wmctrl -lp|egrep -i $recordwindow)|head -n1|\
awk '{print $1}')
    echo "$windowid|$recordfwindow"  # debug line... can be commented out.

echo "Using WindowID: $windowid" # debug line... can be commented out.

if [ -z $windowid ]; then
    espeak "Can't locate specified window!"
    espeak "Exiting..."

xdotool key --window $windowid p

Save the script to a convenient area such as /usr/local/bin/hkrecord.sh

Assign the hotkey

First run the script from the commandline as a quick test. Then assign the global keyboard shortcut. To specify the window (needed only one time) run:

$ ./hkrecord.sh getwindow

Running hkrecord.sh without an argument will control the recording.

Go into System Settings -> keyboard and add a custom Keyboard Shortcut. Pick a hotkey that won't conflict with your foreground program. I use Shift+Ctrl+P.

This is the configuraton for the Custom Shortcut:

Name:        Hotkey Record
Command:     /usr/local/bin/hkrecord.sh

I placed the instructions as comments in the script file.

As an added benefit, the same Global Hotkey can be used for pausing playback. I use this system as a dictaphone to write from my recorded notes.

I tried to make the script very simple to be easy to understand and customize. Thoroughly tested, it will work seamlessly.

Note: Thanks goes out to don_crissti and and George_Vasiliou (https://unix.stackexchange.com/a/328548/81664) for helping me with the GUI for selecting the record window.

  • In theory a nice option, but currently not working on my Unity /16.04). I believe xdotool's xdotool key --window (or actually the way you search for the window) is likely to catch a cold. If I add echo $windowid to the script, I get 7 (!) different window -id's, which obviously is false (Unity), and the script fails totally. Whenever I look for the corresponding window -id in wmctrl however and run the command directly, it works. I'd suggest looking for the appropriate window by pid and using wmctrl. Add a notification and you have a winner. Dec 2, 2016 at 19:35
  • @JacobVlijm I updated the script about the same time you posted. I added the option to isolate only one window if more than one window is found. Be sure to have a much of the Window tittle to distinguish it from similar tittle windows. Dec 2, 2016 at 19:38
  • I mean you need to find the window from wmctrl -lp, looking for a match with pgrep audacity. That is a 100% working match. Dec 2, 2016 at 19:48
  • @JacobVlijm I'm noticing that xdotool isn't recognizing the window by the name "Audacity". Use the save option to save the project or file to a unique name. I already added this information to the answer. Dec 2, 2016 at 19:49
  • really not necessary, just look for the line in wmctrl -lp in which the pid of audacity is found, always works if there is a single window. Dec 2, 2016 at 19:51

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .