Sound alarm when code finishes
Interview Questions practice on Codemia
Over 8,000 real interview questions from top companies, searchable by company and role.
Introduction
If a script or training job runs long enough that you stop watching it, an audible alert at completion can be more useful than another log line. The implementation is usually simple: run the task, then trigger a system beep, play a sound file, or speak a message when the work ends. The best method depends on the operating system and whether you want a built-in alert or a custom sound.
The Simplest Pattern
The general structure is:
- do the work
- when it finishes, trigger a sound
A Python example with a plain terminal bell:
The \a character asks the terminal to ring the bell. This is lightweight, but it depends on terminal and system settings. Some terminals ignore it.
Windows Example With winsound
On Windows, winsound is a simple built-in option.
Or play a system sound:
This is often the easiest reliable approach on Windows machines.
macOS and Linux Command-Line Alerts
If you are using shell scripts, the operating system may already provide tools you can call at the end.
macOS example:
Linux example with a WAV file player:
Or a generic terminal bell:
This is often good enough when you want the notification outside the program rather than inside it.
Custom Sound File in Python
If you want a custom sound rather than a simple beep, you can call a system player or use a library appropriate to your platform.
A subprocess approach keeps the code simple and explicit:
That example is macOS-specific because it uses afplay, but the pattern generalizes to any platform-specific sound player.
Add It to Existing Workflows
The alert can live inside the script:
Or outside the script in the shell:
External notification is nice when you do not want to edit the script itself.
Prefer Clear Signals for Long Jobs
For very long-running jobs, a sound alone may not be enough. In practice, many developers combine sound with:
- a desktop notification
- a terminal message
- a log entry
- an email or chat alert for remote jobs
The right level of notification depends on whether the code runs locally or on a remote machine you are not actively watching.
Common Pitfalls
A common mistake is relying on the terminal bell without checking whether the terminal or operating system has audible alerts disabled.
Another issue is using a platform-specific sound command such as afplay or winsound in code meant to run unchanged on multiple operating systems.
Developers also sometimes put the notification only in the success path and forget that failures may also deserve an alert.
Finally, for remote jobs running on a server, a local sound on the server is often useless. In those cases, use a remote notification channel instead.
Summary
- The basic pattern is simple: run the code, then trigger a sound at the end.
- '
\ais the lightest option, but it is not always audible on every terminal.' - Windows can use
winsound, while macOS and Linux often rely on shell audio tools. - Shell-level alerts are useful when you do not want to modify the program.
- For long or remote jobs, combine sound with a more explicit notification channel when needed.
.png&w=3840&q=75)
Tackling System Design Interview Problems
A short course that equips you with the skills to approach system design interviews methodically.
Start the free courseTrack what you have practised
A free account saves your progress, solutions and study plan across every problem on Codemia.
Interview Questions practice on Codemia
Over 8,000 real interview questions from top companies, searchable by company and role.