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.
Today’s modular x86 servers are compute-centric, designed as a least common denominator to support a wide range of IT workloads. Those generic, virtualized IT workloads have much different resource optimization requirements than hyperscale and cloud applications. They have resulted in a “one size fits all” enterprise IT architecture that is not optimized for a specific set of IT workloads, and especially not emerging hyperscale workloads, such as web applications, big data, and object storage. In this report, you will learn how shifting the focus from traditional compute-centric IT architectures to an innovative disaggregated fabric-based architecture can optimize and scale your data center.
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
| 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 |
| Non-Linux FOSS: Seashore | May 10, 2013 |
| Trying to Tame the Tablet | May 08, 2013 |
- Using Salt Stack and Vagrant for Drupal Development
- Making Linux and Android Get Along (It's Not as Hard as It Sounds)
- New Products
- Validate an E-Mail Address with PHP, the Right Way
- Drupal Is a Framework: Why Everyone Needs to Understand This
- A Topic for Discussion - Open Source Feature-Richness?
- The Pari Package On Linux
- Home, My Backup Data Center
- New Products
- Dart: a New Web Programming Experience
- This is the easiest tutorial
4 hours 55 min ago - Ahh, the Koolaid.
10 hours 34 min ago - git-annex assistant
16 hours 33 min ago - direct cable connection
16 hours 56 min ago - Agreed on AirDroid. With my
17 hours 6 min ago - I just learned this
17 hours 10 min ago - enterprise
17 hours 40 min ago - not living upto the mobile revolution
20 hours 31 min ago - Deceptive Advertising and
21 hours 7 min ago - Let\'s declare that you have
21 hours 8 min ago
Enter to Win an Adafruit Prototyping Pi Plate 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 Prototyping Pi Plate 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
- Next winner announced on 5-21-13!
Free Webinar: Linux Backup and Recovery
Most companies incorporate backup procedures for critical data, which can be restored quickly if a loss occurs. However, fewer companies are prepared for catastrophic system failures, in which they lose all data, the entire operating system, applications, settings, patches and more, reducing their system(s) to “bare metal.” After all, before data can be restored to a system, there must be a system to restore it to.
In this one hour webinar, learn how to enhance your existing backup strategies for better disaster recovery preparedness using Storix System Backup Administrator (SBAdmin), a highly flexible bare-metal recovery solution for UNIX and Linux systems.





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.