Controlling Your Linux System with a Smartphone
My wife and I have four kids. All of which compete for time on our family Linux box. The computer, which is named Saturn, is constantly logged in with the account “saturn”. All kids use this account and are either watching TV, videos, YouTube, listening to music or playing Flash games on the Internet. I was getting sick of kicking the kids off the computer at supper time. This is why the interface “big-meanie” was created.
This next application scales up the previous example. I chose to use Perl as the CGI language, mostly because I am a longtime Perl user and fan. In the following example, I omit the HTML and CSS code because it's not significantly different from the code in the first example.
The main difference being that this example has three buttons: one is for starting a five-minute countdown until all fun is over. Another is to stop the countdown, just in case I have a change of heart. The last is when I really mean business and want to terminate all applicable programs immediately.
Figure 2 shows the layout of Big Meanie.
The first button gives the kids a five-minute countdown to get off the computer. I thought it appropriate to have visual indicators popup over the current windows on the desktop. You need to set a few shell variables in order to output to the local display: XAUTHORITY, HOME and DISPLAY. The following Perl commands accomplish this:
$ENV{'XAUTHORITY'} = '/home/saturn/.Xauthority';
$ENV{'HOME'} = '/home/saturn';
$ENV{'DISPLAY'} = ':0';
This allows windows to open on the local display, even though the command was instigated remotely.
The next thing to note is that you have more than one function to perform, so you need to inform the script which action to perform (that is, which button was pressed). Since you're are dealing with Web URLs here, appending your parameter to the URL is the easiest way to accomplish this. Using a ? sign tells your script that the following text is a parameter. That parameter will be the action to perform.
Here is the full JavaScript file for Big Meanie:
var myDomain = document.domain; // Grab current domain.
var cgiURL = "http://" + myDomain + "/cgi-bin/big-meanie.cgi";
var xmlRequest;
// This is the logic for the "5 Minute Warning" button
function warn5() {
xmlRequest = new XMLHttpRequest();
// Add the 5min variable to the URL
xmlRequest.open("GET",cgiURL + "?5min", true);
xmlRequest.send(null);
}
// This is the logic for the "Cancel Countdown" button
function cancel() {
xmlRequest = new XMLHttpRequest();
// Add the cancel variable to the URL
xmlRequest.open("GET",cgiURL + "?cancel", true);
xmlRequest.send(null);
}
// This is the logic for the "Get Off Now" button
function offnow() {
xmlRequest = new XMLHttpRequest();
// Add the off-now variable to the URL
xmlRequest.open("GET",cgiURL + "?off-now", true);
xmlRequest.send(null);
}
Each button has its own JavaScript function. The URL to the CGI script is appended with the appropriate variable within each function. The variable then is passed to the CGI script with the xmlRequest.send() function.
All action up until now has been happening on the phone browser. Now, let's dive into the script on the server, big-meanie.pl.
There is one last important thing to mention before going over the server-side script (Listing 1). Internet browsers expect a response from sites when making a URL request. The browser hangs if there is no response. You can get around this by creating a second thread that responds automatically with an empty message. The browser doesn't get stuck when the server is executing the command this way, which is important for what you're about to do.
Listing 1. Server-Side Script
#!/usr/bin/perl
# Grab the URL variable
$variable = $ARGV[0];
# Set some important environment variables.
$ENV{'XAUTHORITY'} = '/home/saturn/.Xauthority';
$ENV{'DISPLAY'} = ':0';
$ENV{'HOME'} = '/home/saturn';
# if the 5 minutes warning button is pushed
if ($variable =~ /^5min$/) {
# Lets create a child process to deal with the notifications
defined(my $childpid = fork);
if ($childpid) { # If a child pid exists... this is the parent
# Send response so the browser is happy
print "Content-type: text/html\n\n";
# Show a popup warning message
# displaying 5 minutes remaining.
`zenity --warning --text='5 minutes left to play'`;
} else { # Otherwise it's the child
# Print the amount of time left with a subtle gnome
# notification message.
sleep(60);
`notify-send '4 minutes left to play'`;
sleep(60);
`notify-send '3 minutes left to play'`;
sleep(60);
`notify-send '2 minutes left to play'`;
sleep(60);
`notify-send '1 minutes left to play'`;
sleep(60);
# We are now out of time.
# Let's close all the fun applications
`/usr/bin/tvtime-command quit`; # Close the TV
`pkill mplayer`; # Close mplayer
`pkill totem`; # Close Totem movie player
`pkill rhythmbox`; # Close the music player
`pkill firefox`; # Close the web browser
}
}
# If the Cancel Countdown button has been pushed.
if ($variable =~ /^cancel$/) {
defined(my $childpid = fork);
if ($childpid) { # If parent
print "Content-type: text/html\n\n";
} else {
`pkill big-meanie`;
}
}
# If the Get Off Now button has been pushed.
if ($variable =~ /^off-now$/) {
defined(my $childpid = fork);
if ($childpid) { # If parent
print "Content-type: text/html\n\n";
} else {
`/usr/bin/tvtime-command quit`; # Close the TV
`pkill mplayer`; # Close mplayer
`pkill totem`; # Close Totem movie player
`pkill rhythmbox`; # Close the music player
`pkill firefox`; # Close the web browser
# Just because we can... Send out a tweet that
# someone has been kicked off the computer
`curl -u <twitterUser>:<twitterPassword> -d
status='Someone has been rudely kicked off the computer.'
http://twitter.com/statuses/update.xml > /dev/null`;
}
}
The action variable from the URL is grabbed through the $ARGV[0] array. The if statements that follow test which variable has been specified and, therefore, which action to execute. The first action, 5min, executes a five-minute warning by forking the CGI script (creating a copy of the process). The “parent” branch of the fork executes the if part of the if statement, and the “child” branch executes the else part. The parent sends an empty HTML response to the browser so the client doesn't hang and pops up a five-minute warning message box using Zenity. Zenity provides a simple GUI interface that can be accessed from scripts, and it should be installed by default on most GNOME desktops. There are alternatives for KDE users, such as kdialog or whiptail. The advantage of putting the pop-up dialog in a separate thread from the countdown notifications is the script doesn't stall if the current user doesn't click the OK button on the Zenity window.
The child portion of the fork does the actual five-minute countdown. I wanted to be a little humane here and provide subtle notifications of how much time is left before the user is kicked off. This allows the older kids to save their session or work. My younger kids just need to be eased into the idea of stopping.
The child part of the script sleeps for 60 seconds after the dialog appears. A simple notification then is sent to the desktop once the child wakes up. It appears as a standard GNOME notification in the upper right of the screen. Figure 3 shows how this appears to the user. This procedure repeats until the countdown is complete. Next, the meanie part of Big Meanie kicks in and sends a kill signal to any fun type of program the kids could be enjoying.

