-5

I have a python script which takes few params as argument and I need to run tasks based on this script at a given date and time with other params. I am making an UI to add/modify/delete such tasks with all given params. How do I do it? Is there any tool available? I dont think crontabs are the best solution to this especially due to frequent need of task modification/deletion. The requirement is for linux machine.

One soln could be: Create an API to read all the tasks stored in DB to execute the python script timely and call that API after every few minutes via crontab.

But I am looking for a better solution. Suggestions are welcome.

sns
  • 221
  • 4
  • 17
  • I think, this is gonna be helpful: http://www.baeldung.com/java-timer-and-timertask https://stackoverflow.com/questions/11361332/how-to-call-a-method-on-specific-time-in-java Let me if you know something better. – sns Jul 28 '18 at 15:54
  • I think you can actually use python celery for this. – gigahex Aug 02 '18 at 05:22
  • It would help if you described your use case better. Specifically, are you wanting to use a specific technology for your UI, or is that one of the things you want help with? In a comment you said "I want to trigger them only when date-time is mentioned, not check regularly" I'm not entirely sure what you mean by that. Say someone uses your UI and enters some params, and a date/time. Do you want a single event scheduled to happen at the specified time? – Tony Aug 03 '18 at 22:47

5 Answers5

2

I am assuming that all the arguments (command line) are know beforehand, in which case you have couple of options

  1. Use a python scheduler to programmatically schedule your tasks without cron. This scheduler script can be run either as daemon or via cron job to run all the time.
  2. Use a python crontab module to modify cron jobs from python program itself

If the arguments to scripts are generated dynamically at various time schedule (or user provided), then the only is to use a GUI to get the updated arguments and run python script to modify cron jobs.

Ketan Mukadam
  • 789
  • 3
  • 7
  • crons or cron based library solutions aren't helping me. I need to run a third party python script with dynamically added/modified/deleted arguments including date-time and image url. I want to trigger them only when date-time is mentioned, not check regularly. Can you give a demo link where some python/java based cron library is able to easily add/modify/delete each task? – sns Jul 28 '18 at 15:52
  • If you want to trigger any script on specific date-time, you still need to check regularly whether the date-time has arrived if you don't rely on framework like cron. With `crontab` you can specifiy the date-time from python script itself. Lots of [crontab](https://www.programcreek.com/python/example/97685/crontab.CronTab) examples here. – Ketan Mukadam Jul 30 '18 at 13:03
2
from datetime import datetime
from threading import Timer

x=datetime.today()
y=x.replace(day=x.day+1, hour=1, minute=0, second=0, microsecond=0)
delta_t=y-x

secs=delta_t.seconds+1

def hello_world():
   print "hello world"
   #...

t = Timer(secs, hello_world)
t.start()

This will execute a function in the next day at 1 am.

gigahex
  • 367
  • 1
  • 5
  • 14
1

You could do it with timer units with systemd. What are the advantages over cron?

  1. Dependencies to other services can be defined, so that either other services must be executed first so that a service is started at all (Requires), or a service is not started if it would get into conflict with another service currently running (Conflicts).
  2. Relative times are possible: You can cause Timer Units to start a service every ten minutes after it has been executed. This eliminates overlapping service calls that at some point cause the CPU to be blocked because the interval in the cron is too low.
  3. Since Timer Units themselves are also services, they can be elegantly activated or deactivated, while cronjobs can only be deactivated by commenting them out or deleting them.
  4. Easily understandable indication of times and spaces compared to Cron.

Here come an example:

File: /etc/systemd/system/testfile.service

[Unit]
Description=Description of your app.

[Service]
User=yourusername
ExecStart=/path/to/yourscript

The Timer Unit specifies that the service unit defined above is to be started 30 minutes after booting and then ten minutes after its last activity.

File: /etc/systemd/system/testfile.timer

[Unit]
Description=Some description of your task.

[Timer]
OnBootSec=30min
OnUnitInactiveSec=10min
Persistent=true
User=testuser
Unit=testfile.service

[Install]
WantedBy=timers.target
sunwarr10r
  • 4,420
  • 8
  • 54
  • 109
0

One solution would be to have a daemon running in the background, waking up regularly to execute the due tasks.

It would sleep x minutes, then query the database for all the not-yet-done tasks which todo datetime is smaller than the current datetime. It would execute the tasks, mark the tasks as done, save the result and go back to sleep.

You can also use serverless computation, such as AWS Lambda which can be triggered by scheduled events. It seems to support the crontab notation or similar but you could also add the next event every time you finish one run.

tleb
  • 4,395
  • 3
  • 25
  • 33
  • "You can also use serverless computation, such as AWS Lambda which can be triggered by scheduled events. It seems to support the crontab notation or similar but you could also add the next event every time you finish one run." This seems interesting..any demo I can refer to? other options I am aware of...looking for something which doesn't need to keep running just to check regularly. – sns Jul 28 '18 at 15:47
  • A tutorial is available: https://docs.aws.amazon.com/lambda/latest/dg/with-scheduledevents-example.html – tleb Jul 28 '18 at 16:05
-2

I found the answer to this myself i.e, Timers Since my experience and usecase was in java, I used it by creating REST API in spring and managing in-memory cache of timers in java layer as a copy of DB. One can use Timers in any language to achieve something similar. Now I can run any console based application and pass all the required arguments inside the respective timer. Similarly I can update or delete any timer by simply calling .cancel() method on that respective timer from the hashmap.

  public static ConcurrentHashMap<String, Timer> PostCache = new ConcurrentHashMap<>();
  public String Schedulepost(Igpost igpost) throws ParseException {
        String res = "";
        TimerTask task = new TimerTask() {
            public void run() {
                System.out.println("Sample Timer basedTask performed on: " + new Date() + "\nThread's name: " + Thread.currentThread().getName());
                System.out.println(igpost.getPostdate()+" "+igpost.getPosttime());
            }
        };
        DateFormat dateFormatter = new SimpleDateFormat("yyyy-MM-dd HH:mm");
        Date date = dateFormatter.parse(igpost.getPostdate()+" "+igpost.getPosttime());
        Timer timer = new Timer(igpost.getImageurl());
        CacheHelper.PostCache.put(igpost.getImageurl(),timer);
        timer.schedule(task, date);
        return  res;
    }

Thankyou everybody for suggestions.

sns
  • 221
  • 4
  • 17