chrislynch.link

How To

Find (and delete) old files in Linux

I’ve got a confession to make – I’m a log file addict. I love a good log file and I’ll hold on to those bad boys for way longer than I should sometimes. Even I need to clear down the odd /logs directory every now and then though, and that’s where this handy little piece of command-line-fu comes into play.

find . -type f -mtime +90 

As you might have guessed, this finds all the files in the current directory that are over 90 days old. You can change the +90 to any other value to tighten or widen your search.

find . -type f -mtime +90 -delete

This version, with the addition of -delete automatically deletes the files that it finds. Use with caution; there’s no confirmation and no going back. Always run the find without -delete first to check what you are about to get rid of.

Find files that have changed in Linux

I’ll confess, I stole this from somewhere on Stack Overflow and I’m posting it here just because it’s so flipping useful I know I will need it again some day.

find $1 -type f -print0 | xargs -0 stat --format '%Y :%y %n' | sort -nr | cut -d: -f2- | head -n 20

Yes, this little bit of command-line voodoo will tell you all the files that have changed in the current directory (and its subdirectories) by putting them in datestamp order. It returns the top 20 by default, just change the 20 at the end to get a longer (or shorter) list.

Run code-server with Apache

code-server is nothing short of amazing. A fully-fledged VSCode environment running in a browser means I can jump from one machine to another, including my beloved Pixelbook, with full access to my development environment and toolset. After months of subscribing to services like Codeanywhere, CodeTasty, replacing this with something under my control (and costing me not a red cent) is pretty cool.

That’s not to say setting it up is easy, especially if you tend to throw all your development skunk-works type activity onto one disposable server as I do. One particular problem is running the system securely over SSL when you already have a web server, like Apache, running on the device.

Keeping Code-Server secure when running remotely

If you’re going to run code-server and make it accessible remotely, you want to do that over SSL. The risks of losing control of your development environment are too great otherwise. The easiest way to do this, in my opinion, is with caddy. Caddy is a web server written in Go that, very handily, grabs an SSL certificate for you when you need it and takes care of your SSL needs. Caddy can create a reverse proxy to code-server, letting you access it over SSL on port 443 easily even though it is really running on some other port internally.

The problem with this is that Caddy needs to be running on port 80 and port 443 to perform its SSL magic and it needs to be able to respond to a reverse-lookup from Let’s Encrypt to get an SSL certificate. This means you can’t be running Apache (or any other web server) on those ports.

Caveat: Yes, you can run code-server remotely this quicker and cheaper, but where’s the fun in that?

If you want to keep things simple, you can of course just spin up an extra image on your cloud host of choice and use one of code-server’s really handy cloud images to install your code-server instance. No fuss, no muss. You don’t have to put all your stuff on one machine and it’s probably advisable not to in a lot of scenarios. However, if you (like me) need, or just want, to run everything off one device then this post will show you do it…


If you don’t already have SSL certificates for your domain, get them

This method won’t work if you’re accessing your server by its IP (or it might, I just haven’t tried).

Use LetsEncrypt to get your domain a certificate if you don’t already have one.

Install Code Server

Install code-server exactly as described by code-server themselves. Trust them, they’re the experts.

Don’t run it yet though, you’re going to make some changes to the config file

Install Caddy

Install Caddy as per the instructions here but (like code-server)… don’t run it yet!

Now, start editing your config files…

code-server config

code server needs to be configured to require no password to access it (Caddy will handle authentication) and use no SSL (Caddy will handle that too)

It doesn’t matter what port code server runs on, as long as it’s a free port, but pay attention to the port as you will need it later.

My code-server config looks like this:

bind-addr: 127.0.0.1:8080
auth: none
cert: false

Caddy config

Caddy needs to be told to

  1. Run on non-standard ports for HTTP and HTTPS.
  2. Use the SSL certificates you already have (see above) rather than try to generate its own
  3. Run a reverse proxy to code server
  4. Require HTTPS authentication to allow access

