M
M
mt_max2019-12-20 06:36:10
Python
mt_max, 2019-12-20 06:36:10

How to disable/enable cron tasks using python?

Good afternoon, the situation is as follows: there is a web application on flask for adding phone numbers to the database. There are scripts in cron that send SMS to numbers from the database.

0 9 * * 1 /home/xxx/yyy/mes.py -monday >> /home/xxx/dispatch.log
0 9 * * 3 /home/xxx/yyy/mes.py >> /home/xxx/dispatch.log
0 9 * * 5 /home/xxx/yyy/mes.py >> /home/xxx/dispatch.log

It is necessary to implement a button in the web application that will "turn on/off" the distribution, for example, when the number of SMS starts to exceed the allowable limits. What is the best way to organize this task? There is an idea to make another table in the database and write boolean values ​​there, take the last line in scripts to see what is true / false and, depending on this, do not send out, but it confuses that there will be a bunch of unnecessary rows in the table, but in fact the desired state will be only in the last one.

Answer the question

In order to leave comments, you need to log in

3 answer(s)
S
Sergey Tikhonov, 2019-12-20
@tumbler

Write in the same line if you are confused by a bunch of unnecessary ones.

K
Kirill Gorelov, 2019-12-20
@Kirill-Gorelov

I see two solutions here.
1. Python has the ability to control the console from scripts. Separately, you write a function that will execute a request to the console. But this is an option, so-so.
2. In modern development practices, this is usually done. It's not the cron itself that starts the mailing list. And cron runs a function that will check tasks for mailing. If there are tasks, then the mailing itself is launched. What does it mean - tasks for mailing. Or you have an intermediate table where you store a list of what needs to be sent (the option is also so-so, as for me). Or at the address to store value is sent or not
Or the third variant as you offered, separately to store value true/false.

P
pfg21, 2019-12-20
@pfg21

classically "truunixway" this is done via sed awk and other regexppin. it looks for a line in the file and puts a # in front of it and vice versa.
option 2: keep the contents of the crontab in the database and, with each change, completely generate /etc/crontab with the necessary included tasks
option 3: put the rules into separate files in the etc/cron.d/ directory and move them to another directory to delete them and back.
minus: support for cron.d is not available everywhere.
option 4: if there is a systemd, then use systemd.timer, where the desired functions are already built into the system.

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question