Back to homepage
#cron#vps#server#javascript

Deploy Your Own Cron Jobs Server on a VPS in 9 Simple Steps!

18 min read

This guide focuses on automating tasks efficiently by setting up a cron jobs server on a VPS. Using OVH or another provider, it walks you through creating a GitHub repository, connecting to your VPS, installing necessary tools like Node.js and Git, linking your VPS to GitHub via SSH, and implementing continuous deployment with GitHub Actions. Finally, it covers testing your cron jobs, configuring a crontab, and using Crontab-ui for easier management.

Deploy Your Own Cron Jobs Server on a VPS in 9 Simple Steps!

In today's fast-paced development world, automation is the name of the game. Whether you're scheduling periodic data backups, sending out newsletters, or performing system maintenance tasks, cron jobs are an indispensable tool. But, how do you set up and manage these scheduled tasks efficiently? In this comprehensive guide, I will walk you through a streamlined process to deploy your very own cron jobs server on a VPS. Leveraging the power of OVH's VPS (or any other provider you prefer), we'll dive deep, ensuring that by the end, you'll have a fully functional cron jobs server at your disposal. Let's get those tasks automated!

Table of Contents

  1. Setting Up a Github Repository with a Sample Cron
  2. Renting a VPS with OVH
  3. Establishing a Connection to Your VPS using Putty
  4. Installing Node, Npm, and Git on Your VPS
  5. Generating an SSH Key on your VPS and Linking it to Github
  6. Cloning Your Github Repository to the VPS
  7. Implementing Continuous Deployment with Github Actions
  8. Testing Your Cron on the VPS and Setting Up a Crontab
  9. Installing and Configuring Crontab-ui

Prerequisites

Before diving into the world of cron jobs and VPS deployments, it's essential to ensure you're well-equipped. Here's a checklist of what you should have and know:

  • Basic Understanding of Cron Jobs: Familiarize yourself with the concept of cron jobs and how they function.
  • Familiarity with Github: Since our project will be hosted on Github, a basic understanding of repositories, commits, and cloning is essential.
  • Command-Line Comfort: A lot of our tasks will involve using the command line, whether it's on your local machine or the VPS.
  • Knowledge of SSH: Secure Shell (SSH) is a protocol we'll use extensively, especially when connecting to our VPS and handling Github operations.
  • An Active Github Account: Ensure you have an account set up and ready to host your project.
  • A VPS Provider in Mind: While this guide leans towards OVH, you can choose any provider you're comfortable with.

Once you've ticked off these prerequisites, you're all set to journey through deploying your own cron jobs server on a VPS. Let's dive right in!

Step 1. Setting Up a Github Repository with a Sample Cron

In this section, we'll see how to set up a Node.js project, equipped with a sample cron job. By the end of this guide, you will have a working cron job that greets the world with a friendly “Hello World”.

Setting up a Node.js and npm project

First things first, let's set up a fresh Node.js project:

  • Initialization: Navigate to the folder where you want your project to reside and run the following command in your terminal:
bash
1npm init -y

This command initializes a new Node.js project with default settings. You should see a package.json file in your directory once done.

  • Installing Dependencies: Since our project is using the commander library, let's get it installed. In your terminal, run:
bash
1npm install commander --save

Setting up the Cron Job

  • Setting up the Entry Point (**index.js**): Create a file named index.js in the root directory. This file will act as the entry point of our application. Here's what you need to put inside:
javascript
1const commander = require('commander');
2
3commander
4 .version('1.0.0', '-v, --version')
5 .description('...')
6 .option('-c, --cron <type>', 'cron script name');
7
8commander.parse(process.argv);
9const options = commander.opts();
10const cron = options.cron;
11
12(async () => {
13 console.log(`Start`);
14 try {
15 const cronJob = require(`./crons/${cron}.js`);
16 await cronJob.start();
17 logger.info(`End`);
18 process.exit(0);
19 } catch (err) {
20 console.error(err);
21 process.exit(1);
22 }
23})();

This code sets up commander to pick up the -c flag and the corresponding script name.

  • The Shell Script (**cron.sh**): Create a file named cron.sh in your project root and add the following content:
bash
1#!/bin/sh
2node index.js -c $1

Remember to give the shell script execute permissions:

bash
1chmod +x cron.sh
  • Creating the **hello** cron script: Inside your project directory, create a new folder named crons. Inside this folder, create a new file named hello.js and add:
