Start A Cronjob (with Bash and Python)- 3 mins
What’s the Scenario
For some reason, I need to send a post request to a particular url to get a ticket string, and to update this ticket string in a configuration file periodically so that I can work on this project.
The solution is pretty self-explanatory. For starters, I need to send request and save/update the string in the configuration file, which is implemented by python. And most importantly, I need to start a cronjob to run the python script every 30 mintues.
I’d like to address cronjob first, because you can alter to node to do the http and file IO stuff, and the python code is just something came out of quick API search.
So for now, I assume the python and bash scripts are ready, and the only thing I need to do is to create a cronjob to run the bash script.
env EDITOR=nano crontab -e to open the nano editor, write something like this:
*/30 * * * *means run this job every 30 minutes since the first place is a minute descriptor, however, if you write something like
10 * * * *that means run this job every hour at the 10th minute;
shmeans run this script as
.shfile, followed by the file path of the sh script;
the rest means to redirect the cronjob log to another place
ctrl+o and press enter to save the file,
ctrl+x to close nano.
You can use
tail -f /Users/myname/myproject/ticket-cronjob/cronjob-ticket.log to monitor real time content of the log file.
Besides, you can also type
crontab -l to list all cronjobs, and
crontab -e to read and edit cronjob descriptions.
Regardless of the fact that cronjob will stop when the laptop goes to sleep, there will be a time when you want to stop the cronjob, use
crontab -r then the cronjob will be deleted, but it is not as good as pause and resume, which I’ll look into later.
Python and Bash
The python script for requesting new ticket and updating the file is not difficult, but I am not that familiar with python yet, so I won’t paste the code here except noting that it uses packages such as
As a matter of fact, in this case, it is not necessary to use bash to execute the python script since you can directly run python script in cronjob, but I’d like to give it a try anyway.
The first line is a shebang, it tells the shell what program to interpret the script with when executed.
To make the script more flexible, I change the absolute path with
dirname so it is executable on both local folder and cronjob.
Lastly, you need to execute a command like this
chmod u+x myscript.sh to make this script executable.