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.
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
| 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 |
| Dart: a New Web Programming Experience | May 07, 2013 |
- New Products
- Making Linux and Android Get Along (It's Not as Hard as It Sounds)
- Drupal Is a Framework: Why Everyone Needs to Understand This
- A Topic for Discussion - Open Source Feature-Richness?
- Home, My Backup Data Center
- RSS Feeds
- Trying to Tame the Tablet
- New Products
- What's the tweeting protocol?
- Dart: a New Web Programming Experience
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.







1 hour 56 min ago
6 hours 35 min ago
8 hours 58 min ago
1 day 1 hour ago
1 day 4 hours ago
1 day 5 hours ago
1 day 6 hours ago
1 day 6 hours ago
1 day 11 hours ago
1 day 12 hours ago