javascript
1const start = async () => {
2 // This function is asynchronous, should you require that feature
3 console.log('Hello World');
4}
5
6module.exports = { start }
  • Adding a Start Script: In your package.json, add the following script:
json
1"scripts": {
2 "start": "./cron.sh"
3}

With this, running npm run start hello will trigger our hello cron job, which will log "Hello World".

Pushing to a Github Repository

Now that our project is all set, let's get it up on Github:

  • Create a New Repository: Go to Github and create a new repository.
  • Link the Repository to Your Local Project: Navigate back to your project directory in the terminal. Initialize a git repository and add the remote repository:
bash
1git init
2git remote add origin [YOUR-GITHUB-REPOSITORY-URL]
  • Commit and Push: Add your project files to the staging area, commit, and then push them to Github:
bash
1git add .
2git commit -m "Initial commit with a sample cron job"
3git push -u origin main

And voila! Your Node.js cron job project is now hosted on Github. As we move forward, we'll leverage this setup to integrate with our VPS and automate tasks efficiently. Onward!

Step 2: Renting a VPS with OVH (or your chosen provider)

In this section, we'll delve into what a VPS is, why you might need one, and guide you through the process of renting a VPS from OVH, a popular cloud provider.

Understanding a VPS

A Virtual Private Server (VPS) is essentially a virtual machine sold as a service by an Internet hosting provider. It runs its own copy of an operating system (OS), and customers have superuser-level access to that instance, enabling them to install nearly any software that runs on that OS.

In simpler terms, a VPS is like having your own personal server on the internet. Think of it as a dedicated portion of a physical server, isolated from other users. It provides more control and isolation than shared hosting solutions, without the cost and complexity of a dedicated physical server.

Key Terms and Notions

  • Host: This refers to the main server where your VPS is housed. Your VPS is essentially a smaller, virtual server within this host.
  • IP Address: This is a numerical label given to every device connected to a computer network. For your VPS, this address acts as its identity and location on the internet.
  • SSH (Secure Shell): A cryptographic network protocol used to securely access network services over an unsecured network. With your VPS, you'll use SSH to remotely connect and manage it.
  • Connection: The process of remotely accessing and managing your VPS. Typically, you'd use an SSH client to establish a secure connection to your VPS using its IP address and a set of credentials.

Renting a VPS from OVH

Note: If you already have a VPS, or if you don't want to use the OVH one, you can proceed to the next step.

OVH is one of the world's largest and most reputable cloud service providers. Here's a step-by-step guide on renting a VPS from OVH:

  • Visit OVH's VPS Page: Navigate to OVH's VPS page.
  • Choose Your Package: OVH offers various VPS packages catering to different needs. Review the specifications, and select the one that aligns with your requirements. In my case, I've chosen the Starter one.

OVH VPS

  • Select Additional Features: OVH provides additional features like backup options, monitoring, and more. Depending on your needs, you can opt for these features. You'll also have to choose an Operating System (OS) and a Datacenter. I recommend choosing the latest version of Ubuntu and the Datacenter nearest to your location.

OVH VPS

  • Create an Account: If you don't have an OVH account already, you'll need to create one. This account will be crucial for managing your VPS later on.
  • Make a Payment: Once you've made your choices, proceed to checkout and make the payment.
  • Wait for Activation: Once the payment is confirmed, OVH will send you an email with details about your VPS, including its IP address, login credentials, and further instructions. This process might take some time, so a bit of patience is needed.
  • Connect to Your VPS: Using the details provided in the email, you can now SSH into your VPS. This process will be covered more thoroughly in the next section.

Step 3. Establishing a Connection to Your VPS using Putty

Once your VPS is set up and running, the very next step is to establish a connection to it. This is essential because it's how you'll be managing and operating your server remotely. In this section, we'll walk through the process using Putty, a popular SSH client for Windows. However, remember that using Putty is just a preference; if you're comfortable with other tools or if you're on a different OS with built-in terminal support, you can use those just as easily.

Why Putty?

Putty is a free and open-source terminal emulator that provides an interface for Windows users to connect to remote servers via SSH. It's user-friendly and widely adopted, making it a go-to choice for many who are new to VPS management. But as mentioned earlier, there are plenty of alternatives, and the built-in terminals on macOS and Linux will serve the purpose just as well.

Downloading and Installing Putty

  • Visit the official Putty download page: Navigate to the Putty download page and download the latest version suitable for your Windows version (32-bit or 64-bit).
  • Install Putty: Once downloaded, run the installer and follow the on-screen instructions. Installation is straightforward and should take just a couple of minutes.

