User permission to configure cron

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.

+3
source share
5 answers

, , , 5 , ... cron. cron.

+10
+3

Webmin? , , , cron , .

0

, -, , script, cron. script . cron script, /. "rm -f" , . , , . - ...

script :

[...]

if [ -e "${tag_archive_log_files}" ]; then
    archive_log_files ${params}
    rm -f ${tag_archive_files}
fi

if [ -e "${tag_purge_log_files}" ]; then
    purge_log_files ${params}
    rm -f ${tag_purge_log_files}
fi

[...]
0

, cron . python-crontab, easy_install. :

Example Use:

from crontab import CronTab

tab = CronTab()
cron = tab.new(command='/usr/bin/echo')

cron.minute().during(5,50).every(5)
cron.hour().every(4)

cron2 = tab.new(command='/foo/bar',comment='SomeID')
cron2.every_reboot()

list = tab.find('bar')
cron3 = list[0]
cron3.clear()
cron3.minute().every(1)

print unicode(tab.render())

for cron4 in tab.find('echo'):
    print cron4

for cron5 in tab:
    print cron5

tab.remove_all('echo')

t.write()

(I continued searching on Google for "cron" and could not find anything. The keyword that I was missing was "crontab").

Now I decide whether I will use this or just replace cron completely on the python based scheduler.

0
source

Source: https://habr.com/ru/post/1712752/


All Articles