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?
3 Answers
Does Audacity have cli options?
Unfortunately, looking into man audacity
, does not show any interesting or relevant options:
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
builds)
-blocksize nnn
set the audacity block size for writing files to disk to nnn
bytes
However...
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"
and
/bin/bash -c "kill -stop $(pgrep audacity) && notify-send Stopped"
...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
try:
pid = subprocess.check_output(["pgrep", "audacity"]).decode("utf-8").strip()
except subprocess.CalledProcessError:
pass
else:
state = "(stopped)" in open("/proc/"+pid+"/status").read()
if state == True:
cmd = ["kill", "-cont", pid]; msg = "Recording"
else:
cmd = ["kill", "-stop", pid]; msg = "Pausing"
subprocess.Popen(cmd)
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.

- 83,767
-
1Sending 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 – Sergiy Kolodyazhnyy Dec 01 '16 at 19:52
-
@Serg, yeah, that's why I tested it for a while. until now I didn't get a single error :) – Jacob Vlijm Dec 01 '16 at 19:53
-
-
-
3Hi @Serg 7 hours later, it started right away. We may assume it works. – Jacob Vlijm Dec 02 '16 at 06:29
-
Thanks for the solution. What about adding both commands in one bash script that toggles between the two commands with the same keyboard shortcut? – Mehdi Haghgoo Jan 07 '17 at 17:34
-
-
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
#!/bin/bash
# Author: Serg Kolo
# Date: Dec 1, 2016
# Purpose: simple script for recording audio with arecord
# Written for: https://askubuntu.com/q/855893/295286
record_audio()
{
# Set up some variables to control arecord here
# Please remember to quote the variables
# and pay attention to slashes in file paths
filetype="wav"
filename="record_$(date +%H_%M_%m_%d_%Y)"
directory="$HOME/Music/recordings/"
if ! [ -d "$directory" ];
then
mkdir "$directory"
fi
# 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"
}
main()
{
if pgrep -f "arecord" ;
then
pkill -f "arecord" && notify-send "Recording stopped"
else
record_audio
fi
}
main "$@"
additional information:

- 105,154
- 20
- 279
- 497
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
#!/bin/bash
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.
recordfile="$HOME/recordfile"
getwindowid(){
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"
exit
}
if [ ! $# -eq 0 ]; then
if [[ "x$1" == "get" ]]; then
echo "Getting the window id"
getwindowid
exit
fi
fi
recordwindow="$(head -n1 $recordfile)"
if [ "$(echo $recordwindow | egrep '@0x')" ]; then
windowid=$(echo $recordwindow | sed "s/@//")
else
windowid=$(echo $(wmctrl -lp|egrep -i $recordwindow)|head -n1|
awk '{print $1}')
echo "$windowid|$recordfwindow" # debug line... can be commented out.
fi
echo "Using WindowID: $windowid" # debug line... can be commented out.
if [ -z $windowid ]; then
espeak "Can't locate specified window!"
espeak "Exiting..."
exit
fi
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.

- 25,036
-
In theory a nice option, but currently not working on my Unity /16.04). I believe
xdotool
'sxdotool key --window
(or actually the way you search for the window) is likely to catch a cold. If I addecho $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 inwmctrl
however and run the command directly, it works. I'd suggest looking for the appropriate window bypid
and usingwmctrl
. Add a notification and you have a winner. – Jacob Vlijm Dec 02 '16 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. – L. D. James Dec 02 '16 at 19:38
-
I mean you need to find the window from
wmctrl -lp
, looking for a match withpgrep audacity
. That is a 100% working match. – Jacob Vlijm Dec 02 '16 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. – L. D. James Dec 02 '16 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. – Jacob Vlijm Dec 02 '16 at 19:51 -
I thought that was handled. If it can't find the window it will exit with an audible prompt. – L. D. James Dec 02 '16 at 20:06
-
Yeah, my bad. I immediately removed the comment, but apparantly too late :) – Jacob Vlijm Dec 02 '16 at 20:07
-
xdotool
command which sends a keystroke to a specific window -- the Audacity window, in this case. – wjandrea Dec 01 '16 at 19:17