Connecting to Your VPS

  • Launch Putty: After installation, open Putty from your start menu or desktop shortcut.
  • Enter your VPS details: In the Putty interface, you'll find a field labeled “Host Name (or IP address).” Here, input your VPS's IP address provided by OVH upon activation. Ensure the port is set to 22 (the default SSH port).

Putty

  • Click “Open”: After entering your VPS details, click on the “Open” button at the bottom. If this is your first time connecting to your VPS, you might get a security alert. This is normal; click “Yes” to continue.
  • Login: Once the terminal window pops up, you'll be prompted for a username and password. The username and the password would have been provided to you by OVH when you set up the VPS.

Putty

  • Test your connection: After successfully logging in, you're now directly connected to your VPS! Type ls -larth and hit enter. This command lists files in your current directory, showcasing that you're indeed operating on your remote server.

Putty

Establishing a connection to your VPS is a foundational step in server management. Whether you use Putty, another SSH client, or a built-in terminal, the principle remains the same. With this connection established, you're all set to configure, manage, and deploy applications to your VPS!

Step 4. Installing Node, NPM, and Git on Your VPS

Now that you've successfully connected to your VPS, the next step is to set up the tools that will allow you to deploy and run your projects. The trifecta for many developers is Node.js (which comes with NPM, its package manager) and Git for version control. In this section, we'll walk you through the installation process for each of these tools.

Update Your VPS

Before we dive into the installations, it's always a good idea to start with an updated server. Run the following commands to update the package lists and upgrade any outdated software:

bash
1sudo apt update
2sudo apt upgrade -y

Installing Node.js and NPM

bash
1sudo apt install nodejs npm

Check the Installation: Confirm that Node.js and NPM were successfully installed:

bash
1node -v
2npm -v

These commands should print the versions of Node.js and NPM, respectively.

Installing Git

bash
1sudo apt install git

Verify the Installation: To make sure Git has been installed, you can check its version:

bash
1git --version

This should print the version number, confirming a successful installation.

With Node.js, NPM, and Git installed, your VPS is now geared up and ready for development! These tools are fundamental for many modern web projects. Whether you're looking to deploy a Node.js application or manage your projects with Git, you're all set.

Remember to frequently check for updates for these tools, as the development world moves quickly, and it's essential to keep your tools up to date to benefit from bug fixes, new features, and performance improvements.

Step 5. Generating an SSH Key on your VPS and Linking it to Github

SSH keys are a way to identify trusted computers, without involving passwords. By setting up an SSH key, you can create a secure connection between your VPS and GitHub. This allows you to pull and push code without having to enter a password every time. In this section, you'll be generating an SSH key on your VPS and linking it to your GitHub account.

Generating an SSH Key on Your VPS

Create the Key: On your VPS, use the ssh-keygen command to start the key generation process:

bash
1ssh-keygen

Save the Key: You'll be prompted to select a location to save the key. The default location (~/.ssh/id_rsa) is usually fine, so you can simply press Enter.

Set a Passphrase (Optional): For added security, you can set a passphrase for your key. If you choose not to use one, simply press Enter.

Copying the Public SSH Key

Once the key is generated, you'll need to copy the public key to add it to GitHub. To display and copy your public key, use the following command:

bash
1cat ~/.ssh/id_rsa.pub

This will display the public key in the terminal. Highlight and copy the entire key.

Verifying the authorized_keys File

The authorized_keys file is where the server keeps a list of public keys that are allowed to authenticate. To ensure your newly created key is properly stored:

  • Navigate to the .ssh directory:
bash
1cd ~/.ssh
  • Check the content of the authorized_keys file:
bash
1cat authorized_keys

You should see your public key listed. If not, you can append it:

bash
1cat id_rsa.pub >> authorized_keys

Adding the SSH Key to Your GitHub Account

  • Go to GitHub: Log into your GitHub account.
  • Access SSH settings: Navigate to the top right corner, click on your profile picture, then click Settings. From the sidebar, choose SSH and GPG keys.
  • Add a New SSH Key: Click on the New SSH key button.
  • Paste & Save: In the “Title” field, give your key a descriptive name, like “My VPS.” In the “Key” field, paste the copied public key. Finally, click Add SSH key to save.

Testing the Connection

To ensure that everything is set up correctly, you can test the SSH connection to GitHub from your VPS:

bash
1ssh -T git@github.com

You should see a message like:

bash
1Hi [YourUsername]! You've successfully authenticated, but GitHub does not provide shell access.

