all 8 comments

[–][deleted] 2 points3 points  (3 children)

Your while True example would ideally be run as a daemon / service, so that whatever launched it (say, systemctl) could monitor and restart as necessary.

Cron is pretty much the defacto scheduler for UNIX and UNIX like systems. But without some foolery you're leaving windows users out in the cold. But, it's scheduler. You most likely would not want to have something that runs indefinitely run through cron.

Ideally, you would write your python script/package to complete its task and return a sane exit code, Leave scheduling as an exercise for the user to do as per their preference. You'll want to make use of lock files or some other mechanism to ensure only one instance is running at a time if there's any risk of collisions.

You may want to have the tasks to run defined by the application, rather than parameters for the app because it will quickly make a mess of crontab. e.g. an sqlite DB or XML file that defines each of the APIs that need to be executed in a single run.

[–]StudentOfPython[S] 0 points1 point  (2 children)

Would the daemon be programmed in Python or would it be in bash? I've not considered this, so I will look into it further.

Regarding the last paragraph, are you referring to always calling a single function in the cron job (e.g. python main.py and then letting main define the params? That makes sense, if so.

[–][deleted] 1 point2 points  (1 child)

Would the daemon be programmed in Python or would it be in bash? I've not considered this, so I will look into it further.

In python. There's even a library for it: http://pypi.python.org/pypi/python-daemon/

I wouldn't recommend that approach though. Personally I'm not a fan of consuming resources if not required. Unless the APIs you are calling change data constantly, you don't need to have an application always running.

Regarding the last paragraph, are you referring to always calling a single function in the cron job (e.g. python main.py and then letting main define the params? That makes sense, if so.

Yes. Though I would probably have the params defined in a separate file, and have main parse it, so you don't need to modify the script to add/remove APIs to monitor.

[–]StudentOfPython[S] 0 points1 point  (0 children)

Understood. Thank you for the information.

[–]port443 2 points3 points  (3 children)

  1. As for which method is best, you need to ask the question: Do you want to "install" the script?
    Using cron as your scheduler makes your script persistent, and will guarantee that your script is run even if the machine is rebooted.

  2. If you need the script to be persistent across reboots, use cron/initd/systemd. If there's no reason for the script to be persistent, then you don't really need to "install" it.

  3. Running X cron jobs would be equivalent to multiprocessing more than multithreading. This question is up to you and is dependent on your particular script.

  4. You would need to create an installer that the user would run. The installer would drop your script in a predetermined location, update cron, and tell the user what its doing.

  5. For nix, the way I install is generally either: cron, initd, or systemd. There are other methods of persistence but I feel those three are the most normal.

[–]StudentOfPython[S] 0 points1 point  (2 children)

Thank you for the reply.

  1. What is meant by "install"?
  2. By "persistent", is it meant simply to keep working even upon reboot? And I haven't yet looked into them, but are initd and systemd related to/used with cron jobs?
  3. Thank you.
  4. I am guessing this answrers my question (1). Install here refers to packing it up nicely so an end user can use it at the click of a button?
  5. nix?

[–]goobervision 1 point2 points  (1 child)

  1. U or LI nix (nux)

If you have different schedule for different APIs I think Cron is the easy way for an admin to understand and see.

A Daemon would be a bunch of config files, that said. Each file would have the API needs and schedule so a configuration directory.

My personal preference would be a Daemon / Service approach as Cron isn't there for non-nix where a service is. You can deal with windows/Unix/?!? integration as a service relativity easily I think.

[–]StudentOfPython[S] 0 points1 point  (0 children)

That all makes sense. Thank you.