How to make dune run in crontab?

There’s a simple demo project js101,

bin/dune:

(executable
 (name main)
 (modes js)
 (preprocess (pps js_of_ocaml-ppx))
 (libraries js_of_ocaml js_of_ocaml-ppx))

bin/main.ml:

open Js_of_ocaml

let () =
  let console = Js.Unsafe.pure_js_expr "console" in
  let log_func = Js.Unsafe.get console "log" in
  let message = Js.string "Hello, JavaScript!" in
  Js.Unsafe.fun_call log_func [| Js.Unsafe.inject message |]

For some reason, we need to run it in crontab, so here’s the testjs.sh:

rm -rf /home/ubuntu/todel/js101/_build/;
cd /home/ubuntu/todel/js101/;
dune build;
cp /home/ubuntu/todel/js101/_build/default/bin/main.bc.js /home/ubuntu/todel/;
echo "done";

Here’s the crontab:

*/5 * * * * sh /home/ubuntu/todel/testjs.sh >> /home/ubuntu/todel/testjs.log

It seems that the script been triggered success with:

$ cat testjs.log 
done
done

However, there no main.bc.js file generated by the crontab.

~/todel$ ls
js101  testjs.log  testjs.sh

But when I run the script from terminal, it does there:

~/todel$ sh testjs.sh 
done                  

~/todel$ ls
js101  main.bc.js  testjs.log  testjs.sh

Maybe you forgot to load your opam env in your script?

Also I would use ˋset -eˋ and ˋset -x` to debug things further (though I’m not 100% sure if those are bash only switches)

You should also redirect stderr, otherwise you risk missing some error messages.

Cheers,
Nicolas

It seems that there’s some error with the so, the log shows:

+ export PATH=/home/ubuntu/.opam/ide-dev/bin:/usr/bin:/bin
+ export LD_LIBRARY_PATH=/home/ubuntu/.opam/ide-dev/lib/stublibs:
+ rm -rf /home/ubuntu/todel/js101/_build/
+ cd /home/ubuntu/todel/js101/
+ dune build
File "_none_", line 1:
Error: I/O error: dlljsoo_runtime_stubs.so: No such file or directory

I have set the LD_LIBRARY_PATH in the script:

set -e
set -x

export PATH="/home/ubuntu/.opam/ide-dev/bin:$PATH"
export LD_LIBRARY_PATH="/home/ubuntu/.opam/ide-dev/lib/stublibs:$LD_LIBRARY_PATH"

rm -rf /home/ubuntu/todel/js101/_build/;
cd /home/ubuntu/todel/js101/;
dune build;
cp /home/ubuntu/todel/js101/_build/default/bin/main.bc.js /home/ubuntu/todel/;
echo "done";

And the so exists:

$ ls -l /home/ubuntu/.opam/ide-dev/lib/stublibs/dlljsoo_runtime_stubs.so
-rwxr-xr-x 1 ubuntu ubuntu 56808 May 30 15:31 /home/ubuntu/.opam/ide-dev/lib/stublibs/dlljsoo_runtime_stubs.so

Update: It seems that we only need set CAML_LD_LIBRARY_PATH