It’s Cron Time!

Cain Watson
Jul 24, 2017 · 1 min read

Cron is a software utility available on Unix-like machines that allows you to automate scripts.

To get started we first need to create file and put the commands you wish to run inside of it.

Then add the shebang to the top of your script. In short, the shebang #!/bin/bash will execute your code with bash.

#!/bin/bashecho Hello Cron!

After this, you need to make your script into an executable by running this command:

chmod +x <your_file>.sh

Next, create a new crontab

crontab -e

This will open up a text editor like vim and this is where you will schedule your tasks. Each line will represent a single task.

Cron works off a timing system that goes:

MINUTE
HOUR
DAY_OF_MONTH
MONTH
DAY_OF_WEEK

Heres an example. (The star represents any number in cron)

* * * * * ~/jobs/my_shell_script.sh >> ~/jobs/my_shell_script_output.txt

This will run the file my_shell_script.sh every minute and output its logs into my_shell_script_output.txt

You can use this tool to determine the time you want.

More Cron Commands

List All Of Your Crontabs

crontab -l

Remove Crontabs

crontab -r

Executing Other Languages

You must use the proper shebang for the language you are using. To do this, all you have to do is find your path to it. (Typically located in /usr/local/bin/)

Node: #!/usr/local/bin/node
Kotlin: #!/usr/local/bin/kotlin
etc…

Helpful Links

How to Run Node.js Scripts from a Cron Job
Crontab Guru
Make A Shell Script Executable

Welcome to a place where words matter. On Medium, smart voices and original ideas take center stage - with no ads in sight. Watch
Follow all the topics you care about, and we’ll deliver the best stories for you to your homepage and inbox. Explore
Get unlimited access to the best stories on Medium — and support writers while you’re at it. Just $5/month. Upgrade