我已经设置了一个 systemd 服务来基于此在 Linux 上运行完美代理文章,但找不到 dbt 命令。有谁知道如何让服务捕获 dbt 命令?
我使用的是 GCP 虚拟机、Ubuntu、20.04 LTS、amd64
这是服务状态:
prefect-agent.service - Prefect Agent
Loaded: loaded (/etc/systemd/system/prefect-agent.service; disabled; vendor preset: enabled)
Active: active (running) since Tue 2023-03-28 02:03:07 UTC; 1min 53s ago
Main PID: 140053 (prefect)
Tasks: 3 (limit: 19187)
Memory: 139.6M
CGroup: /system.slice/prefect-agent.service
└─140053 /home/ulan/anaconda3/envs/zoomcamp/bin/python /home/ulan/an>
Mar 28 02:03:55 de-zoomcamp prefect[140053]: File "/home/ulan/anaconda3/envs/zoomcamp/lib/python3.9/site-packages/prefect/engine.py", line 1438, in orchestrate_t_task_run
Mar 28 02:03:55 de-zoomcamp prefect[140053]: result = await task.fn(*args, **kwargs)
Mar 28 02:03:55 de-zoomcamp prefect[140053]: File "/home/ulan/anaconda3/envs/zoomcamp/lib/python3.9/site-packages/prefect_dbt/cli/commands.py", line 154, in trigger_dbt_cli_command
Mar 28 02:03:55 de-zoomcamp prefect[140053]: result = await shell_run_command.fn(command=command, **shell_run_command_kwargs)
Mar 28 02:03:55 de-zoomcamp prefect[140053]: File "/home/ulan/anaconda3/envs/zoomcamp/lib/python3.9/site-packages/prefect_shell/commands.py", line 114, in shell_run_command
Mar 28 02:03:55 de-zoomcamp prefect[140053]: raise RuntimeError(msg)
Mar 28 02:03:55 de-zoomcamp prefect[140053]: RuntimeError: Command failed with exit code 127:
Mar 28 02:03:55 de-zoomcamp prefect[140053]: /tmp/prefect-655rr1g5: line 1: dbt: command not found
Mar 28 02:03:56 de-zoomcamp prefect[140053]: 02:03:56.020 | ERROR | Flow run 'graceful-dolphin' - Finished in state Failed('Flow run encountered an exception. RuntimeError: Command failed with exit code 127:\n/tmp/prefect-655rr1g5: line 1: dbt: command not found\n\n')
Mar 28 02:03:56 de-zoomcamp prefect[140053]: 02:03:56.702 | INFO | prefect.infrastructure.process - Process 'graceful-dolphin' exited cleanly.
我的服务代码:
[Unit]
Description=Prefect Agent
[Service]
User=ulan
WorkingDirectory=/home/ulan/DE_zoomcamp_project/dbt/who_disease_data
ExecStart=/home/ulan/anaconda3/envs/zoomcamp/bin/prefect agent start -q "default"
Restart=always
[Install]
WantedBy=multi-user.target
我已经在本地安装了 dbt-bigquery,当我直接在终端中命令 prefect agent start -q "default" 时它就可以工作。
答案1
当任何进程尝试运行未指定完整(或相对)路径的命令时,它将在环境变量中存在的每个文件夹中查找可执行文件是否存在$PATH
。这就是你可以运行的方式,例如,ls
,而不是/bin/ls
- 因为/bin
存在于你的PATH
变量中。
dbt
不在任何标准路径中 - 它安装在 中/home/ulan/anaconda3/envs/zoomcamp/bin
,因此如果此文件夹不在PATH
服务的环境变量中,prefect
代理将不知道在哪里找到它。
[Service]
在文件的部分下,添加:
Environment="PATH=/bin:/usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin:/home/ulan/anaconda3/envs/zoomcamp/bin"
然后,当代理查找dbt
命令时,它将在变量中的所有文件夹中查找(按指定的顺序),并最终在列表中的最后一个文件夹(可执行文件所在的文件夹)中找到它。