Running a script on linux boot as a user

As a part of getting build services for a project up and running (GitLab is freaking cool by the by), I decided it might be a good idea to set up a local NPM cache to help speed up restoration. Yeah, I know there is a local cache as well, but thats not as fun to tinker with!

Firstly, I set up an internal cache as per the instructions by Alex. Then, once I was happy with the now snappy restore process, looked into getting the service to start on boot.

First, switch over to the root user, as we will need this to create, and register our script

sudo su - root

Now, head over to the init.d folder

cd /etc/init.d

Now, we create our startup script. This can be anything, just as long as it is descriptive! Some sources suggest creating a single script for all your custom actions, but I am a fan of atomicity;

Now that vim is cracked open, throw the following contents in

su - username -c "~/.npm-packages/bin/forever ~/.npm-packages/bin/npm-proxy-cache -e -t 600000 -h http://your-endpoint-here/"

I found that when running su with a command, my distro (Ubuntu 16.04.2 64) required the command in the format su - -c "". Your distro may vary.
Also, I found that at this stage, $PATH may not yet be instantiated, and set, so I just used the direct path to where I knew the package would be.

Next, we need to make the script executable, and register the init script with the startup service

chmod +c ./
update-rc.d ./ defaults 100

And there we go! We should now have our cache running on startup.