This indicates that your VPS can securely communicate with GitHub using SSH.

With your SSH key now generated and linked to GitHub, you've established a secure bridge between your VPS and your repository. This will significantly streamline your workflow, eliminating the need for repetitive password inputs during Git operations.

Step 6. Cloning Your Github Repository to the VPS

After setting up an SSH connection between your VPS and GitHub, the next logical step is to clone your repository onto the server. Doing so allows your VPS to have access to the codebase, scripts, and other essential components necessary for running your applications or tasks.

Navigate to a Suitable Directory

Before cloning, decide where you want your repository to live on the VPS. For the sake of this tutorial, we'll use the home directory.

To navigate to your home directory:

bash
1cd ~

Clone the Repository

Now, let's proceed with cloning. Use the following command, replacing <username> with your GitHub username and <repository-name> with the name of your repository:

bash
1git clone git@github.com:<username>/<repository-name>.git

This will create a new directory in your current location with the name of the repository and pull down all the code from GitHub.

Note: Ensure you're using the SSH URL (which starts with _git@github.com:_) rather than the HTTPS URL.

Navigate to Your Repository's Directory

After cloning, you'll have a new directory named after your GitHub repository. Navigate into this directory:

bash
1cd <repository-name>

Verify the Cloning Process

To ensure everything was cloned correctly:

  • List the files in the directory:
bash
1ls -la

You should see familiar files and directories from your GitHub repository.

  • Also, check the current Git status:
bash
1git status

This should indicate that your branch is up-to-date with the origin.

Your GitHub repository is now successfully cloned onto your VPS! With direct access to your code on the server, you can easily execute, modify, or deploy applications as needed. The SSH link established in the previous steps ensures a secure and efficient communication between your server and your GitHub repository.

Step 7. Implementing Continuous Deployment with Github Actions

Continuous Deployment is a software development practice where code changes are automatically deployed to a production environment. By combining the power of GitHub Actions and a simple shell script on our VPS, we can automate the deployment process, ensuring that our server always runs the latest code from the main branch.

Create a Deployment Script on the VPS

Before setting up GitHub Actions, we need a deployment script on our VPS that GitHub Actions can trigger. This script will fetch the latest changes from the repository and apply them to the server.

  • SSH into your VPS and navigate to your repository's directory:
bash
1cd <repository-name>
  • Create a new shell script called deploy.sh:
bash
1touch deploy.sh
  • Edit the script using a text editor of your choice, e.g., nano:
bash
1nano deploy.sh
  • Add the following lines to deploy.sh:
bash
1#!/bin/sh
2
3# Navigate to the directory of your repository
4cd <path-to-your-repository>
5
6# Fetch the latest changes
7git fetch
8
9# Pull the latest code
10git pull origin main
  • Save and exit the text editor, then make the script executable:
bash
1chmod +x deploy.sh

Set up GitHub Actions for Continuous Deployment

  • In your local project, create a .github/workflows directory:
bash
1mkdir -p .github/workflows
  • Navigate to the workflows directory:
bash
1cd .github/workflows
  • Create a new file deploy.yml:
bash
1touch deploy.yml
  • Edit deploy.yml:
yaml
1name: Deploy to VPS
2
3on:
4 push:
5 branches:
6 - main
7
8jobs:
9 deploy:
10 runs-on: ubuntu-latest
11 steps:
12 - name: Deploying to VPS
13 uses: appleboy/ssh-action@master
14 with:
15 host: ${{ secrets.VPS_HOST }}
16 username: ${{ secrets.VPS_USERNAME }}
17 key: ${{ secrets.VPS_SSH_KEY }}
18 script: /path-to-your-repo-on-vps/deploy.sh

Note: Ensure you've the right path for the script attribute.

  • Save and exit. Now, commit and push these changes to GitHub:
bash
1git add .
2git commit -m "Added GitHub Actions for continuous deployment"
3git push origin main

Add Secrets to Your GitHub Repository

For security reasons, you shouldn't hardcode sensitive information like your VPS address or SSH keys in your code. GitHub provides a feature called “Secrets” to handle this.

  1. Go to your GitHub repository.
  2. Click on the “Settings” tab.
  3. In the left sidebar, click on “Secrets”.
  4. Click on “New repository secret”.
  5. Add the following secrets:
  • VPS_HOST: Your VPS's IP address.
  • VPS_USERNAME: Your VPS's username (e.g., ubuntu).
  • VPS_SSH_KEY: Your private SSH key which should be connected to the VPS. (~/.ssh/id_rsa on your VPS).

