Keep systemd service active between 18:00-08:00

Hi folks!

Im running hyprsunset at 18:00 every day to “emulate” nightmode for my screen (lower color temp to 5000k). This is done with a systemd timer+service.

My timer right now is triggered as usual at 18 and runs hyprsunset fine.
But what happens if the timer is triggered at 18 but then i restart my system at 19?
I assume the service wont be started again?

How can i keep my service active but only between 18:00 and 08:00 the next day, even thru system restarts?

This is my timer and service right now:

  systemd.user.timers."hyprsunset" = {
    wantedBy = [ "timers.target" ];
      timerConfig = {
        OnCalendar = "18:00:00";
        Unit = "hyprsunset.service";
    };
  };

  systemd.user.services."hyprsunset" = {  
    script = ''
      ${pkgs.hyprsunset}/bin/hyprsunset --temperature 5000
    '';
    serviceConfig = {
      Type = "oneshot";
    };
  };

Thank you for your time!
Regards

add another service start at login, check the time if it is between 18:00 and 08:00 then activate nightmode.

No extra service is needed if you modify hypersunset to check the time before changing the temperature and add default.target on wantedBy

Or you use wlsunset which you just start as a service and does all that for you. You just give it your geo coordinates and based on the sunset at that location it successively changes the Gama for you. This works flawlessly on my setup.

Thanks! Solved it like this:

  systemd.user.timers."hyprsunset" = {
    wantedBy = [ "timers.target" ];
      timerConfig = {
        OnCalendar = "18:00:00";
        Unit = "hyprsunset.service";
    };
  };

  systemd.user.services."hyprsunset" = {
    wantedBy = [ "default.target" ];
    script = ''
      current_hour=$(date +"%H")

      if [ "$current_hour" -ge 18 ] || [ "$current_hour" -lt 9 ]; then
        echo "The current time is after 18:00. Starting hyprsunset!"
        ${unstable.hyprsunset}/bin/hyprsunset --temperature 5000
      else
        echo "The current time is not after 18:00. Not starting hyprsunset."
      fi
    '';
    serviceConfig = {
      Type = "oneshot";
    };
  };

1 Like

Nice! Will definitely check that out!