I have this bash script on a server that runs every hour through cron. I was completely happy, but now the user wants to adjust the frequency through the web interface.
I am not comfortable managing the cron configuration programmatically, but I'm not sure if other options are better.
As I see it, I can:
- Schedule the script to run once a minute and see if it should really run "now"
- Deviate altogether and use the deamon, which is its own planner. This probably means rewriting the script in python
- ... or suck it and manipulate the cron configuration from the web interface (written in python BTW)
What should I do?
EDIT: To clarify, the main reason I fear manipulating cron is that it is basically manipulating text without checking, and if I ruin it, none of my other cron jobs will run.
Here is what I did:
Taking stefanw's advice, I added the following line at the top of my bash script:
if [ ! `cat /home/username/settings/run.freq` = $1 ]; then
exit 0
fi
I installed the following cron jobs:
0 */2 * * * /home/username/scripts/publish.sh 2_hours
@hourly /home/username/scripts/publish.sh 1_hour
*/30 * * * * /home/username/scripts/publish.sh 30_minutes
*/10 * * * * /home/username/scripts/publish.sh 10_minutes
From the web interface, I allow the user to choose between these four parameters and based on what the user has selected, I write the line 2_hours/1_hour/30_minutes/10_minutesto the file in /home/username/settings/run.freq.
I don't like it, but it seems like a better alternative.
source
share