My Caddy file looks like this:

{
        http_port 9080
        https_port 9443
}

mydomain:9999 {

        basicauth * {
                username passwordhash
        }
        reverse_proxy 127.0.0.1:8080
        tls /path/to/ssl/file /path/to/ssl/key

}

Replace mydomain:9999 with your domain and the port you want to run on.

Replace username and passwordhash with a username and password hash (you can generate a password hash here).

Replace the /path/to/ssl/file and /path/to/ssl/key with paths to the relevant files on your machine. If you generated these with LetsEncrypt and you’re using Apache, like me, you will find these in the configuration file for the domain itself.

Firewall config

It’s unlikely that the ports you picked for Caddy are going to be open on your firewall. (If they are, they probably belong to something already or your firewall config is leaky). You need to ensure that the SSL port Caddy is running on is able to accept inbound connections. You may choose to lock this down to known IP addresses if you have fixed IP addresses that you are going to be remotely accessing the system from.

You’re good to go

Start the code-server service and, once it is running, start Caddy.

code-server &
caddy start

You should be able to connect remotely to your development environment at https://[your-domain]:[caddy-ssl-port]

You will be prompted for the password you configured in the Caddy file (above) – get it right and you should see your remotely accessible code-server in your browser.

How to get the MD5 Fingerprint RSA Key for a Linux Server

If you’re writing code to connect to a Linux server over SSH or SFTP, you may need to get the MD5 “fingerprint” of the server. It’s that funny little string of numbers that appears, just once, when you connect to a server and you never see again unless it changes…

ssh-keygen -l -E md5 -f <(ssh-keyscan localhost 2>/dev/null)

Cron job to keep MySQL Running

If your MySQL server is randomly falling over, you need to fix it. There’s no good reason to let that nonsense continue. Sometimes though, that’s easier said than done and whilst you’re working on why the server keeps falling over you need something to keep bringing it back up

Here’s a little snippet of code that will do just that. Call it from a CRON job and your server will be checked and started back up as regularly as you need it to be.

systemctl is-active --quiet mysql || systemctl restart mysql

The Rules of Social Media: How to Protect Your Business from Changing Algorithms

I have a few rules when it comes to building online engagement through social media. They’re not designed to enable “get rich quick” tactics, but to ensure longevity and robustness for businesses who rely on the traffic that they get from social media to fuel their businesses.

Rule 1 of understanding social media:

“If you’re not paying for the product, then you are the product”

The balance of power between you and a social network will always be in favour of the network. The more you want from the network, the more you have to give the network.

Rule 2 of understanding social media:

Social networks make money when you watch and/or click on ads and everything they do is to make you watch and click more.

The more time that you spend on the platform, the more ads you see and the more likely you are to click on something, making the platform money.

The first purpose of “the algorithm” – that mysterious machine that decides what you see and what you don’t see on each social network – is not to show you the *best* things or the most *relevant* things but the things that are most likely to engage you and keep you on the platform.

Rule 3 of understanding social media

The platform is nothing without your content, but the platform owes you absolutely nothing.

This might be a tough pill to swallow, but it is true. Facebook is a business. LinkedIn in a business. Twitter is a business. YouTube is a business. It’s not a community, it’s certainly not a family (or “fam”), and it will do what is best for its profits not what is best for you.

YouTube frequently changes its algorithm and YouTubers complain bitterly that they have lost followers, lost views on their videos, and lost revenue. The interesting thing? YouTube doesn’t lose revenue when this happens. YouTube makes more money when this happens.

What does this mean for me?

In short – never build your business on a platform you don’t control and never, ever a single social media platform. Never assume that because you can sell on Facebook today, or sell on Instagram today, that those platforms will be there forever and that their algorithms will always work for you.

Social media platforms must be treated in the same way that you would treat a print advert, or a poster, or getting your brand name on the back of the bus. It might be your message, but its not your means of delivery. The audience isn’t your audience, you’re only renting them and you pay that rent by creating engaging content for the network.

