What is the best task scheduling approach in Plone 4? - cron

We need to schedule some tasks in Plone 4 (notify users after n days of inactivity, etc.). What is the best way to do it? Is there something in Plone or maybe an old cron job? I would like to avoid cron4plone.

Simply use the built-in <clock-server> functionality in zope.conf; list them in the zope-conf-additional option of plone.recipe.zope2instance:
zope-conf-additional =
<clock-server>
method /Plone/path/to/callable
period 7200
user username-to-invoke-method-with
password password-for-user
host localhost
</clock-server>
The above snippet will call /Plone/path/to/callable every 2 hours, with the Host header set to localhost with the configured user and password.
The clock-server was added to Zope 2.10; before this it was a separate product by Chris McDonough. I generally created dedicated views for such tasks.
The alternative is to use a cron job to call either a view (usually with wget or cron) or a zopectl command line script. I use this when I need precise control over when the script needs to be executed, such as at midnight every day.

Related

Linux doesn't execute scheduled job later than schulded time

I am beginner with Linux cron job.
Requirement:
Record attendance of employee. Every dayat 10 AM a Jar should execute and asks to login to record his/her presence.
Cron Job
I have created a cron job and placed at /etc/cron.d/ and has below line.
0 10 * * * /home/user/Documents/attendance.sh
If system get started before 10 AM then a login screen UI comes but if user comes late and start system after 10 AM then it doesn't show login screen UI.
So it should execute JAR even though user login after 10 AM.
Also, pleaes let me know if there is another way to achieve this goal.
Please guide.
Thanks,
Ankur
A couple of alternatives occur to me.
1 If you'd like to have attendance.sh run for every user at login, you could add it to the system-wide login shell rc file, probably /etc/profile.
2 If it's available, you could use anacron, which is well suited for systems that are not running all the time. You could schedule attendance.sh to run daily at a particular time after login.
These alternatives will behave differently. 1 will run at each login, so several times a day or only once in several days, depending upon whether the user logs in and out daily, several times daily or stays logged in when they leave work. 2 will run every day that the user is logged in (assuming that they stay logged in at least as long as the delay you specify).
If your requirements are more exacting, say "run at 10 for all logged in users, otherwise run when a late user logs in". You could make something work, but doing so would require some extra work.
Added following questioners comment
As I alluded to in my discussion of how 1 & 2 differ there can be problems with 1 if the user does not just log in at the start of their work day and log out at it's end. It sounds to me as if what you are really after is an application that is tied into the window manager. I am not experienced with triggering scripts with window manager events, but surely it's possible to do so, e.g. Run script on screen lock/unlock. You may have to do some research depending upon your needs and the sort of systems that you have. One thing to keep in mind is that a solution that works for the user who doesn't log out every day may backfire on those who log out several times a day. Solution 2 may be the best of the easy straightforward solutions for your problem, but you may need something more elaborate to handle edge cases and where (perhaps) employee performance is being monitored.
You can make the script run hourly. Script checks the time, if it's 10 or later, it checks if the user logged in today, and if not asks him to login. So you can record the login hour too.
Edit: You can put your script in /etc/cron.hourly

Is Cron the right option for this?

I am trying to create a script that watches my college time table and registers them for a class when it is open. Kind of like an Ebay auction sniper. I was wondering if cron is the right tool for this. I need to be able to run the script for every user. The user will enter their username and password and the script will query the timetable.
Looking for some advice on if cron is the tool or if there are other tools out there.
cron runs a particular program or script at a specified time. For example, if you wanted a report compiled and e-mailed every day at 2 a.m., that would be a cron job.
In this sense, cron has a timetable, but I am not sure that it is the sort of timetable of which you are thinking.
From a system-design perspective, the clean way to achieve the effect you want naturally would be to let the students' class requests join a queue, then to have the college's registrar's own computer take requests from the queue as seats became available. However, I assume from your Ebay reference that this is not possible in your case.

cron jobs: Monitor time it takes for jobs to finish

