Raspberry Pi Twitter Sentiment Server – Part 6 – Executing via Cron

So we’ve run all our segments of code within R console. For this to work properly, unaided, we have to put some scripts in place.  I’ve amended all the files to run from the command line in one easy go.

R --no-restore --no-save --args "#bigdata" 100 < RunTwitterSentiment.r

A few things we need to keep in mind. Firstly the Raspberry Pi is a small device so it doesn’t like to be punished too much. Also we need to be mindful of Twitter’s API rate limit.

Search will be rate limited at 180 queries per 15 minute window for the time being, but we may adjust that over time. A friendly reminder that search queries will need to be authenticated in version 1.1.

It all depends on your needs at the end of the day.

Building the shell script

Firstly we need a simple shell script that will do the work when cron runs. If you create a text file from the command line called runner.sh (use vi or nano etc) and copy the following in:

cd /home/pi/rpisentiment
R --no-restore --no-save --args "#bigdata" 100 < RunTwitterSentiment.r

Assuming that the code, credentials and database are in a directory called rpisentiment all should be well.

Next we need to make the shell script run and this requires changing the permissions of the file.  Using chmod to do this:

chmod 775 runner.sh

So from the command line we can run the script with:


And this will fire up R, do the search, sentiment and database storage. Then gracefully exit.  Lovely.

Scheduling with Cron

From the early days of Unix Cron still confounds and amaze. Once you know the basics then it’s not too bad.

So we want our script to run every 10 minutes, this doesn’t overload the Pi, gives R plenty of time to get through the results and it keeps the Twitter rate limit happy too. All this is up to you to change for your searching requirements though.

First open up the Pi user’s cron tab with:

crontab -e

Then at the bottom of the file type the following:

*/10 * * * * /home/pi/rpisentiment/runner.sh > /home/pi/rpisentiment.log

What’s happening is we’re instructing our runner.sh file to run every ten minutes.  For more details on cron have a look at the man page or the page on Wikipedia.

You can see any problems or progress in the rpisentiment.log file, any output created goes there.

Congratulations you have a sentiment server. Okay it only services one search phrase on limited hardware but still, it does work 🙂

You can download ALL the required files and scripts here.




Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s

This site uses Akismet to reduce spam. Learn how your comment data is processed.

%d bloggers like this: