2008-06-05
Emit a remote beep using PHP.
One day I thought it would be nice if I could send a beep from my server to my desktop PC after a batch job. It was usual that I added a ;beep after a job which was supposed to take a longish time on my desktop PC, and found this way convenient. The only problem was that there existed not already such program for doing that remotely. And indeed, having a client/server for this, or starting to develope one, seems a bit like shooting flies with a shotgun... so, thought a bit and the more or less obvious answer was to do the thing with Apache+PHP, using PHP's exec() or system() function. I already had Apache2 running on the desktop PC with PHP for developing purposes, so there was no need to install it just for this tiny task.
The code on desktop side looks like as follows (so this is the server in the point of view of software architecture).
<html><head><title>rbeep</title>
</head>
<body>
<?php
// Remote beep
// 2008-06-02 H. Pitkala
// Public domain
// From the remote host you can call this with
// wget http://hostname/rbeep.php -O - >/dev/null
2>&1
// beep must be set chmod o+rx (and probably also suid: chmod 4755)
$cmd = "/usr/bin/beep -f 440 -l 150 -n -f 660 -l 150";
exec($cmd, &$output, &$ret);
echo "ret = $ret";
?>
</body>
</html>
The sound is a bit different from the default beep, to make some distinction to the local beeps. And as you see from the comments, the program beep must be installed and set chmod o+rx and possibly also suid (chmod 4755).
In Debian, I had to do
a2enmod php5
and manually add
AddHandler php5-script .php
into /etc/apache2/mods-enabled/php5.conf in order to PHP to work. I remember having done this earlier but maybe had installed new version of the configuration file so it had disappeared.
Then, on the server side (client side architecturally) I made a Bash script to just go to the url:
#!/bin/bash
# remote beep
# 2008-06-02
wget rhp300/rbeep.php -O - >/dev/null 2>&1
What this does is that it loads rbeep.php, printing out nothing. The output is supressed with stdout redirection. Wget messages are redirected with the stderr redirection. The suppression of wget messages could have been done with --quiet option as well. So, when rbeep.php is called, it emits a beep on the PC it's running. Nice, this was intended.
For more possibilities, it could be nice in some situations if other notifications than sound be used. There are lot of possibilities for that, e.g. zenity, dcop, knotify... But as for now, this is sufficient for me! Enjoy!