Cron

From NixOS Wiki
Revision as of 20:21, 17 February 2018 by imported>Sjau (<code> is easier to spot that <tt>)

Using Cron

General

Cron is a very useful tool to run stuff at predefined times. Users, if allowed, can setup their own cron job, while the system crontab can be easily setup from the configuration.nix

  # Enable cron service
  services.cron = {
    enable = true;
    systemCronJobs = [
      "*/5 * * * *      root    date >> /tmp/cron.log"
    ];
  };

The above example would run the command date >> /tmp/cron.log as root user every 5 minutes (indicated by */5*. For more information regarding the cronjob entries, see the link below.

Loading environment

However, sometimes cron won't run because it's missing the according environment (as it is the case for rss2email as example). In that case you just have to source the profile file first before running the desired command, like in the example below:

      "*/10 * * * *   johndoe   . /etc/profile; /run/current-system/sw/bin/r2e run"

The . /etc/profile; part first sources the profile file and hence loading the environment. After that, the actual command is being run in the proper environment. The above entry would run the rss2email program every 10 minutes as user johndoe

See also