Systemd -> working directory?

Hello dear Nix wizards

I would like to run a python script with a systemd service/timer.
The script runs when directly executing via terminal.

The script is not running via the systemd service with the following error message:

Dez 25 09:27:27 svr0 obe[62073]: FileNotFoundError: [Errno 2] No such file or directory: '/home/svr/.cache/selenium/chromedriver/linux64/120.0.6099.109/chromedriver'

Here are the relevant parts of the script.py:

from selenium import webdriver
from selenium.webdriver.chrome.options import Options
...
# Run headless
chrome_options = Options()
chrome_options.add_argument("--headless=new")

wb = webdriver.Chrome(options=chrome_options)
wb.get(url)

and the systemd service/timer:

systemd.user.services."obe" = {
  enable = true;
  wantedBy = [ "default.target" ];
  partOf = [ "default.target" ];
  path = with pkgs; [
    (python311.withPackages(ps: with ps; [
      selenium
      python311Packages.requests
    ]))
    chromedriver                                   # A WebDriver server for running Selenium tests on Chrome
    chromium                                       # An open source web browser from Google
  ];
  script = "/home/svr/Syncthing/Scripts/obe.py";
};

systemd.user.timers."obe" = {
  enable = true;
  wantedBy = [ "timers.target" ];
  timerConfig = {
    OnCalendar = "Sun 11:09:00";
    RandomizedDelaySec = 3600;
    Unit = "obe.service";
  };
};

Would be so delighted to finally find a solution to this!

Looks like it’s trying to write a file to the cache directory of the user, but that user doesn’t have a cache directory (and presumably no home directory, assuming it’s a system user).

You should be setting the XDG_CACHE_HOME to a directory this user has access to.

Systemd has support for creating this directory with the right permissions and such through the CacheDirectory= setting. It even sets the XDG var for you.

2 Likes

thank you very much for your answer TLATER!

Sadly I still get the same error

I added

 serviceConfig = {
    CacheDirectory="/home/svr/.cache";
  };

like so:

systemd.user.services."obe" = {
  enable = true;
  wantedBy = [ "default.target" ];
  partOf = [ "default.target" ];
  serviceConfig = {
    CacheDirectory="/home/svr/.cache";
  };
  path = with pkgs; [
    (python311.withPackages(ps: with ps; [
      selenium
      python311Packages.requests
    ]))
    chromedriver                                   # A WebDriver server for running Selenium tests on Chrome
    chromium                                       # An open source web browser from Google
  ];
  script = "/home/svr/Syncthing/Scripts/obe.py";
};

Read the manual again:

These options take a whitespace-separated list of directory names. The specified directory names must be relative, and may not include “..”.

Use

serviceConfig.CacheDirectory = "obe";

This will reliably create a directory /home/svr/.cache/obe to place the files in (or presumably an error if the user lacks a home directory?).

If you still get the same error, your application doesn’t listen to $XDG_CACHE_HOME for some reason, and you’ll have to find out why - or add another CacheDirectory to create what it expects or something like that.