Back to the Future With Cron Jobs and Ruby Gems

It’s Back to the Future Day!

Back to the Future is my all-time favorite movie — and the sequels are a close second and third — so this is a pretty special day. To celebrate, I’ll discuss time travel!

Sort of. I’ll discuss cron jobs.

A cron job is a way to schedule tasks ahead of time in Unix-type command-line systems such as the Mac Terminal — for example, sending out an email, updating a file at the same time every day, and so on. Tasks can be scheduled for a particular time or at regular intervals — daily, weekly, etc.

I decided I wanted to post the following tweet on October 21, 2015, at 4:29 pm, to celebrate Marty McFly’s arrival from 1985:

Yes, it's October 21, 2015 at 4:29 pm...

...on the East Coast.

But Hill Valley is in California. Three hours to go.

#BackToTheFuture

I wanted to preschedule my tweet, so it could go out even if I’m away from my computer at the time (as long as my computer is running). First I tried some web apps, but I couldn’t get tweets to appear at the exact time I wanted. So I turned to the command line. Here’s how this works.

(1) Using the “t” Ruby gem

The first step is to install a Ruby gem called t, which provides command-line access to Twitter:

gem install t

After connecting your Twitter account to the gem by following the instructions on the gem’s GitHub page, you can tweet directly from the command line by typing

t update "[text of your tweet]"

The text of your tweet will immediately show up on Twitter as a tweet from your account.

But what if you want to pre-schedule a tweet? What if you want to run the above command not now, but later? You’ll need to set up a cron job.

(2) Setting up a cron job

This involves two steps: (a) setting up a file that contains your “t” command, and (b) setting up the actual cron job.

(a) Setting up the file containing your “t” command

The best place to create this file (or shell script) is in your home directory. To find out where this is, type:

echo $HOME

My home directory is “Users/[myname].”

You can go there directly by typing

cd $HOME

In your home directory, create a new file with a .sh extension, which is the extension for a shell script:

touch [filename].sh

Open this empty file in your text editor. At the top of the empty file, type

#!/bin/bash

(The exact path might be different on different computers, but this worked for me on a Mac.)

Next, you can type the “t” update command, but you have to preface the “t” with something. Ruby Version Manager (rvm) creates wrappers for gems, and you need to preface the “t” with the path for the wrappers. This link helped me figure that path out. For me, that path is /Users/jeffslutzky/.rvm/wrappers/ruby-2.2.3, so the full path to “t” is:

/Users/jeffslutzky/.rvm/wrappers/ruby-2.2.3/t

And therefore the full command is:

/Users/jeffslutzky/.rvm/wrappers/ruby-2.2.3/t update "[tweet text]"

So the full file is these two lines:

#!/bin/bash

/Users/jeffslutzky/.rvm/wrappers/ruby-2.2.3/t update "[tweet text]"

After creating the shell script, we have to change its permissions, by going back to the command line and typing

chmod +x [filename].sh

After creating the shell script and setting its permissions, the next step is to set up the cron job.

(b) Setting up the cron job

We’re actually setting up a crontab, which is the text file that contains the schedule of tasks. I was having problems using my default text editor, Sublime, for crontabs, so I had to set my default crontab editor to Nano by typing this on the command line:

export VISUAL=nano; crontab -e

After that, open the crontab file by typing:

crontab -e

This file requires one line containing two things: a series of five numbers corresponding to the time we want the task to run, and the path to the shell script we just wrote.

The numbers come in this order: minutes, hours (in 24-hour format), days of month, months, days of week. I don’t care about the day of the week, so I can replace that with an asterisk (*). For example, October 21 at 4:29 pm (i.e. 10/21 at 16:29) is:

29 16 21 10 *

That’s just 10/21 – 16:29 in reverse order.

The path to my shell script is:

/Users/jeffslutzky/bttf.sh

So, adding this to the time, the full line to type in for this task is:

29 16 21 10 * /Users/jeffslutzky/bttf.sh

Save the file. That’s it. The tweet is now pre-scheduled. We’re all done!

