分类: LINUX
2006-09-06 13:42:55
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.
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 , if you want a Linux-module with user access for the beep-function.
#include
#include
#include
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
(you can rename it from beep.bin
to beep
after downloading)
#define WIN32_LEAN_AND_MEAN
#include
#include
#include
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.
public class Beeper {
public static void main(String args[]) {
try {
Runtime.getRuntime().exec("beep.exe 440 1000");
} catch (Exception e) {}
}
}