The simplest way to run Node.js code on a schedule
Tags: javascript, node, cron, serverless
Published: 2020-04-03 17:58:17

I remember the first time I discovered `cron`. I’ve been obsessed with automation my whole life, and as a fledgling developer `cron` became my workhorse. 

At the time, I worked at an IT help desk. I used `cron` for email reminders, automated reports, backups: anything I could schedule, I did.

![](https://lh3.googleusercontent.com/c1J4hwTAE45ELFjJEXXvmYs9jFR_fVho6Jj3qPq7cop26H7L_rggwiNzdeuzjtigsZrqtwHE_UlyRvYAqhR5yGHuW9UF4od0mRO03E7gmKns76ygy_vesirU1kzUt3JoDZMppPaP)

`cron` is simple: you just tell it what you want to run, when you want to run it:

    0 0 * * *  node script.js  # run script.js once a day


But it comes with tradeoffs: primarily, you have to operate a server to run it, which costs money and time. If you don’t already have a server where you can run jobs, setting one up just for `cron` is far from ideal. 

In the era of the cloud, it’s also outdated. Cron jobs are simple only if you cut your teeth on Linux and can effectively administer a server. This is an inaccessible option for many modern devs who operate far up the stack.

So it’s no surprise that we have other choices for scheduling code in 2020:

- [AWS Lambda + CloudWatch Events](https://docs.aws.amazon.com/lambda/latest/dg/services-cloudwatchevents-expressions.html)
- [Google Cloud Scheduler](https://cloud.google.com/scheduler)
- [Airflow](https://airflow.apache.org/)
- [Kubernetes CronJobs](https://kubernetes.io/docs/tasks/job/automated-tasks-with-cron-jobs/)
- [ECS Tasks](https://docs.aws.amazon.com/AmazonECS/latest/developerguide/scheduling_tasks.html)
- etc.

These options work well in their intended context: Lambda is great when you’re glueing together AWS resources. Kubernetes CronJobs or ECS Tasks are ideal when your code runs in a container. Airflow is great when you have complex dependencies between jobs, for example in data pipelines. 

But when you just want to run a script on a schedule, they’re overkill.

### Enter Pipedream

[Pipedream](http://pipedream.com/) is an integration platform for developers. You run serverless [workflows](https://docs.pipedream.com/workflows/) - any Node.js code - triggered by HTTP requests, timers, emails, and more. You can also use [pre-built actions](https://docs.pipedream.com/workflows/steps/actions/) to connect to hundreds of APIs and apps - actions are just Node functions that perform common operations against these APIs.

You can sign up and run any Node code on a schedule in **less than one minute**. Watch this video or follow the step-by-step instructions below:

{% youtube PwjVR0dj-Hk %}

First, install the Pipedream CLI:

```bash
curl https://cli.pipedream.com/install | sh
```

`cd` into a directory with a Node script you’d like to run, or just create a simple one-line script:

```bash
echo 'console.log("Hello, world")' > cronjob.js
```

then deploy that script to Pipedream, running it every 15 seconds:

```bash
pd deploy --run cronjob.js --timer --frequency 15s
```

This step will prompt you to sign up for Pipedream if you haven’t already. The `pd` CLI will deploy your code to Pipedream, and print logs as the script produces them. You can press `Ctrl-C` to quit the real-time stream, and listen for new logs later by running:

```bash
pd logs cronjob-js
```

You can delete this job and all its logs by running:

```bash
pd delete cronjob-js
```

You can list all running jobs with `pd list`, update their code or schedule with `pd update`, and more. [See the docs to learn more](https://github.com/PipedreamHQ/pipedream/tree/master/interfaces/timer).

### A practical example: send an HTTP request on a schedule

If you already host code at some URL, and just want to trigger a job to run via HTTP request, you can use Pipedream to send that request on a schedule. In this example, we’ll hit the [Star Wars API](https://swapi.co/) once a day.

First, open your editor and create a file called `http.js` with the following contents:

```javascript
const axios = require("axios");
    
const resp = await axios({
  method: "GET",
  url: `https://swapi.co/api/films/`  // replace with your URL
});
   
console.log(resp.data);
```

Then run

```bash
pd deploy --run cronjob.js --timer --cron “0 0 * * *”
```

This will make a GET request to SWAPI, once a day at midnight, but you can modify this script to send any HTTP request, at any schedule. See our guide on [making HTTP requests in Node](https://docs.pipedream.com/workflows/steps/code/nodejs/http-requests/) for more examples.

See [the README on cron jobs](https://github.com/PipedreamHQ/pipedream/tree/master/interfaces/timer) or the [Pipedream docs](https://docs.pipedream.com/) to learn more about the platform at large.

### We ❤️ Feedback

We're eager to hear your feedback. We're shipping new features every day - check out [our roadmap](https://github.com/PipedreamHQ/roadmap) and [reach out anytime via email or Slack](https://docs.pipedream.com/support/) to let us know what we can improve.