I'm doing a research project that requires I monitor cron jobs on a Ubuntu Linux system. I have collected data about the jobs' tasks and when they are started, I just don't know of a way to monitor how long they take to finish running.
I could calculate the time of finishing the task minus starting it with something like this but that would require doing that on the Shell scripts of each cron job. That's not necessarily difficult by any means but it seems a little silly that cron wouldn't in some way log this, so I'm trying to find an easier way :P
tl;dr Figure out time cron jobs take from start to finish
You could just put time in front of your crontabs, and if you're getting notifications about cron script outputs, it'll get sent to you.
For example, if you had:
0 1,13 * * * /maint/run_webalizer.sh
add time in front
0 1,13 * * * time /maint/run_webalizer.sh
and you'll get some output that looks like (the "real" is the time you want):
real 3m1.255s
user 0m37.890s
sys 0m3.492s
If you don't get cron notifications, you can just pipe the output to a file.
man time. Maybe you can create a wrapper and tell Cron to use it as your "shell" or something like that.
Cronitor (https://cronitor.io) is a tool I built exactly for this purpose. It uses http requests to record the start and end of your jobs.
You'll be notified if your job doesn't run on schedule, or if it runs for too long/too short. You can also configure it to send alerts to you via email, sms, but also Slack, Hipchat, Pagerduty and others.
I use the Jenkins CI to do this via its external-monitor-job plugin. Jenkins can track start and end times, track overall execution time over time, save the output of all jobs it tracks, and present success/failure conditions graphically.
https://wiki.jenkins-ci.org/display/JENKINS/Monitoring+external+jobs

How to define frequency of a job in application by users?

I have an application that has to launch jobs repeatingly. But (yes, that would have been to easy without a but...) I would like users to define their backup frequency in application.
In worst case, they would have to choose between :
weekly,
daily,
every 12 hours,
every 6 hours,
hourly
In best case, they should be able to use crontab expressions (see documentation for example)
How to do this? Do I launch a job every minutes that check for last execution time, frequency and then launches another job if needed? Do I create a sort of queue that will be executed by a masterjob?
Any clues, ideas, opinions, best pratices, experiences are welcome!
EDIT : Solved this problem using Akka scheduler. Ok, this is a technical solution not a design answer but still everything works great.
Each user defined repetition is an actor that send messages every period to a new actor to execute the actual job.
There may be two ways to do this depending on your requirements/architecture:
If you can only use Play:
The user creates the job and the frequency it will run (crontab, whatever).
On saving the job, you calculate the first time it will have to be run. You then add an entry to a table JOBS with the execution time, job id, and any other information required. This is required as Play is stateless and information must be stored in the DB for later retrieval.
You have a job that queries the table for entries whose execution date is less than now. Retrieves the first, runs it, removes it from the table and adds a new entry for next execution. You should keep some execution counter so if a task fails (which means the entry is not removed from DB) it won't block execution of the other tasks by the job trying again and again.
The frequency of this job is set to run every second. That way while there is information in the table, you should execute the request around as often as they are required. As Play won't spawn a new job while the current one is working if you have enough tasks this one job will serve all. If not, it will be killed at some point and restored when required.
Of course, the crons of the users will not be too precise, as you have to account for you own cron delays plus execution delays on all the tasks in queue, which will be run sequentially. Not the best approach, unless you somehow disallow crons which run every second or more often than every minute (to be safe). Doing a check on execution time of the crons to kill them if they are over a certain amount of time would be a good idea.
If you can use more than Play:
The better alternative I believe is to use Quartz (see this) to create a future execution when the user creates the job, and reproram it once the execution is over.
There was a discussion on google-groups about it. As far as I remember you must define a job which start every 6 hours and check which backups must be done. So you must remember when the last backup job was finished and make the control yourself. I'm unsure if Quartz can handle such a requirement.
I looked in the source-code (always a good source ;-)) and found a method every, where I think this should be do what you want. How ever I'm unsure if this is a clever design, because if you have 1000 user you will have then 1000 Jobs. I'm unsure if Play was build to handle such a large number of jobs.
[Update] For cron-expressions you should have a look into JobPlugin.scheduleForCRON()
There are several ways to solve this.
If you don't have a really huge load of jobs, I'd just persist them to a table using the required flexibility. Then check all of them every hour (or the lowest interval you support) and run those eligible. Simple.
Or, if you prefer to use cron syntax anyway, just write (export) jobs to a user crontab using a wrapper which calls back to your running app, or starts the job in a standalone process if that's possible.

How can I setup a system to tell me if a cron job is NOT running fine?

This is more of an "general architecture" problem. If you have a cron job (or even a Windows scheduled task) running periodically, its somewhat simple to have it send you an email / text message that all is well, but how do I get informed when everything is NOT okay? Basically, if the job doesn't run at its scheduled time or Windows / linux has its own set of hangups that prevent the task from running...?
Just seeking thoughts of people who've faced this situation before and come up with interesting solutions...
A way I've done it in the past is to simply put at the top of each script (say, checkUsers.sh):
touch /tmp/lastrun/checkUsers.sh
then have another job that runs periodically that uses find to locate all those "marker" files in tmp/lastrun that are older than a day.
You can fiddle with the timings, having /tmp/lastrun/hour/ and tmp/lastrun/day/ to separate jobs that have different schedules.
Note that this won't catch scripts that have never run since they will never create the initial file for find-ing. To alleviate that, you can either:
create that file manually when creating the cron job (won't handle situations where someone inadvertently deletes the marker file); or
maintain a list of required marker files somewhere so that you can detect when they're missing as well as outdated.
And, if your cron job is not a script, put the touch directly into crontab:
0 4 * * * ( touch /tmp/lastrun/daily/checkUsers ; /usr/bin/checkUsers )
It's a lot easier to validate a simple find script than to validate every one of your cron jobs.

Resources