To see the contents of the crontab, type:

crontab -l

To edit it again, type:

crontab -e

You can even add other tasks to the crontab file by adding a new line with a new time and shell script path.

I’ve actually pre-scheduled two tweets. If my calculations are correct, when this baby hits 88 miles per hour, you’re gonna see some serious tweets (at twitter.com/tinmanic).

To recap:

(1) Create a file with an .sh extension containing a command-line action, and set the file permissions.

#!/bin/bash

/Users/jeffslutzky/.rvm/wrappers/ruby-2.2.3/t update "[tweet text]"

(2) Create a new task in the crontab to execute the above file at a chosen time.

29 16 21 10 * /Users/jeffslutzky/bttf.sh

Helpful links

delorean

Sublime Hints and Shortcuts

I’ve completed my first week as a student in the Flatiron School’s Web Development Immersive program. It’s been exciting and fun and busy and I’ve met friendly, interesting people. It’s the best experience I’ve had in ages.

In this blog post I’ll cover Sublime. Sublime is the default text editor we use at the Flatiron School; we use it to write all our Ruby programs, and I’m using it to write this very blog post. Besides Terminal, it’s probably the program we use most often. Therefore, knowing some Sublime tips and shortcuts can help a programmer become much more efficient. One of our instructors sent us a link to a whole bunch of Sublime shortcuts. I thought I’d cover some of those, and some other tips I’ve found most useful so far:

Setting up your visual environment. Since you’ll be staring at the Sublime interface all the time, you’ll want the screen to look pleasant. The default environment for Sublime is light text on a black background. I find that to be hard on my eyes, but fortunately you can change this. In the menu, go to Sublime Text > Preferences > Color Scheme. Under this menu, Flatiron’s Sublime installation has an option called Colorsublime-Themes, where you can pick Solarized Dark or Solarized Light. I use Solarized Light, which gives you a soothing beige background:

solarized_light

There are a ton of other options in the menu under Color Scheme – Default.

Multiple column view. Most Flatiron labs require getting certain tests to pass. It can be helpful to be able to look at your code and the test at the same time, especally when the test includes some data, such as a sample hash that you need to transform into another hash, or some supplied variables. To do this, you can press OPTION+CONTROL plus the number of columns you want. Pressing OPTION+CTRL+2 will give you two columns, and then you can drag any of the open tabs to any column:

two_column_view

Since this will give you narrower columns, you can then go to the menu bar and select View > Word Wrap so everything remains on the screen.

To switch back to single-column view, press OPTION+CTRL+1.

Comment out a line from anywhere on that line. Often I’m using binding.pry to look into my program, but then I want to stop using binding.pry and just see if my tests pass. Instead of repeatedly typing and deleting “binding.pry,” you can just comment it out by typing COMMAND and the slash key (/). The nice thing is that you don’t even have to be on the beginning of the line. If you type COMMAND + / from anywhere on that line, it will put a pound sign at the beginning of the line and comment out the whole line. To un-comment a line, just press COMMAND + / again.

Replace every instance of some text with different text. Sometimes you’ve named a variable but later decide you want to change it to something else. You can easily change every instance of the variable in your program. Just highlight any instance of the text, and every instance of that text will then have an outline around it:

highlighted_text

Now, press CTRL+COMMAND+G and you can then edit every single instance of that text at once! Everything you type will affect every instance of that text in your program:

edited_text

When you’re done, click anywhere in your program to stop. So cool.

(Just be careful – if your program contains, for example, “item” and “items,” you won’t be able to edit “item” without also editing “items.”)

Move an entire chunk of text up or down. If you want to move some text to a different part of your program – for example, if you want to rearrange the order of your methods – just highlight the lines of text you want to move, then click CTRL-COMMAND and the up-arrow or down-arrow. The entire chunk of text will move up or down a line each time you click.

There are so many other shortcuts in Sublime. It would be hard to memorize them all at once, but the more I use Sublime, the more I’ll probably become familiar with them. You will, too.

Sublime is… sublime.