December 09, 2016

Raspberry Pi Bot Server

My trusty RPi3 twitter bot server "starkiller".

So let's say you've got a twitter bot that you want running 24/7, but you don't want it running on your personal system. What you need here is a server, which is really just a dedicated system. Note, if you're looking for a web server, you'd be better off with Amazon's AWS .

If you're running automated tasks and answering tweets however, a Raspberry Pi is an excellent choice. 

The steps are pretty straightforward:

1. Setup a computer (raspberry pi) to run your program.
2. Write a script to launch your program.

3. Run the script automatically at startup.

Setup your computer

The starting point here is simply to buy a Raspberry Pi. RPi3 is great because it has build in wifi, and it's very affordable, any generation will work though. Once you've got the hardware, you need to install an OS. I strongly recommend Raspbian Jessie with Pixel (download and install guide can be found here).

Once your pi is running and connected to the internet, you need to transfer your bot's executable or script to the raspberry pi. Here is a guide to using scp for this purpose, or you can just email it to yourself.

Write a launch script

Now you need to write a script to run the bot simply containing the line you would type into the terminal. To do this, open terminal and run:

$> nano

This opens a basic text editor. In this file, you're just going to have one line - the command you would use to run using a terminal, with a fully resolved path:

$> python3 <path_to_file>/

To close, press ctrl-x, and then "y" to confirm the filename. Run pwd now to get your current working directory. This gives you the full path to your script "".

Schedule your task

Now we are going to make our script executable with chmod, and schedule this script to run at startup using cron. This is done by running:

$>chmod +x <path_to_file>/
$>crontab -e

Select "2" to use nano as your editor, and scroll to the bottom of the text. Add the following line and make sure to press enter to add the line break:

@reboot <path_to_file>/

Example crontab added line

Now close crontab using ctrl-x, y, which will display a dialogue indicating a new crontab was installed.  Run "$>sudo reboot -n" to reboot your pi, and when it starts up again your bot should be automatically running!

To confirm it is running, try using this command to view log messages related to cron tasks.

$>grep cron /var/log/syslog

Nice work! Now you've got a Raspberry Pi server. Time to add some more automation.

No comments:

Post a Comment