software engineering

Count occurrences with grep

By Adam K Dean on

Today I learnt that grep has a '-c' switch that counts occurrences. I'll never pipe it to 'wc -l' again!

$ ps aux | grep -c adam

But if we use wc, we would also need to trim it:

$ ps aux | grep adam | wc -l

So, use -c.

Fleetctl destroy all services

By Adam K Dean on

Here is a nice little bash trick to destroy all services using fleetctl.

fleetctl list-units | sed 1d | while read -r line ; \
    do fleetctl destroy $(echo $line | cut -f1 -d ' '); done

It gets a list of units, removes the first line, loops through, grabs the first word, and and destroys it.

$ fleetctl list-units | sed 1d | while read -r line ; \
    do fleetctl destroy $(echo $line | cut -f1 -d ' '); done
Destroyed Job august-frosting_v4.web.1-announce.service
Destroyed Job august-frosting_v4.web.1-log.service
Destroyed Job august-frosting_v4.web.1.service
Destroyed Job august-frosting_v4.web.2-announce.service
Destroyed Job august-frosting_v4.web.2-log.service
Destroyed Job august-frosting_v4.web.2.service
Destroyed Job quaint-teamwork_v5.cmd.1-announce.service
Destroyed Job quaint-teamwork_v5.cmd.1-log.service
Destroyed Job quaint-teamwork_v5.cmd.1.service
Destroyed Job quaint-teamwork_v5.cmd.2-announce.service
Destroyed Job quaint-teamwork_v5.cmd.2-log.service
Destroyed Job quaint-teamwork_v5.cmd.2.service

Now imagine typing each of those by hand.

Detect if JS is running under Node

By Adam K Dean on

Here is a snippet for how to detect if JavaScript is running under Node:

var isNode = typeof process !== "undefined" && 
    {} === "[object process]";

We check here whether the variable process is defined, and if it is, we check it's type to make sure it's the proper process object and not just a regular old JavaScript object.

Scale a Dockerfile app in Deis

By Adam K Dean on

Deis allows you to run apps in two different ways: using a Procfile, and using a Dockerfile. When using a Procfile, you define what type of processes you'll be running, such as:

web: node index.js

To scale this, say to 2 instances, it's as simple as:

$ deis ps:scale web=2

This doesn't work when using the Dockerfile approach however. For that, we can see from ps:list that the instance type is actually cmd.

$ deis ps:list
--- cmd:
cmd.1 up (v2)

If we were using a Procfile, this would say web.1 up (v2) etc. So to scale the Dockerfile, we need to use cmd rather than web.

$ deis ps:scale cmd=2

And then, once it's got coffee, it'll scale your apps.

RDP via OpenSSH

By Adam K Dean on

I wrote about connecting to RDP over SSH, but that relied upon PuTTY. As I'm now using OSX, I have access to OpenSSH. This makes things easier.

ssh -L <localport>:<remotehost>:<remoteport> <sshserver>

For example:

ssh -L 8001:

Now you can connect to local port 8001 with RDP.