Skip to content

Instantly share code, notes, and snippets.

@fdnklg
Last active August 19, 2019 09:50
Show Gist options
  • Save fdnklg/2c784e82735a3122acf1c07009795fd4 to your computer and use it in GitHub Desktop.
Save fdnklg/2c784e82735a3122acf1c07009795fd4 to your computer and use it in GitHub Desktop.

If you’re interested to execute a script more often at a specific time you might be interested to setup a cron job. Now I want to briefly summarise the most basic how to about setting up a cronjob for a node script on a linux web server.

What is cron?

Cron is a service that executes scripts at a given time. Each cron job is following this syntax:

* * * * * auszuführender Befehl
┬ ┬ ┬ ┬ ┬
│ │ │ │ │
│ │ │ │ └──── weekday (0-7, Sonntag ist 0 oder 7)
│ │ │ └────── month (1-12)
│ │ └──────── day (1-31)
│ └────────── hour (0-23)
└──────────── minute (0-59)

An asterisk (*) defines that the script is executed at every digit. In the example above the script would be executed every minute.

Check file permissions

Before you set up a cron job via the command line please make sure you have the correct file permissions to perform the actions like reading, writing, executing files.

In my example I want to write files on a regular basis on to my web server. In order to do that I check the permissions inside the directory of my script with (for more information check this out: What’s behind the ‘ll -al’ command?):

# list all files and file permissions in a long list
ll -al

# should return something like this
-rwx------. 1 fdnklg fdnklg  216 Jul  7 13:00 cron-test.js
drwxrwxr-x. 2 fdnklg fdnklg    6 Jul  7 13:00 data
-rw-r--r--. 1 fdnklg fdnklg   64 Jul  7 13:01 data1.txt
-rw-r--r--. 1 fdnklg fdnklg   64 Jul  7 13:10 data10.txt
-rw-r--r--. 1 fdnklg fdnklg   64 Jul  7 13:11 data11.txt
-rw-r--r--. 1 fdnklg fdnklg   64 Jul  7 13:12 data12.txt

In my example I use the tool chmod to change the file permissions to read, write and execute. For more information about file permissions on linux servers, please follow this link: Set file permissions on a linux server. In most cases you just want to use this command:

chmod 700 name-of-your-script.js

Setup the cronjob

After setting up the correct file permissions there’s only one more thing to do. Edit the crontab table and add your cronjob. In my example I want my script to be executed every minute and write a file to another directory. First Open the edit window with:

crontab -e

# This provides you a list of all active crontabs
crontab -l

Inside the editor you just add a line with the following syntax:

# Here it’s important to tell how the script is interpreted 
# in this case we add the node before because it’s a node script.

* * * * * node path/to/your/script/script-name.js

And that’s it! After saving your changes within the nano editor the cronjob is active! It will be executed every minute on every day.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment