Periodic background tasks on a server

What are the ways to run periodic background tasks on a server (Linux or FreeBsd) for an application in Ocaml? Those are tasks that’ll be run a few times per day and each will take up to an hour to complete. Storing/changing data in a database will be involved too.

Cron? Or anything else specific to OCaml?

Personally I would use systemd on Linux. On FreeBSD you might use cron I guess.

Interesting. I wouldn’t have thought that cron was no longer the go-to solution for this problem. Interesting.


A pure OCaml solution, so you don’t have to suffer with configuring your job schedules in lesser config languages, is to start an OCaml application on boot that daemonizes and schedules the tasks.

See the Daemon module in Core and or Clock.every in Async.


Well, except that that wouldn’t survive reboots; and if you’re going to add an init task, well, that’s what systemd is for these days after all, right?

1 Like

Agreed, there’s no need to reinvent the wheel here. Let service managers on the OS do their job (including daemonizing your service, taking care of the pidfile, taking care of capturing stdout/stderr and logging, …). Let the OCaml app focus on its job.

I think my answer is not that outlandish given the community.

In my experience people writing OCaml code care about correctness and effectiveness more than normal folk and quickly become dissatisfied with the jobs the standard operating environment systems are “supposed” to do for them.

Run OCaml apps on fleets of servers long enough and you start wondering at what point in the future you can replace some crappy Linux daemon you depend on with some OCaml code. Or when your Linux init program will be the path to your OCaml app. Or when the day you can move your OCaml app into ring 0 is and delete the Linux kernel :grin:

1 Like

I guess the crontab opam package is exactly what you are looking for:

opam search cron
# Packages matching: match(*cron*)
# Name  # Installed # Synopsis
crontab --          Interacting with cron from OCaml

Try goaljobs

Sure, but my suggestion does not preclude that. You can write a small, self-contained OCaml app that just does its ETL/data munging job, and deploy it with systemd for now, and then later as time allows write a ‘correct’ scheduling tool in OCaml and switch your job over to use that. It’s in fact more modular and generic than baking in the scheduling directly into your core app logic. And you may end up finding out that doing scheduling properly is not actually that easy and that tools that have spent years, sometimes decades working on it may do it better despite not being written in OCaml :slight_smile: