Writing Cron Jobs on a Mac when code editor set to vscode as default

prgrmr-yn
5 min readJan 31, 2023

--

Cron jobs are an essential tool for automating tasks in Unix-based systems. They allow you to schedule commands or scripts to run automatically at specified times and intervals. However, by default, cron jobs are configured to run using the vi editor, which can be difficult to use for some people. In this article, I will show you how to work with cron jobs using Visual Studio Code (VSCode) as the default editor.

Method 1: Using the EDITOR Environment Variable

You can set the EDITOR environment variable to nano, which will make it the default editor for cron jobs. Here's how you can set it in your shell profile

Step-1: Open the terminal

Step-2: Enter the command posted below, you are about to change your default editor to nano, but don’t worry after setting the cron job you can set it back to vscode using- export EDITOR=code

export EDITOR=nano

Step-3: Enter the command posted below, you are about to enter nano editor, its not like vs code so don’t panic :D

crontab -e

Step-4: Your window will look something like this, that where we are gonna write our cronjob, in this step you wont be entering any command or pressing other buttons, go to next step.

Step-5: Enter the command posted below. You will be writing your first cronjob here, now this is not an article on how to write cronjob so i wont be explaining that but to get more information on the syntax, i suggest you go on youtube and search “Cronjob syntax explained” and you will find very good examples and in the end, you can write your own cronjob.

05 16 * * * /bin/bash -l -c ‘ echo “This is some text” >> /Users/yatin/bash/filename.txt ’

Step-6: Now once you have pasted the code in nano, i wanna explain what you copied , the two number you entered in the start followed by asterisk is the time format, you are trying to enter the time you wanna run now here i have it for “4:05pm everyday” but you can enter according to what you want by going to https://crontab.guru , 16 here stands for the hour and 05 is minute so to do 1605 which is 4:05pm you will enter following command, or you can leave it as it is.

This is the basic syntax for your reference

* * * * * command_to_executed
| | | | |
| | | | + — — — day of week (0–6) (Sunday=0)
| | | + — — — — month (1–12)
| | + — — — — — day of month (1–31)
| + — — — — — — hour (0–23)
+ — — — — — — — min (0–59)

Next you you telling bash you are running a script, and pushing ‘This is some text’ into your file, now to get the path of your directory you are currently in, you can open another terminal while you are in nano by command+t and type pwd | pbcopy, this will check your current directory and copy it on clipboard, to go back to nano editor, go back to nano editor by Command+Shift+leftArrowKey and replace /Users/yatin/bash/

with what you have copied before, you have to make sure its all in one line, and with filename.txt you don’t have to create it, it will generate itself , now its time to exit nano editor by entering three set of pairs one by one, Ctrl+o,Enter,Ctrl+x, Your screen should look something like this.

Step-7: To check you active cronjobs, you can type ‘crontab -l’ to list all the cronjobs

Step-8: You are done, Woohoo, Time to change default editor to vscode by entering export EDITOR=code in your terminal, Also you can enter ruby myscript.rb or node myscript.js instead of adding text in a file by just entering

05 16 * * * /bin/bash -l -c ‘node myscript.js’

05 16 * * * /bin/bash -l -c ‘ruby myscript.rb’

If for some reason it doesn’t execute you need to check your settings, here we are granting full disk access to cron

Notes: Theres few things to know about cronjob, your computer needs to be running at the time of the set cronjob for your computer to be able to execute that command .

  • Click on the (+) icon to add an item to the list:
  • Press command+shift+G then type /usr/sbin/cron and Enter, you can find cron in finder:
  • Click open and now you are all set,

I hope this helps.

--

--