1

I'm trying to remove all files in a specific directory using systemd at reboot.

Here is my /lib/systemd/system/delete-at-boot.service file (root, access 644):

[Unit]
Description=Remove content of test folder at boot

[Service]
ExecStart=/bin/rm /home/user/test/*

[Install]
WantedBy=multi-user.target

When I run /bin/rm /home/user/test/* manually the deletion works fine but when I start the process, I can see my files are not deleted.

● 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'.

I believe the problem is related to the "*" But I'm not sure what is the best way to replace it.

2

Wildcards are not supposed to be used like that in a systemd service. change your ExecStart to:

ExecStart=/bin/sh -c '/bin/rm /home/user/test/*'

Now it should perform the action correctly.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.