I've never created, nor used a cron job before, but what I've gathered from numerous questions and answers on SO is that the process is fairly simple and involves something like the following:
- Create bash file with shell commands
- Edit crontab
I've found lots of questions and answers on SO regarding cron jobs, but not a single one of them actually explains the syntax. I've tried looking online for a reliable explanation too, but to no avail. I did find this page, however, which explains the time and date portion of crontab
statements very clearly.
Here's my understanding so far:
1. Create bash script, which can be placed anywhere.
#!/bin/bash
cd /home/user/public_html/scrapy/projects/myproject/spiders
scrapy crawl mycrawler
What is the significance of the
#!/usr/bin/bash
statement?Why is it commented out?
Is using a shell script as a proxy even necessary to run Python scripts?
2. Edit crontab via the crontab -e
command
I've seen so many different recommendations for this part, so I'm going to list a few examples from a few different answers.
PATH=/usr/bin
* 5 * * * cd project_folder/project_name/ && scrapy crawl spider_name
- Is embedding commands directly in
crontab -e
considered good practice?
*/5 * * * * /usr/local/bin/python /home/Documents/SCRAPE_PYTHON/SCRAPE.py &>> /home/Desktop/log.txt
- What is the significance of the first path,
/usr/local/bin/python
, in this context?
He states in his answer that &>> /home/Desktop/log.txt
is the file to which errors and other output will be appended.
Is that what the
&>>
does?Is that universal for every single Linux environment?
*/2 * * * * /home/user/shell_scripts/cj-scrapy.sh
How come the above code does not include two paths?
Is it a potential security vulnerability to place shell scripts in the
/home/user/scripts
directory?Is there a specific directory where shell scripts like this are commonly stored?
Example #4
The cPanel Cron Job Wizard recommends the following syntax:
/usr/local/bin/php /home/user/public_html/path/to/cron/script
Why all of the discrepancies between crontab
recommendations?
I understand the syntax of the time and date portion of crontab
, but can somebody please explain the proper syntax for the rest of it?