Your aim should always be to have your audience engage with you on a platform that you own. Your website is the ideal vehicle for this.

So, I just link to my website then, right?

No, not exactly. Remember Rule 2?

Unless you are paying for that click, the social network isn’t going to be keen to let you have it. Some networks make it more difficult than others to even include a link (Instagram, for example) whilst others are widely agreed to limit the exposure of posts with links in them (such as LinkedIn, where users often put links in comments rather than in the body of the post).

The ideal way to migrate your customer from your social media channels if you’re selling a product is to include something with the product, like a discount voucher, that you can only use via your business website and not through a social media channel.

If you’re in the content game, make sure you have exclusive, high quality content on your website that isn’t available anywhere else. Not only will this increase your organic exposure as search engines find this content and index it, but it is something you can point your audience to with a simple “check out my website for exclusive content” message *without* posting a link – all the social media platforms will let you include a link to your own website in your bio or profile.

Should I just come off social media?

Absolutely not – just make sure you understand that posting on social media is a transaction both with the platform and with your audience and taking nothing for granted. Today’s Twitter is tomorrow’s MySpace…

(Actually, today’s Twitter might already be today’s MySpace. Maybe today’s TikTok is tomorrow’s Twitter? I hate time travel…)

How to Simulcast with OBS and Restream.io

Live streaming is a great way to create new video content. If you’re comfortable “winging it” and talking on the fly, it can also be a lot of fun and much more interactive than recording and then sharing videos.

YouTube player

There are lots of places, or “platforms”, that you chose for your live stream; Facebook, YouTube, Twitch, LinkedIn, etc. Which one you pick can have a big impact on how big an audience is and how they interact with you. So, what if your audience is split across multiple platforms, or you’re trying to build a new audience on one platform without losing your existing audience on another?

Enter Simulcasting

What is Simulcasting?

Simulcasting is the process of sharing the same stream across multiple platforms at the same time. Simulcasting = Simultaneous Casting.

It’s not the easiest thing in the world to set up but thankfully there are some great online services that help with this. The one that I’ve been playing with is [restream.io](https://restream.io/).

What is restream.io?

restream.io allows you to connect a single restream.io account to multiple streaming platforms simultaneously. A free account will connect you to any number of platforms with a single account per platform – if you want to connect to business pages and the like, you will need a paid account.

Using restream.io

Once you’ve signed up for an account; which just requires a username, email, and password; you should be able to pick from the list of supported platforms and connect them to restream.io.

This is the same OAuth process as letting any other tool access your Facebook, YouTube, or another account so it’s mostly just point and click to authorise everything.

Once you’ve done that you can either stream from the web browser directly (which didn’t actually work for me – see below) or from a streaming tool.

What didn’t work?

My first attempt to stream directly through restream.io just… didn’t work. There was a long wait whilst it promised me that it was connecting and then, just when I thought maybe it had connected all I seemed to have managed to do was make a video in which I appeared to be made of a semi-inert pile of Lego bricks.

Not a great look.

Enter OBS / How to make restream.io work properly.

For my second attempt, I used OBS (Open Broadcast Studio). I’ve been using OBS for a long time to record videos, but never for streaming (because, well, I hadn’t streamed before!).

OBS only supports a single stream destination, but that’s fine because you can then use restream.io to cast that stream to different platforms. I tested using Facebook and YouTube Events (which I’d already connected to restream.io)

Before you start streaming, you will need to set up a streaming target. This lives in the settings window. There is a dropdown list of targets – you may need to click the “Show All” option to get the option for restream.io, but it is there.

In the current version of OBS once you do pick restream.io from your list of streaming targets you will have the option to log into your restream.io account.

This is highly recommended – no messing about with RMTP server addresses and stream keys, just log in and everything should connect automatically.

From there, it was just a case of hitting “Start Streaming” and… the rest is a glorious piece of internet history.