Figure 3. GNOME Notification on the Desktop Displaying the Time Remaining
I used the notify-send command commonly installed with GNOME. It is included in the libnotify-bin package, and it should be in most distribution repositories. KDE users can use kdialog with the --passivepopup flag instead of notify-send.
The second if statement is for the cancel countdown button. It sends a kill signal to all instances of big-meanie, essentially killing itself and all sleeping instances, which will kill any previously started five-minute wait child processes that are waiting to shut down things.
The last if statement doesn't need much explanation. It kicks the user off immediately and is almost identical to the first, excluding the countdown logic. You already may have noticed the juicy little addition at the very end of the code: big-meanie sends a status update to Twitter stating somebody has been kicked off the computer. It seemed appropriate for big-meanie to gloat to the whole world that someone's fun has been terminated. It also acts as a safety feature informing me if one of the kids has discovered the program and is possibly using it in any unfriendly fashion on fellow siblings.
Realizing the promise of Apache® Hadoop® requires the effective deployment of compute, memory, storage and networking to achieve optimal results. With its flexibility and multitude of options, it is easy to over or under provision the server infrastructure, resulting in poor performance and high TCO. Join us for an in depth, technical discussion with industry experts from leading Hadoop and server companies who will provide insights into the key considerations for designing and deploying an optimal Hadoop cluster.
Sponsored by AMD
Built-in forensics, incident response, and security with Red Hat Enterprise Linux 6
Every security policy provides guidance and requirements for ensuring adequate protection of information and data, as well as high-level technical and administrative security requirements for a system in a given environment. Traditionally, providing security for a system focuses on the confidentiality of the information on it. However, protecting the data integrity and system and data availability is just as important. For example, when processing United States intelligence information, there are three attributes that require protection: confidentiality, integrity, and availability.
Learn more about catching the bad guy in this free white paper.
Sponsored by DLT Solutions
| Designing Electronics with Linux | May 22, 2013 |
| Dynamic DNS—an Object Lesson in Problem Solving | May 21, 2013 |
| Using Salt Stack and Vagrant for Drupal Development | May 20, 2013 |
| Making Linux and Android Get Along (It's Not as Hard as It Sounds) | May 16, 2013 |
| Drupal Is a Framework: Why Everyone Needs to Understand This | May 15, 2013 |
| Home, My Backup Data Center | May 13, 2013 |
- RSS Feeds
- Dynamic DNS—an Object Lesson in Problem Solving
- Making Linux and Android Get Along (It's Not as Hard as It Sounds)
- Using Salt Stack and Vagrant for Drupal Development
- New Products
- A Topic for Discussion - Open Source Feature-Richness?
- Drupal Is a Framework: Why Everyone Needs to Understand This
- Validate an E-Mail Address with PHP, the Right Way
- What's the tweeting protocol?
- Tech Tip: Really Simple HTTP Server with Python
- Kernel Problem
1 hour 6 min ago - BASH script to log IPs on public web server
5 hours 33 min ago - DynDNS
9 hours 9 min ago - Reply to comment | Linux Journal
9 hours 41 min ago - All the articles you talked
12 hours 5 min ago - All the articles you talked
12 hours 8 min ago - All the articles you talked
12 hours 9 min ago - myip
16 hours 34 min ago - Keeping track of IP address
18 hours 25 min ago - Roll your own dynamic dns
23 hours 38 min ago
Enter to Win an Adafruit Pi Cobbler Breakout Kit for Raspberry Pi

It's Raspberry Pi month at Linux Journal. Each week in May, Adafruit will be giving away a Pi-related prize to a lucky, randomly drawn LJ reader. Winners will be announced weekly.
Fill out the fields below to enter to win this week's prize-- a Pi Cobbler Breakout Kit for Raspberry Pi.
Congratulations to our winners so far:
- 5-8-13, Pi Starter Pack: Jack Davis
- 5-15-13, Pi Model B 512MB RAM: Patrick Dunn
- 5-21-13, Prototyping Pi Plate Kit: Philip Kirby
- Next winner announced on 5-27-13!
Free Webinar: Hadoop
How to Build an Optimal Hadoop Cluster to Store and Maintain Unlimited Amounts of Data Using Microservers
Realizing the promise of Apache® Hadoop® requires the effective deployment of compute, memory, storage and networking to achieve optimal results. With its flexibility and multitude of options, it is easy to over or under provision the server infrastructure, resulting in poor performance and high TCO. Join us for an in depth, technical discussion with industry experts from leading Hadoop and server companies who will provide insights into the key considerations for designing and deploying an optimal Hadoop cluster.
Some of key questions to be discussed are:
- What is the “typical” Hadoop cluster and what should be installed on the different machine types?
- Why should you consider the typical workload patterns when making your hardware decisions?
- Are all microservers created equal for Hadoop deployments?
- How do I plan for expansion if I require more compute, memory, storage or networking?





Comments
Create .Xauthority file
Hi,
I really liked your article, I was trying it but there is no .Xauthority file in my home folder.
Can you guide me how to create it under ubuntu 10.10.
Thank You
Or, you could install webmin.
Or, you could install webmin.