Server

Update Bower dependencies automatically via Cron jobs

Automatic updates to your bower dependencies might not be the use case for the usual developer who uses Bower.

I didn’t even think of a day where I would want my bower updates to be handled every day, 365 days a year.

I built materialize.khophi.co to help me solve a problem I’ve always had with custom building MaterializeCSS before use. Since the app runs from the browser, I hooked it up to the web on my droplet.

On my local development box, I’m happy to update my bower dependencies as and when I want. However, on the server, keeping the bower dependencies, especially the core dependency, MaterializeCSS up to date always is important since it will be bad for business should a user try to download the latest of the custom CSS but get’s an older version in return.

So, on the server, a cron task to run daily for such a purpose will be great.

The Bower update script

Call it auto_update_bower.sh and put in this:

#!/bin/bash
cd ~/material && bower update

Remember to make your script executable by way of this:

chmod +x auto_update_bower.sh

The above auto_update_bower.sh remains as it is at the moment, assuming our Bower root folder is in the /home/whateveruser/material/ .

We change to that directory using the ~ (tilde) which resolves to the home directory of the current user, then run the bower update command

Our Cron Job

Time to reference this script from our cron job. You might just wanna run this job from user space, so simply check to see if you have any already existing cron jobs for the current user.

$ cd ~
$ pwd
/home/myusername/
$ crontab -l

The last command should list your available commands. Then, we wanna append a new task, so we do

$ crontab -e

Then let’s start typing:

0 18 * * * /path/to/script/update_material_bower.sh >> /path/to/log.log 2>&1

Wonder what the 0 18 * * * is for? Check out this beautiful website that helps you visualize Cron commands in English!

Crontab.guru – Enter your cront time and see what it means

You got what the above time means? That will be when the task will be run. Any outputs from the running of the script will be stored in the /path/to/log.log file specified.

The 2>&1 indicates that the standard error (2>) is redirected to the same file descriptor that is pointed by the standard output (&1).

So, if your use case demands you update your Bower project automatically, without any worry of having broken dependencies or whatnot, you’re good to go with a simple Cron job!

Related Articles

Back to top button