5 whenjobs - A powerful but simple cron replacement
9 Editing the jobs script:
14 Get and set variables:
16 whenjobs --get variable
17 whenjobs --set variable=value [variable=value ...]
20 Start and stop the per-user daemon:
22 whenjobs --daemon-start
23 whenjobs --daemon-stop
24 whenjobs --daemon-status
25 whenjobs --daemon-restart
30 whenjobs --cancel serial
31 whenjobs --start "name"
32 whenjobs --tail serial
36 Whenjobs is a powerful but simple replacement for cron. It lets you
37 run jobs periodically like cron, but it also lets you trigger jobs to
38 run when user-defined variables are set or change value.
40 Periodic jobs are written like this:
44 # Get the current load average.
45 load=`awk '{print $1}' /proc/loadavg`
46 whenjobs --set --type float load=$load
49 When-statements let you create jobs that run based on variables set
54 mail -s "ALERT: high load average: $load" $LOGNAME < /dev/null
57 (When statements are "edge-triggered", meaning that this job will only
58 run when the load goes from under 6 to E<ge> 6).
60 Like L<crontab(5)>, whenjobs are controlled by a jobs file which can
61 be edited from the command line:
65 Whenjobs uses a daemon called L<whenjobsd(8)>. Unlike crond, this
66 daemon runs as the same user. Each user who wants to use whenjobs
67 starts their own daemon:
69 $ whenjobs --daemon-start
71 You can also have the daemon start as you when the machine boots by
72 adding the following line to a boot file such as C</etc/rc.local>.
73 Replace C<username> with your username:
75 su username -c /usr/sbin/whenjobsd
77 Variables are the key to expressing dependencies between whenjobs.
78 Variables are stored (per-user) in the daemon. You can use the
79 command line tool to examine and set variables:
81 $ whenjobs --variables
83 libguestfs_build_local=1.17.16
84 libguestfs_commit=7e32d892d76a31f55e2a4151902623b9949e3efa
85 libguestfs_dist=1.17.16
86 libguestfs_release=1.17.16
87 libguestfs_stable_build_local=1.16.10
88 libguestfs_stable_commit=27433a0a335301441b1eb6244ba425c2c44b2d99
89 libguestfs_stable_dist=1.16.10
90 libguestfs_stable_release=1.16.10
91 libguestfs_stable_version=1.16.10
92 libguestfs_version=1.17.16
93 $ whenjobs --set cat=sushi
97 Note: The act of setting a variable (using I<--set>) can trigger jobs
100 You can also list out what jobs are running:
103 287 libguestfs-stable: fedora 16
104 running in: /tmp/whenjobsa2afc44fd757465f95438309f1a51609
105 started at: 2012-03-13 10:59:37
107 and you can 'tail' the output of running jobs which is useful for
110 $ whenjobs --tail 287
111 Uploading: 147496271972717053d46b82a07435ca libguestfs-1.16.10.tar.gz
113 You can start and cancel jobs manually:
115 $ whenjobs --start 'libguestfs: poll'
116 $ whenjobs --cancel 287
122 =item B<--cancel> serial
124 Cancel the job with the given serial number.
126 Use I<--jobs> to list running jobs along with their serial numbers.
127 The serial number is also available in the job script (as
128 C<$JOBSERIAL>) and in the log file.
130 =item B<--daemon-start>
132 =item B<--daemon-stop>
134 Start and stop the per-user daemon.
136 =item B<--daemon-status>
138 Prints the status of the daemon: C<up> or C<down>.
140 =item B<--daemon-restart>
142 Restart the daemon. (If it is not running, then this command
149 Edit the jobs script. If you make changes to the jobs script, then it
150 is automatically uploaded to the daemon.
152 The C<$EDITOR> environment variable is used for editing. If not set,
155 =item B<--get> variable
157 Print the value of a variable.
163 Display brief usage and exit.
167 List the names of all loaded jobs (whether they are running or not).
168 Use I<--jobs> to list running jobs.
172 List all running jobs.
174 Note that it is possible for the same job to be running more than once
175 (for example, a periodic job that takes longer than the period to run).
181 List the jobs script.
183 =item B<--lib> directory
185 Set the library directory which needs to contain the auxiliary files
186 C<pa_when.cmo> and C<whenlib.cma>. Normally you do not need to
187 specify this. However if you are running whenjobs without installing
188 it, then you need to point this to the C<lib/> directory from the
191 whenjobs --lib $builddir/lib -e
193 =item B<--set> variable=value [variable=value ...]
195 =item B<--type> bool|int|float|string|unit
197 I<--set> sets the variable named C<variable> to the new C<value>. The
198 variable is created if it does not already exist. Note that setting a
199 variable can cause jobs to run immediately.
201 To unset a variable, set it to the empty string like this:
205 By default variables are strings. You can also set the type of a
206 variable when setting it by adding the optional I<--type> parameter.
207 The I<--type> parameter should come I<before> the variable
208 declaration, like this:
210 whenjobs --set --type int free_space=10000
212 See the discussion of variable types in the L</REFERENCE> section
215 You can set multiple variables. When setting multiple variables in a
216 single command, the values are all changed in a single atomic
219 whenjobs --set cat=sushi food=fish
221 When using I<--type> and multiple variables, the type changes the
222 remaining command line parameters until the next I<--type>, eg:
224 whenjobs --set cat=sushi \
225 --type float weight=3.5 \
226 --type string food=fish
228 (C<cat> and C<food> are strings, and C<weight> is a float).
230 =item B<--start> "job name"
232 Start the job immediately and unconditionally.
234 This runs the job even if its normal preconditions are not met. This
235 may cause unexpected results, so use with caution.
237 =item B<--tail> serial
239 Tail the output of the running job identified by its serial number.
240 Use the I<--jobs> flag to get a list of running jobs.
242 =item B<--test> variable=value [variable=value ...]
244 This works the same way as the I<--set> option, but the difference is
245 that the variables are not set. Instead, it lists out the jobs that
246 I<would> run, I<if> the variables were updated to these new values.
248 The variables are not actually updated, and the jobs are not actually
251 The output is a list of job names that would run.
255 Compile the jobs script and upload it to the daemon, without editing.
256 Note that the I<--edit> option does this automatically. Furthermore,
257 when the daemon is started it checks for a jobs script and loads it if
262 Display all the variables and their values, in the format C<name=value>.
268 Display the name and version of the program and exit.
270 =item B<--whisper> variable=value [variable=value ...]
272 This works the same way as the I<--set> option, but with the
273 difference that jobs' when clauses are not reevaluated. In other
274 words, the variables are set, but "quietly" so as not to trigger any
277 Note that this can lead to some unexpected results: one case is a
280 when changed a || changed b : << ... >>
282 If C<a> is changed using I<--whisper>, then the job will not run.
284 But later on, if C<b> is set but to the same value that it already has
285 (ie. not changed), the job will run because the whole when-clause is
286 reevaluated and C<a> is found to have changed since the last run of
293 A whenjobs file consists of a series of one or more "every" or "when"
296 Comments in the file can be written using C<(* ... *)>. Comments
299 Shell script fragments are written using C<E<lt>E<lt> ... E<gt>E<gt>>.
300 Within shell script fragments, use C<#> for comments (as in ordinary
301 shell scripts). Because C<E<gt>E<gt>> has a special meaning, it
302 cannot be used in the shell script (ie. for redirection). You have to
303 write C<E<gt>\E<gt>> instead which is replaced with C<E<gt>E<gt>> when
304 the shell script is parsed.
306 =head2 EVERY STATEMENTS (PERIODIC JOBS)
308 An every statement has the form:
315 where C<E<lt>periodE<gt>> is a I<period expression>, which may take
316 one of the forms below. Don't forget the colon character between the
317 period expression and the shell script.
319 An every statement is a job which runs periodically.
321 =head3 PERIOD EXPRESSIONS
325 =item B<every second>
327 The job runs every second.
329 =item B<every minute>
331 The job runs every minute.
335 The job runs every hour.
339 The job runs every day, at midnight UTC.
343 The job runs every week, on a Thursday at midnight UTC.
347 The job runs every month, on the first of the month at midnight UTC.
351 The job runs every year, on the first day of the year at midnight UTC.
353 =item B<every decade>
355 =item B<every century>
357 =item B<every millenium>
359 The job runs every 10, 100 or 1000 years.
361 =item B<every I<N> seconds>
363 The job runs every I<N> seconds (I<N> is any number E<ge> 1).
365 =item B<every I<N> minutes>
367 The job runs every I<N> minutes.
369 =item B<every I<N> hours>
371 The job runs every I<N> hours.
373 =item B<every I<N> days>
375 The job runs every I<N> days.
377 =item B<every I<N> weeks>
379 The job runs every I<N> weeks.
381 =item B<every I<N> months>
383 The job runs every I<N> months.
385 =item B<every I<N> years>
387 =item B<every I<N> decades>
389 =item B<every I<N> centuries>
391 =item B<every I<N> millenia>
393 The job runs every I<N>, I<10*N>, I<100*N> or I<1000*N> years.
397 =head2 WHEN STATEMENTS (DEPENDENT JOBS)
399 A when statement has the form:
406 where C<E<lt>exprE<gt>> is a I<when expression>, described below.
407 Don't forget the colon character between the period expression and the
410 A when statement is a job which runs when the conditions described in
411 its when-expression become true.
413 When jobs are I<edge triggered>. This means that they run when the
414 condition changes from false to true (or in the case where the
415 expression has not been evaluated before, when it evaluates initially
418 =head3 WHEN EXPRESSIONS
420 When expressions are fully recursive expressions constructed from the
425 =item I<expr> B<&&> I<expr>
427 =item I<expr> B<||> I<expr>
429 The boolean "and" or "or" of the two sub-expressions.
431 =item I<expr> B<E<lt>> I<expr>
433 =item I<expr> B<E<lt>=> I<expr>
435 =item I<expr> B<==> I<expr>
437 =item I<expr> B<E<gt>=> I<expr>
439 =item I<expr> B<E<gt>> I<expr>
441 The two sub-expressions are evaluated and the usual comparison
442 operator is performed.
444 If the sub-expressions are numeric, then numeric comparison is done.
445 If either sub-expression is non-numeric, then both expressions are
446 converted (if necessary) to strings and string comparison is done.
450 Boolean negative of I<expr>.
452 =item I<expr> B<+> I<expr>
454 For numeric sub-expressions, this performs addition.
456 If both sub-expressions are strings, this performs string
459 Other types give an error.
461 =item I<expr> B<-> I<expr>
463 =item I<expr> B<*> I<expr>
465 =item I<expr> B</> I<expr>
467 =item I<expr> B<mod> I<expr>
469 Both sub-expressions are evaluated, and if both are numeric, then the
470 result is subtraction, multiplication, division or modulo.
472 Other types give an error. Note that I<mod> really is an infix
477 If I<expr> is a string, this returns the length of the string.
481 The value of the named variable.
483 Previously undefined variables are automatically initialized to the
486 =item B<prev> I<variable>
488 The I<previous> value of the named variable. This means, the value
489 that it had last time this when-job ran.
491 If the when-job has not run yet, then this returns C<"">.
493 Job state is preserved across file reloads, but I<only> for jobs that
494 are explicitly named. If you find that jobs using C<prev>, C<changes>
495 etc are running unnecessarily when the jobs file is edited or
496 uploaded, try giving the jobs an explicit name.
498 =item B<changes> I<variable>
500 If the named variable has changed since this job last ran, then this
501 evaluates to true, else false.
503 This is the same as writing C<prev variable == variable>.
505 =item B<increases> I<variable>
507 If the named variable has changed and increased since this job last
508 ran, then this evaluates to true, else false.
510 This is the same as writing C<prev variable E<lt> variable>.
512 =item B<decreases> I<variable>
514 If the named variable has changed and decreased since this job last
515 ran, then this evaluates to true, else false.
517 This is the same as writing C<prev variable E<gt> variable>.
519 B<Note:> There is a subtle gotcha with the I<decreases> operator: The
520 first time the expression is evaluated, the job has (by definition)
521 not yet run. Therefore C<prev variable> evaluates to C<""> (see
522 definition of I<prev> above). Since it is always true that
526 the I<decreases> operator evaluates to false, and since this usually
527 means the job does not run, the operator always evaluates to false.
529 To fix this, ensure that the variable is initialized (see
530 L</SETTING THE INITIAL VALUE OF VARIABLES> below).
534 This evaluates to true the first time the expression is evaluated
535 after the jobs file has been reloaded or the daemon restarted.
536 Thereafter it evaluates to false.
538 Don't use this to initialize variables: it won't do what you mean.
544 Constants that evaluate to boolean false or true respectively.
546 =item I<"any string">
550 In a boolean context, the empty string evaluates to false, and
551 non-empty strings evaluate to true.
555 Any integer. (Arbitrarily large integers are supported.)
557 In a boolean context, 0 evaluates to false, and non-zero evaluates to
568 Any floating point number.
570 In a boolean context, 0 evaluates to false, and non-zero evaluates to
577 The code between C<E<lt>E<lt> ... E<gt>E<gt>> is a shell script. It
578 is executed using C<$SHELL>, or if that environment variable is not
581 =head3 SHELL SCRIPT VARIABLES
583 Every variable that has been set (using the whenjobs I<--set> option)
584 is exported to the script, so you can simply get the value of any
585 variable by writing C<$name>.
587 In addition, there are some special variables available:
593 The name of the job. If the job has been named explicitly, then that
594 name is available through this variable, else it will be some implicit
599 The serial number of the job. This is simply a variable that
600 increments each time a job is run, and is unique to that run of the
605 Other environment variables such as C<$HOME>, C<$LOGNAME> etc are
608 =head3 SHELL SCRIPT TEMPORARY CURRENT DIRECTORY
610 The shell script runs with its current directory set to a temporary
611 directory. The temporary directory is removed when the shell script
612 exits. Therefore you can write temporary files here without worrying
613 about cleaning them up.
615 If you want to store permanent state, then you have to save it to a
616 well-known directory, eg. C<$HOME>, C</var> etc.
618 =head3 SHELL SCRIPT USER
620 The shell script runs as the ordinary user. It has no special
625 Jobs are given implicit names (C<job$1>, C<job$2> etc.). You can also
626 name jobs explicitly by preceeding the "every" or "when" statement
635 The job name is passed to the shell script in the C<$JOBNAME>
636 environment variable.
638 =head2 OCAML EXPRESSIONS
640 As well as simple "every" and "when" expressions, advanced users may
641 want to use arbitrary OCaml expressions, functions, etc in the jobs
642 script. These are useful for factoring common code or strings, for
643 setting the initial values of variables, or for defining pre and post
646 A simple example of an OCaml expression is:
648 let prefix = "daily_"
650 job (prefix ^ "virus_scan")
656 job (prefix ^ "disk_check")
662 which creates two jobs called C<"daily_virus_scan"> and
663 C<"daily_disk_check"> (C<^> is the OCaml string concatenation
666 OCaml expressions have access to a library of functions called
667 B<Whentools> which is described below. It lets you set variables,
668 create jobs algorithmically, etc.
670 The OCaml expressions run once, when the jobs file is being loaded or
673 =head3 SETTING THE INITIAL VALUE OF VARIABLES
675 Variables are created when they are referenced, and until set they
676 have the value empty string (just like the shell). Across file
677 reloads, the previous values of variables are preserved.
679 To initialize a variable to a known value when the jobs file is
680 loaded, call one of the C<Whentools.set_variable*> functions as in
684 Whentools.set_variable "name" "Richard";
685 Whentools.set_variable_int "counter" 0
689 Before a job runs, you can arrange that a C<pre> function is called.
690 This function may decide not to run the job (by returning C<false>).
692 One use for this is to prevent a particular job from running if there
693 is already an instance of the same job running:
696 pre (Whentools.one ())
699 # Takes longer than 10 seconds to run, but 'Whentools.one ()'
700 # will ensure only one is ever running.
704 When using pre functions, jobs must be given an explicit name, ie.
705 you must use the C<job> statement.
707 A number of pre functions are available in the library; see below.
709 You can also write your own post functions (in OCaml). The function
710 is passed one argument which is a C<Whentools.preinfo> struct, defined
711 below. It should return a boolean: C<true> if the job should run, and
712 C<false> if the job should not run.
714 Note that a fresh serial number (see L</JOBSERIAL>) is assigned to
715 each run, whether or not the job actually runs because of
718 =head3 POST FUNCTIONS
720 After a job runs, you can control what happens to its output by
721 writing a C<post> function. To write a post function you have to
722 name the job (ie. have an explicit C<job> statement). Put C<post ...>
723 after the job name like this:
726 post (Whentools.mailto "you@example.com")
732 A number of post functions are available in the library; see below.
734 You can also write your own post functions (in OCaml). The
735 function is passed one argument which is a C<Whentools.result> struct,
738 =head3 WHENTOOLS LIBRARY
744 =item B<Whentools.mailto> [I<~only_on_failure:true>]
745 [I<~from:from_address>] I<email_address> I<result>
747 This built-in post function sends the result of the script by email to
748 the given email address.
750 If the optional C<~only_on_failure:true> flag is set, then it is only
751 sent out if the script failed.
753 If the optional C<~from> flag is set, then the from address is set
754 accordingly. This is sometimes needed when sending mail.
756 Note the C<result> parameter is passed implicitly by the daemon. You
757 do not need to add it.
759 Here are some examples of using the mailto function:
762 post (Whentools.mailto "you@example.com")
769 post (Whentools.mailto ~only_on_failure:true
776 let from = "me@example.com"
777 let to_addr = "you@example.com"
780 post (Whentools.mailto ~from to_addr)
786 =item B<Whentools.max> I<n>
788 This built-in pre function ensures that a maximum of I<n> instances of
791 It checks the list of running jobs, and if I<n> or more instances are
792 already running, then it returns C<false>, which ensures that the new
795 =item B<Whentools.one> I<()>
797 This built-in pre function ensures that only one instance of the job
798 is running. It is the same as calling:
802 =item B<Whentools.set_variable> I<name> I<string>
804 Set variable I<name> to the string.
806 =item B<Whentools.set_variable_bool> I<name> I<b>
808 Set variable I<name> to the boolean value I<b>.
810 =item B<Whentools.set_variable_int> I<name> I<i>
812 Set variable I<name> to the integer value I<i>.
814 =item B<Whentools.set_variable_string> I<name> I<s>
816 Set variable I<name> to the string value <s>. This is
817 the same as I<Whentools.set_variable>.
819 =item B<Whentools.set_variable_float> I<name> I<f>
821 Set variable I<name> to the floating point value I<f>.
829 =item B<Whentools.preinfo>
831 This structure is passed to pre functions. It has the following
835 pi_job_name : string; # Job name.
836 pi_serial : Big_int.big_int; # Job serial number.
837 pi_variables : (string * variable) list; # Variables set in job.
838 pi_running : preinfo_running_job list; # List of running jobs.
840 and preinfo_running_job = {
841 pirun_job_name : string; # Running job name.
842 pirun_serial : Big_int.big_int; # Running job serial number.
843 pirun_start_time : float; # Running job start time.
844 pirun_pid : int; # Running job process ID.
847 =item B<Whentools.result>
849 This structure is passed to post functions. It has the following
853 res_job_name : string; # job name
854 res_serial : big_int; # job serial (same as $JOBSERIAL)
855 res_code : int; # return code from the shell script
856 res_tmpdir : string; # temporary directory script ran in
857 res_output : string; # filename of stdout/stderr output
858 res_start_time : float; # when the job started
867 =head1 ENVIRONMENT VARIABLES
877 Richard W.M. Jones L<http://people.redhat.com/~rjones/>
881 Copyright (C) 2012 Red Hat Inc.
883 This program is free software; you can redistribute it and/or modify
884 it under the terms of the GNU General Public License as published by
885 the Free Software Foundation; either version 2 of the License, or
886 (at your option) any later version.
888 This program is distributed in the hope that it will be useful,
889 but WITHOUT ANY WARRANTY; without even the implied warranty of
890 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
891 GNU General Public License for more details.
893 You should have received a copy of the GNU General Public License
894 along with this program; if not, write to the Free Software
895 Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.