It’s Cron Time!
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>.shNext, create a new crontab
crontab -eThis 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.txtThis 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 -lRemove Crontabs
crontab -rExecuting 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
