我到处寻找,只是想弄清楚为什么 systemctl 无法启动我的设备。
我正在尝试将 GCP 的 cloud-sql-proxy 作为 systemd 服务启动。这就是我的样子cloud-sql-proxy.service
。
[Install]
WantedBy=multi-user.target
[Unit]
Description=Cloud SQL Proxy
Requires=networking.service
After=networking.service
[Service]
Type=simple
WorkingDirectory=/opt
ExecStart=/opt/cloud_sql_proxy -instances=pupa=tcp:5432 -credential_file=/etc/pupa-240309-5336639b0c06.json
Restart=always
StandardOutput=journal
User=root
我sudo systemctl enable cloud-sql-proxy
成功运行了。然后当我执行sudo systemctl start cloud-sql-proxy.service
它时失败并显示以下消息:
Failed to start cloud-sql-proxy.service: Unit not found.
以下是更多信息:
$ ll -al /etc/systemd/system/cloud-sql-proxy.service
-rw-r--r--. 1 root root 327 May 22 10:56 /etc/systemd/system/cloud-sql-proxy.service
$ sudo systemctl list-unit-files
...
cloud-sql-proxy.service enabled
...
$ systemctl status cloud-sql-proxy
● cloud-sql-proxy.service - Cloud SQL Proxy
Loaded: loaded (/etc/systemd/system/cloud-sql-proxy.service; enabled; vendor preset: disabled)
Active: failed (Result: resources) since Sun 2019-05-19 15:49:48 UTC; 3 days ago
Main PID: 1414 (code=exited, status=1/FAILURE)
May 19 15:49:48 instance-1 systemd[1]: cloud-sql-proxy.service: main process exited, code=exited, status=1/FAILURE
May 19 15:49:48 instance-1 systemd[1]: Unit cloud-sql-proxy.service entered failed state.
May 19 15:49:48 instance-1 systemd[1]: cloud-sql-proxy.service failed.
May 19 15:49:48 instance-1 systemd[1]: cloud-sql-proxy.service holdoff time over, scheduling restart.
May 19 15:49:48 instance-1 systemd[1]: cloud-sql-proxy.service failed to schedule restart job: Unit not found.
May 19 15:49:48 instance-1 systemd[1]: Unit cloud-sql-proxy.service entered failed state.
May 19 15:49:48 instance-1 systemd[1]: cloud-sql-proxy.service failed.
对我遗漏的地方有什么建议吗?谢谢!
答案1
您可以在 /usr/lib/systemd/system/cloud_sql_proxy.service 中尝试类似这样的操作吗?
[Unit]
Description=GCP CloudSQL Proxy
After=network.target
[Service]
User=root
Group=root
WorkingDirectory=/usr/bin
Type=forking
RemainAfterExit=yes
ExecStart=/bin/sh -c '/usr/bin/nohup /usr/local/cloud_sql_proxy -instances=${INSTANCE_CONNECTION_NAME} -credential_file=${CREDENTIAL_FILE} &'
StandardOutput=journal
KillMode=process
[Install]
WantedBy=multi-user.target
您的配置文件将是 /usr/lib/systemd/system/cloud_sql_proxy.service.d/settings.conf
[Service]
Environment=INSTANCE_CONNECTION_NAME=[YOUR CONNECTION NAME]
Environment=CREDENTIAL_FILE=[PATH TO YOUR CREDENTIAL FILE]
答案2
根据我的经验,这是由于Requires
未找到其中一项服务。如果您能够启用服务但启动服务时返回,Unit not found
则检查 下的服务Requires
。就我而言,我有一个,Requires: rpcbind.service
但该服务未安装在我的系统上。
答案3
我觉得你正在关注这个[1]链接,但如果你看底部,他们指定如果你想在 Centos 7 上执行相同操作,则需要使用不同的文件。也许这就是它失败的原因。
对于 Centos 7,使用以下命令:
[Install]
WantedBy=multi-user.target
[Unit]
Description=Google Cloud Compute Engine SQL Proxy
Requires=network.target
After=network.target
[Service]
Type=simple
WorkingDirectory=/usr/local/bin
ExecStart=/usr/local/bin/cloud_sql_proxy -dir=/var/run/cloud-sql-proxy -instances=<instance_connection_name>=tcp:3306 -credential_file=/var/local/cloud-sql-proxy/<credential_json>.json
Restart=always
StandardOutput=journal
[1]https://gist.github.com/goodwill/a981c2912ae6a83761a624f657f34d9f