Beep for Linux and Windows

For servers it is sometimes useful to produce a beep. You can do this in Linux with the echo command:

echo -e "\007" >/dev/tty10

And on Windows (type Ctrl-G for the ^G):

echo "^G"

But if you want to change the frequency and duration you need a small program.

For Linux

Note: you need write access to /dev/tty10 or you have to run this program as root. This could be a security hole. Search on Google, if you want a Linux-module with user access for the beep-function.

#include <stdlib.h>
#include <fcntl.h>
#include <linux/kd.h>

int main(int argc, char *argv[])
{
    int fd = open("/dev/tty10", O_RDONLY);
    if (fd == -1 || argc != 3) return -1;
    return ioctl(fd, KDMKTONE, (atoi(argv[2])<<16)+(1193180/atoi(argv[1])));
}

Copy this to a file beep.c and compile it like this:

cc beep.c -o beep
strip beep

Precompiled on RedHat 7.2 (you can rename it from beep.bin to beep after downloading)

For Windows

#define WIN32_LEAN_AND_MEAN
#include <windows.h>
#include <stdlib.h>
#include <tchar.h>

int APIENTRY _tWinMain(HINSTANCE hInstance,
    HINSTANCE hPrevInstance,
    LPTSTR lpCmdLine,
    int nCmdShow)
{
    char* ms = strchr(lpCmdLine, ' ');
    if (!ms) return 1;
    ms++;
    if (!strlen(ms)) return 1;
    Beep(atoi(lpCmdLine), atoi(ms));
    return 0;
}

For compiling with Visual Studio .NET copy this to a file beep.c, create an empty Win32 console project and add the beep.c-file to it.

Precompiled for Windows

Calling from Java

public class Beeper {
    public static void main(String args[]) {
        try {
            Runtime.getRuntime().exec("beep.exe 440 1000");
        } catch (Exception e) {}
    }
}


18. July 2002, Frank Buß