#quickstart
## Cron, in a nutshell
Crontab stands for Cron Table. This file has a table like structure with each row of the table representing a job to be run at the time specified. Cron is an amazing tool for automating workflows in small and medium businesses.
To get started, check to see if you have items in your crontab:
```sh
crontab -l
```
If you do not have items in your crontab, and wish to edit it, you can open it with the following command:
```sh
crontab -e
```
This should open with your default editor. If you want to open it in Nano (suggested), however, use this command:
```sh
EDITOR=nano crontab -e
```
Once opened, your crontab may look something like this (don't worry if its blank instead)
```
# Edit this file to introduce tasks to be run by cron.
#
# Each task to run has to be defined through a single line
# indicating with different fields when the task will be run
# and what command to run for the task
#
# To define the time you can provide concrete values for
# minute (m), hour (h), day of month (dom), month (mon),
# and day of week (dow) or use '*' in these fields (for 'any').
#
# Notice that tasks will be started based on the cron's system
# daemon's notion of time and timezones.
#
# Output of the crontab jobs (including errors) is sent through
# email to the user the crontab file belongs to (unless redirected).
#
# For example, you can run a backup of all your user accounts
# at 5 a.m every week with:
# 0 5 * * 1 tar -zcf /var/backups/home.tgz /home/
#
# For more information see the manual pages of crontab(5) and cron(8)
#
# m h dom mon dow command
```
If you opened your crontab in Nano, the commands to edit your crontab will be at the bottom of the screen. Here are the ones that will matter most to you:
- Ctrl + O = save your work ('Write out')
- Ctrl + X = exit the crontab
For now, add a basic 'hello world' example to your crontab. At the bottom of the crontab, add this line:
```
# example
5 10 * * * cd Desktop/ && echo 'hi' > hi.txt
```
Save your work (Ctrl + O) and exit (Ctrl +X)
This line means that on the 5th minute of 10am, every day of the week, a process will navigate to your desktop (note: you may need to replace the cd statement to your own setup) and will then write the phrase 'hi' to a TXT file called 'hi.txt'
Next, lets replace the 'echo hi > hi.txt' statement with a script that performs the same action
In your desktop, open your text editor and add the following line, then save it as 'hi.sh':
```sh
echo 'hi from scrip' > hi_from_script.txt
```
Now open your crontab again and add the following line:
```
10 10 * * * cd Desktop/ && bash hi.sh
```
This line means that on the 10th minute of 10am, every day of the week, a process will navigate to your desktop and will execute a bash script called 'hi.sh'. This bash script simply writes the phrase 'hi from script' to a TXT file.
Lastly, on your dekstop, open your text editor and copy and paste these lines in, then save the file as hi.py:
```python
with open('hi_from_python.txt', 'w') as file:
file.write('hi from python')
```
Now open your crontab again and add the following line:
```
36 10 * * * cd Desktop/ && python3 hi.py
```
Note: at any point you can check to make sure your crontab entries saved by using the command:
```sh
crontab -l
```
This line means that on the 36th minute of 10am, every day of the week, a process will navigate to your desktop and a execute a python script called 'hi.py'. This script simply writes the phrase 'hi from python' to a TXT file.
## A few gotchas
By default, cron does not have access to the terminal. This means that a line of:
```
45 10 * * * echo 'hi'
```
Will not print 'hi' on your terminal. Similary, the commands open, code, cat, etc. etc. will not interact with your terminal. All outputs must be directed to something (e.g. a .txt file)
## A few helpful resources
The cron syntax is fairly simple to initially learn, yet can be super complex to get good at. The following website can serve as a helpful resource for formatting cron entries:
https://crontab.guru/examples.html