【问题标题】:Run a script with systemd timers on Nixos在 Nixos 上运行带有 systemd 计时器的脚本
【发布时间】:2021-04-02 13:03:36
【问题描述】:

我有一个小的 shellscript scrape.sh 可以抓取网站并将生成的数据放入新目录:

website='test.com'
dir_name="datev_data/$(date -u +'%Y-%m-%dT%H:%M:%S')"
mkdir $dirname
wget --directory-prefix="$dir_name" "$website"

(我真的不在乎数据最终在哪里,只要它每次都获取一个新目录并且我可以访问数据。因此我现在将它放入我的主目录/home/kaligule。)

手动运行这个脚本效果很好,所以现在我想在我的 nixos-server 上每小时运行一次这个脚本。因此,我将以下内容放入我的配置中(受this post 启发):

    systemd.services.test_systemd_timers = {
      serviceConfig.Type = "oneshot";
      script = ''
        echo "Will start scraping now."
        {pkgs.bash}/bin/bash /home/kaligule/scrape.sh
        echo "Done scraping."
      '';
    };

    systemd.timers.test_systemd_timers = {
      wantedBy = [ "timers.target" ];
      partOf = [ "test_systemd_timers.service" ];
      timerConfig.OnCalendar = [ "*-*-* *:00:00" ];
    };

然后我测试一下:

sudo nixos-rebuild switch # everything is fine
sudo systemctl start test_systemd_timers # run it immediatelly for testing

我明白了:

Job for test_systemd_timers.service failed because the control process exited with error code.
See "systemctl status test_systemd_timers.service" and "journalctl -xe" for details.

第一个建议的命令给了我这个:

● test_systemd_timers.service
     Loaded: loaded (/nix/store/f8348svxpnn6qx08adrv5s7ksc2zy1sk-unit-test_systemd_timers.service/test_systemd_timers.service; linked; vendor preset: enabled)
     Active: failed (Result: exit-code) since Fri 2021-04-02 14:50:02 CEST; 2min 36s ago
TriggeredBy: ● test_systemd_timers.timer
    Process: 5686 ExecStart=/nix/store/4smyxxxlhnnmw8l6l3nnfjyvmg0wxcwh-unit-script-test_systemd_timers-start/bin/test_systemd_timers-start (code=exited, status=127)
   Main PID: 5686 (code=exited, status=127)
         IP: 0B in, 0B out
        CPU: 11ms

Apr 02 14:50:02 regulus systemd[1]: Starting test_systemd_timers.service...
Apr 02 14:50:02 regulus test_systemd_timers-start[5686]: Will start scraping now.
Apr 02 14:50:02 regulus test_systemd_timers-start[5687]: /nix/store/4smyxxxlhnnmw8l6l3nnfjyvmg0wxcwh-unit-script-test_systemd_timers-start/bin/test_systemd_timers-start: line 3: {pkgs.bash}/bin/bash: No such file or directory
Apr 02 14:50:02 regulus systemd[1]: test_systemd_timers.service: Main process exited, code=exited, status=127/n/a
Apr 02 14:50:02 regulus systemd[1]: test_systemd_timers.service: Failed with result 'exit-code'.
Apr 02 14:50:02 regulus systemd[1]: Failed to start test_systemd_timers.service.

第二个建议的命令给了我:

Apr 02 14:54:42 regulus systemd[1]: Starting test_systemd_timers.service...
░░ Subject: A start job for unit test_systemd_timers.service has begun execution
░░ Defined-By: systemd
░░ Support: https://lists.freedesktop.org/mailman/listinfo/systemd-devel
░░ 
░░ A start job for unit test_systemd_timers.service has begun execution.
░░ 
░░ The job identifier is 34454.
Apr 02 14:54:42 regulus test_systemd_timers-start[5734]: Will start scraping now.
Apr 02 14:54:42 regulus test_systemd_timers-start[5735]: /nix/store/4smyxxxlhnnmw8l6l3nnfjyvmg0wxcwh-unit-script-test_systemd_timers-start/bin/test_systemd_timers-start: line 3: {pkgs.bash}/bin/bash: No such file or directory
Apr 02 14:54:42 regulus systemd[1]: test_systemd_timers.service: Main process exited, code=exited, status=127/n/a
░░ Subject: Unit process exited
░░ Defined-By: systemd
░░ Support: https://lists.freedesktop.org/mailman/listinfo/systemd-devel
░░ 
░░ An ExecStart= process belonging to unit test_systemd_timers.service has exited.
░░ 
░░ The process' exit code is 'exited' and its exit status is 127.
Apr 02 14:54:42 regulus systemd[1]: test_systemd_timers.service: Failed with result 'exit-code'.
░░ Subject: Unit failed
░░ Defined-By: systemd
░░ Support: https://lists.freedesktop.org/mailman/listinfo/systemd-devel
░░ 
░░ The unit test_systemd_timers.service has entered the 'failed' state with result 'exit-code'.
Apr 02 14:54:42 regulus systemd[1]: Failed to start test_systemd_timers.service.
░░ Subject: A start job for unit test_systemd_timers.service has failed
░░ Defined-By: systemd
░░ Support: https://lists.freedesktop.org/mailman/listinfo/systemd-devel
░░ 
░░ A start job for unit test_systemd_timers.service has finished with a failure.
░░ 
░░ The job identifier is 34454 and the job result is failed.
Apr 02 14:54:42 regulus sudo[5731]: pam_unix(sudo:session): session closed for user root

所以因为我在日志中得到了Will start scraping now.,所以我认为作业已开始,但无法运行脚本。我的问题是:

  • 我应该把我的脚本放在哪里(以及拥有哪些权限)?
  • 我应该如何添加我的配置以使脚本按照我的描述运行?

(当然,我很感谢对我的方法的每一个反馈。)

【问题讨论】:

    标签: web-scraping timer systemd nixos


    【解决方案1】:

    您的问题是,在您的脚本中,{pkgs.bash} 应该是 ${pkgs.bash};没有$,您将无法获得变量插值。

    【讨论】:

    • 你说得对,我忘了。输出改变了。非常感谢。
    • 对于那些追随我的人:实际有用的错误消息在第一个命令的输出中,但到目前为止它没有显示在我的 80 字符终端上。
    【解决方案2】:

    在 NixOS 上,systemd 服务在非常小的默认环境下运行。您可以在systemd.services.<name>.path中添加缺少的包

        systemd.services.test_systemd_timers = {
          serviceConfig.Type = "oneshot";
          path = [
            pkgs.wget
            pkgs.gawk
            pkgs.jq
          ];
          script = ''
            echo "Will start scraping now."
            ${pkgs.bash}/bin/bash /home/kaligule/scrape.sh
            echo "Done scraping."
          '';
        };
    

    【讨论】:

      猜你喜欢
      • 2018-06-05
      • 1970-01-01
      • 2019-11-07
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2020-10-11
      • 1970-01-01
      • 2018-01-23
      相关资源
      最近更新 更多