![Systemd 服务创建符号链接](https://linux22.com/image/1026119/Systemd%20%E6%9C%8D%E5%8A%A1%E5%88%9B%E5%BB%BA%E7%AC%A6%E5%8F%B7%E9%93%BE%E6%8E%A5.png)
我希望 Systemd 管理符号链接的存在。符号链接应存在于/var/run/mysqld/mysqld.sock
并指向/var/run/proxysql/mysqld.sock
。原因显而易见/有争议(我不会深入讨论)。让我们忽略这些细节,专注于所请求的解决方案。
到目前为止我已经创建了这个单元文件/usr/lib/systemd/system/mysqldsocket.service
:
[Unit]
Description=Create a symlink to the ProxySQL MySQL UNIX socket
After=network.target
[Service]
Type=oneshot
RuntimeDirectory=mysqld
RuntimeDirectoryMode=0775
ExecStart=/usr/bin/ln -s /var/run/proxysql/mysqld.sock /var/run/mysqld/mysqld.sock
ExecStop=rm -f /var/run/mysqld/mysqld.sock
RemainAfterExit=yes
[Install]
WantedBy=multi-user.target
运行systemctl start mysqldsocket.service
确实会创建符号链接并将systemctl stop mysqldsocket.service
其删除。但是 systemd 不知道该文件是否真的存在,systemctl status mysqldsocket.service
并不总是与实际情况相符。
# systemctl start mysqldsocket.service
# rm /var/run/mysqld/mysqld.sock
# systemctl status mysqldsocket.service
● mysqldsocket.service - Create a symlink to the ProxySQL MySQL UNIX socket
Loaded: loaded (/lib/systemd/system/mysqldsocket.service; disabled; vendor preset: enabled)
Active: active (exited) since Sun 2022-04-03 22:21:46 UTC; 14s ago
Process: 750220 ExecStart=/usr/bin/ln -s /var/run/proxysql/mysqld.sock /var/run/mysqld/mysqld.sock (code=exited, status=0/SUCCE>
Main PID: 750220 (code=exited, status=0/SUCCESS)
我可以添加一些内容来/usr/lib/systemd/system/mysqldsocket.service
告诉 systemd 验证符号链接确实存在并据此报告正确的状态吗?