我正在尝试在重启时使用 systemd 删除特定目录中的所有文件。
这是我的/lib/systemd/system/delete-at-boot.service
文件(root,访问权644):
[Unit]
Description=Remove content of test folder at boot
[Service]
ExecStart=/bin/rm /home/user/test/*
[Install]
WantedBy=multi-user.target
当我/bin/rm /home/user/test/*
手动运行时,删除工作正常,但是当我启动该过程时,我可以看到我的文件没有被删除。
● delete-at-boot.service - Remove content of test folder at boot
Loaded: loaded (/lib/systemd/system/delete-at-boot.service; enabled; vendor preset: enabled)
Active: failed (Result: exit-code) since Fri 2019-08-02 18:21:09 UTC; 7s ago
Process: 5460 ExecStart=/bin/rm /home/user/test/* (code=exited, status=1/FAILURE)
Main PID: 5460 (code=exited, status=1/FAILURE)
Aug 02 18:21:09 tegra-ubuntu systemd[1]: Started Remove content of test folder at boot.
Aug 02 18:21:09 tegra-ubuntu rm[5460]: /bin/rm: cannot remove '/home/user/test/*': No such file or directory
Aug 02 18:21:09 tegra-ubuntu systemd[1]: delete-at-boot.service: Main process exited, code=exited, status=1/FAILURE
Aug 02 18:21:09 tegra-ubuntu systemd[1]: delete-at-boot.service: Unit entered failed state.
Aug 02 18:21:09 tegra-ubuntu systemd[1]: delete-at-boot.service: Failed with result 'exit-code'.
我认为问题在于“*”但我不确定替代它的最佳方法是什么。
答案1
通配符不应该在服务中这样使用systemd
。将您的更改ExecStart
为:
ExecStart=/bin/sh -c '/bin/rm /home/user/test/*'
现在它应该可以正确执行操作了。