cronjob to future at 42. tweet.

Kalindi
6 min readDec 1, 2018

--

Most of the projects at 42 are code code code and that’s fun, but some oh dear, are not! One of those is also the twitter project! It felt silly at first, and why would I even put effort into one more social media that promotes an even lower attention span rate with its mega random constant stream of stuff that I mostly don’t care about, but then a project is a project, and hey I got into it:

LOL

And then, actually a project - and well life too - is what you make of it.

So, I decided I’ll learn about automation and bots. I took a dip into this a couple months ago, and my bot is really sweet:

and pretty clever:

But it’s time to step up the game!

And so I learned about:

part of our project pdf #42life

So, let me tell you something about:

Cronjobs and sending scheduled tweets from the command line

There are two parts to it: setting up twitter auth from the computer and then setting up cronjobs. So the first thing I had to figure out was how to send tweets from the command line. After a bit of looking around and trying various lines and random post requests, I found these pretty useful and straightforward instructions: a-guide-to-the-twitter-api-and-twurl. I would have wished to do more code from scratch stuff, but I figured I can do it another time. It’s a twitter inhouse curl (wikipedia: cURL is a command-line tool for getting or sending files using URL syntax.) that includes authorisation too. So then as I set it up started my usual learning routine:

1. “I wanna do this”
2. I try to find ways to do this
3. I manage to do this

So I managed to send a message over CLI (command line interface):

twurl -d "status=Posting from the commandline a link to a post about posting from the command line https://medium.com/p/1e9081815e41" /1.1/statuses/update.json

Basically: twurl -d "status=tweet" /1.1/statuses/update.json

Sometimes step 3 doesn’t really work out that well, and then I adapt step one a bit, but it’s usually somewhere along these lines. I actually wanted to retweet with text as well but never managed that. This is what I tried: twurl -d ‘status=retweet tweet’ /1.1/statuses/retweet/1068368475356188672.json and this is what I got:

retweet, yeah, but where is the text???

I found the id used in the POST request in the respose info of the previous tweet (if you add | jq at the end of the request the json response gets nicely formatted).

The ID is also in the link to the tweet itself: https://twitter.com/KalindiFonda/status/1068368475356188672

So, got the retweet, but got no text. I tried a bit, but then got distracted by my new pursuit: scheduling and automating tweets. I stumbled upon the million-gazzilion tweeting media suites that are used to manage multiple Twitter or other social media accounts. One of their features is scheduling posts, but I was interested in finding a more codey way, and having tried heroku twitter scheduling before I decided to try to do it from the command line:

So pretty

This is the cron notation for a minute. time time time time time.

The asterisks stand for: minute, hour, day in month, month, day in week.
I was mesmerised and intrigued by this image of time. It felt very appropriate, a good visual representation of moments, as well as a certain feeling of or “this is how it goes, forward, relentlessly, forever, over and over”.

So crons from wikipedia: “The software utility cron is a time-based job scheduler in Unix-like computer operating systems.” Just what I needed. After some reading I figured the notation * * * * * command is used to do this. Basically:
* * * * * twurl -d 'status=some time pun' /1.1/statuses/update.json

And that’s how I scheduled some tweets. One a minute to be exact.
crontab -e in the terminal opens a file where the above commands can go.
I encountered an error where the jobs didn’t run because the path to twurl wasn’t found, but I solved that by adding everything thatecho $PATH gave me to the top of the crontab -e doc. I then looked for ways in which I could read the first line from a file, post that on twitter and then take the next line and post that too. Look at this mess:

* * * * * line=status=$(head -n 1 ~/Desktop/to_tweet.txt) ; head -n 1 ~/Desktop/to_tweet.txt >> ~/Desktop/tweeted.txt ; tail -n +2 ~/Desktop/to_tweet.txt > to_tweet.txt.tmp && mv to_tweet.txt.tmp ~/Desktop/to_tweet.txt ; twurl -d "$line" /1.1/statuses/update.json

but it works.

So, it puts the tweet to text into the line variable by reading the first line from the to_tweet.txt file. Then it takes that first line and puts it into a tweeted.txt file, and then it deletes that first line by putting all the other lines to a temp file and then moving the temp file back to to_tweet.txt, and then finally calling the twurl command with the $line variable.

Boom boom boom.

I don’t think cron jobs are a great way to go about scheduling tweets, but I learned about their existence now, and maybe one day I’ll use them for something useful, like backups or mailing failure logs or actually I don’t really know what I can do with it. If I’ll continue tweeting from the command line, (and won’t use one of the many feature full tools that one can find for exactly that) the at command is much better for scheduling one time events (tip for mac users), and sleep is an option too. But at seems best:

Typing at 1042 opens the stdin where one can type command(s) to run at specific times (ctrl + D closes it). echo "com m and" | at 1042 will do to! Check this out:

My computer waiting for more!

What happens above: I check the date - I set a job at 1313 - I get an email (I can see the You’ve got mail prompt after running any command, in this case date, to see a minute has passed) - I read the email with the cat command. The email shows me what the job that ran put into the std output.

An at job for a tweet would look something like this:

Uh oh, I have a deadline now! : S

So I have until 22.42. Gotta write! But if I don’t manage, I guess I can still resort to atrm JOBNUM, and delete my queued job. But I’ll try filling up the blanks and editing this post by then! Another good ride at 42!!

After saying all this, I still can’t confidently say Follow me on twitter because I am still not sure it is a good venue to spend life hours and stuff (it’s so easy to get distracted and other more dedicated places can do a better job at supporting, motivating and directing behaviour one wants), but here I am and it was fun, so yeah:

Follow me on twitter or wha?

As I am nearing the 28 days of twitter presence that this project asks for, I googled its name and found out its meaning:

Good things here I come.

--

--