Run jobs with 'set -e' so that they exit on error.
[whenjobs.git] / daemon / daemon.ml
index 47a67ed..6ccfcce 100644 (file)
@@ -198,7 +198,7 @@ and reload_file () =
     dependencies := map in
 
   (* Re-evaluate all when jobs. *)
     dependencies := map in
 
   (* Re-evaluate all when jobs. *)
-  reevaluate_whenjobs (StringMap.keys !jobs);
+  reevaluate_whenjobs ~onload:true (StringMap.keys !jobs);
 
   (* Schedule the next every job to run. *)
   schedule_next_everyjob ()
 
   (* Schedule the next every job to run. *)
   schedule_next_everyjob ()
@@ -207,7 +207,7 @@ and reload_file () =
  * a fixpoint.  Run those that need to be run.  every-statement jobs
  * are ignored here.
  *)
  * a fixpoint.  Run those that need to be run.  every-statement jobs
  * are ignored here.
  *)
-and reevaluate_whenjobs jobnames =
+and reevaluate_whenjobs ?(onload=false) jobnames =
   let rec loop set jobnames =
     let set' =
       List.fold_left (
   let rec loop set jobnames =
     let set' =
       List.fold_left (
@@ -218,7 +218,7 @@ and reevaluate_whenjobs jobnames =
           assert (jobname = job.job_name);
 
           let r, job' =
           assert (jobname = job.job_name);
 
           let r, job' =
-            try job_evaluate job !variables
+            try job_evaluate job !variables onload
             with Invalid_argument err | Failure err ->
               Syslog.error "error evaluating job %s (at %s): %s"
                 jobname (Camlp4.PreCast.Ast.Loc.to_string job.job_loc) err;
             with Invalid_argument err | Failure err ->
               Syslog.error "error evaluating job %s (at %s): %s"
                 jobname (Camlp4.PreCast.Ast.Loc.to_string job.job_loc) err;
@@ -353,12 +353,15 @@ and run_job job =
     (* Create a temporary file containing the shell script fragment. *)
     let script = dir // "script" in
     let chan = open_out script in
     (* Create a temporary file containing the shell script fragment. *)
     let script = dir // "script" in
     let chan = open_out script in
+    fprintf chan "set -e\n"; (* So that jobs exit on error. *)
     output_string chan job.job_script.sh_script;
     close_out chan;
     chmod script 0o700;
 
     output_string chan job.job_script.sh_script;
     close_out chan;
     chmod script 0o700;
 
+    let shell = try getenv "SHELL" with Not_found -> "/bin/sh" in
+
     (* Execute the shell script. *)
     (* Execute the shell script. *)
-    (try execvp "bash" [| "bash"; "-c"; script |];
+    (try execvp shell [| shell; "-c"; script |];
      with Unix_error (err, fn, _) ->
        Syslog.error "%s failed: %s: %s" fn script (error_message err)
     );
      with Unix_error (err, fn, _) ->
        Syslog.error "%s failed: %s: %s" fn script (error_message err)
     );