Work the Shell - FilmBuzz Trivia Goes Live
I was sure last month that we'd wrap up this film-trivia Twitter game, but, as you'll recall, I ended that column with “Oh. We've run out of space. Again.”
This month, I'll skip the prelude and jump in. You should follow the triviabot at twitter.com/FilmBuzz, and you can find back columns on the Linux Journal Web site if you need to get up to speed.
Last month, I ended by showing you a rudimentary solution to sending out twitters on the command line that looked like this:
#!/bin/sh # tweet - command line twitter interface user="filmbuzz"; pass="acctpasswd" msg=$(echo $@ | sed 's/+/%2B/g;s/ /+/g') $curl --silent --user "$user:$pass" --data-ascii \ "status=$msg" "http://twitter.com/statuses/update.json" \ > /dev/null
With that available, sending Twitter updates is as easy as typing:
$ tweet "My favorite film? Probably Lawrence of Arabia"
And, it's off into the ether (Figure 1).
So, clearly you can send tweet messages from the command line. In previous columns, we also pulled all the pieces together to be able to output trivia questions to standard out (stdout).
We have two command-line shell scripts that we need to put together: one sends its input as a message to Twitter, and the other actually generates a trivia question. Here's the latter, in action:
$ generate-trivia-question.sh Film Trivia! Was the movie "Schindler's List" released in 1993, 1994 or 1996?
There are a bunch of ways to put them together, but I'm partial to subshells using the $( ) notation. So, here's how I can output the very first real live programmatically generated trivia question to the FilmBuzz Twitter account:
$ ./tweet $(bash generate-trivia-question.sh)
And, Figure 2 shows the output.
Hu-bloody-rah! Finally.
Now that we have a command-line-based method of generating and disseminating movie trivia questions via Twitter, we need to automate the process, because I am not going to log in every few hours and type that command on the command line!
The tool of choice for any sort of automation, of course, is crontab. If you're reading Linux Journal, I imagine you're already familiar with it, but if not, read the man page for this powerful utility (man crontab).
For all its strengths, crontab requires that you have a simple invocation, and I generally like to write a script specifically targeting that crontab entry—like this:
#!/bin/sh # film trivia crontab job tweet="/home/filmbuzz/trivia/tweet" generateq="/home/filmbuzz/trivia/generate-trivia-question.sh" question="$(/usr/local/bin/bash $generateq)" $tweet "$question" exit 0
Easy enough. This can, of course, be run from the command line to test it, but what we really want to do is run it from crontab to see if all the paths and file permissions are correct, so it can run unattended.
To create or edit a crontab file, type crontab -e. I'm going to send out only two trivia questions each day: one at 11am and one at 3pm. The format of crontab entries is a bit tricky though, as the time and date recurrence requirements are specified as minute, hour, day-of-month, month and day-of-week, followed by the command itself. Fields you don't want to specify can be left as an asterisk (a wild card). So, if I want to run this command seven days a week at 11am and 3pm, I can most easily specify it as:
0 11,15 * * * trivia-cronjob
That's not quite right, though, because generally you can count on cron jobs having a far more truncated PATH than you're used to interactively, so every path should be specified (including in any script that's executed). Here's what I actually have in my crontab:
0 11,15 * * * $FBDIR/trivia/trivia-cronjob
Oh, there's one more wrinkle. My server runs in UTC (Universal Time, Coordinated, aka Greenwich Mean Time), so when it's 11:22am here in Colorado, my server thinks it's 17:22:41 GMT 2008.
Because crontab uses the system time, I need to adjust my specified times to meet my expectations, moving from 11,15 to 17,21. Otherwise, we're good to test!
Hmm...nothing happened. A quick check of my e-mail reveals the cause:
/home/filmbuzz/trivia/trivia-cronjob: permission denied
That's easily fixed with a chmod call (Figure 3).
As you can see in Figure 3, that fixed the problem, and now we've got a live trivia-question-injection system that scrapes the Internet Movie Database, figures out the correct and two likely, but incorrect, release years and puts it out on Twitter.
Next month, we'll finally move to a new scripting topic. And, in the meantime, if you want to enjoy the fruit of our extended labor and try your hand at film trivia, follow @FilmBuzz on Twitter at twitter.com/filmbuzz.
Dave Taylor is a 26-year veteran of UNIX, creator of The Elm Mail System, and most recently author of both the best-selling Wicked Cool Shell Scripts and Teach Yourself Unix in 24 Hours, among his 16 technical books. His main Web site is at www.intuitive.com, and he also offers up tech support at AskDaveTaylor.com. Follow Dave on Twitter through twitter.com/DaveTaylor.
Dave Taylor has been hacking shell scripts for over thirty years. Really. He's the author of the popular "Wicked Cool Shell Scripts" and can be found on Twitter as @DaveTaylor and more generally at www.DaveTaylorOnline.com.
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
| 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 |
| Non-Linux FOSS: Seashore | May 10, 2013 |
- RSS Feeds
- Making Linux and Android Get Along (It's Not as Hard as It Sounds)
- Using Salt Stack and Vagrant for Drupal Development
- Dynamic DNS—an Object Lesson in Problem Solving
- 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?
- Download the Free Red Hat White Paper "Using an Open Source Framework to Catch the Bad Guy"
- Tech Tip: Really Simple HTTP Server with Python
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?







4 hours 19 min ago
7 hours 30 min ago
9 hours 45 min ago
10 hours 14 min ago
11 hours 12 min ago
12 hours 41 min ago
13 hours 49 min ago
14 hours 36 min ago
21 hours 12 min ago
1 day 2 hours ago