With these secrets in place, GitHub Actions will be able to SSH into your VPS and execute the deploy.sh script.

Test the Continuous Deployment

  1. Make a minor change to your codebase, e.g., update the README.
  2. Commit and push the changes to main.
  3. Head to the “Actions” tab in your GitHub repository. You should observe the action being triggered.
  4. After the action finishes running, SSH into your VPS and verify the changes are present.

By following these steps, you've created a pipeline that automates the deployment of your code to your VPS each time you push to the main branch. It's a huge efficiency boost, ensuring your server is always running the most recent version of your code.

Step 8. Testing Your Cron on the VPS and Setting Up a Crontab

Deploying code is just half the journey. Ensuring that it runs correctly in the deployment environment is equally crucial. Once we've got our code on the VPS, we need to run our Node cron job and see if it behaves as expected. After successful tests, we'll use crontab to automate the job on a schedule.

Running Your Cron Job Manually on the VPS

If you haven't already connected, SSH into your VPS. Then, navigate to your project's directory:

bash
1cd path-to-your-repo

Before you run your Node.js app, you'll want to make sure you have all the required packages:

bash
1npm install

Based on the setup we discussed in step 1, you can run your cron job using the following:

bash
1npm run start hello

After executing, you should see the Hello World output, signaling that your cron job script works correctly on the VPS!

Automating the Cron Job with Crontab

If everything is working as expected, it's time to automate the cron job to run at specific intervals using crontab.

  • Open crontab for editing:
bash
1crontab -e

If prompted to select an editor, nano is usually the simplest for beginners, but you can choose according to your preference.

  • Add your cron job:

Scroll to the bottom and add a line for your cron job. The line's format begins with five asterisks (representing minutes, hours, days, months, and days of the week), followed by the command to run.

For example, to run your script every day at 3 am, you'd add:

bash
10 3 * * * cd /path-to-your-repo && npm run start hello >> /path-to-your-repo/logs.txt 2>&1

The last part of the instruction is used to save logs in a text file. We can refer to it later to ensure everything is working as expected.

Adjust the time settings according to your desired schedule. You can use tools like crontab.guru to help generate the correct timing syntax.

  • Save and exit:

If using nano, press CTRL + X to save, press Y to confirm changes, and then Enter to exit.

  • Verify your cron job:

You can check your current list of cron jobs with:

bash
1crontab -l

This will display all scheduled tasks, including the one you just added. Additionally, after the next execution, you can check the logs.txt to ensure that everything is running properly.

Step 9: Installing and Configuring Crontab-ui

Crontab-ui is a handy web interface for managing cron jobs without diving into the server's command line each time. With Crontab-ui, you get a visual representation of all your cron jobs, the ability to edit, delete, or add new ones. For this step, we'll be installing Crontab-ui on our VPS and configuring it so that you can access and manage your cron jobs from your local machine.

Installation

Firstly, let's install Crontab-ui. Ensure you have Node.js and npm installed (as covered in Step 4).

bash
1npm install -g crontab-ui

Running Crontab-ui

Start Crontab-ui using the following command:

bash
1crontab-ui

This will start Crontab-ui and, by default, it will be running at http://127.0.0.1:8000. However, this address is local to the VPS, so we need to make it externally accessible.

Accessing Crontab-ui Externally

To access Crontab-ui from your local machine, we must bind it to the public IP address of the VPS. Stop the running Crontab-ui by pressing CTRL+C. Then restart it by binding it to the VPS's public IP:

bash
1HOST=0.0.0.0 crontab-ui

Warning: This makes Crontab-ui accessible to anyone who knows the IP address of your VPS. Later, consider setting up a reverse proxy with Nginx or another web server to secure access, or implement authentication.

Accessing from a Local Machine

Once it's running and bound to the public IP, open a web browser on your local machine and enter the VPS's IP address followed by the port number: http://[Your_VPS_IP]:8000.

Now you should be greeted with the Crontab-ui interface, and you can manage your cron jobs visually!

Crontab-ui

Crontab-ui offers a more user-friendly approach to managing cron jobs, especially for those who prefer a GUI over the command line. But ensure you take necessary precautions to secure access, especially if your VPS holds sensitive data or operations.

Conclusion

From setting up the foundational elements with GitHub and a sample cron, to the nuanced steps of accessing and managing cron jobs visually with Crontab-ui, I hope this guide serves as a comprehensive roadmap for your projects.

If you found this tutorial helpful or think there's room for improvement, I warmly invite your feedback.