I have a folder /mnt/automounts which is used to automount usb drives in (which is working.). There is one scenario that still causes an issue. When a usb drive is plugged in, then the system is shut down and afterwards I unplug the usb drive. When i start up the system again the specific folder of the mount is still present in mnt/automounts.

For this reason I would like to start a script at shutdown (and reboot) that removes the automounts folder. to do this I've created a shell script called clearmnt.sh and put it in the /etc/init.d folder>

clearmnt.sh looks like this:



# Provides: clearmnt

# Required-Start:

# Required-Stop:

# Should-Stop:
# Default-Start: 0 6

# Default-Stop:
# Short-Description: removes directories of automounted folders in /mnt


rm -rf /mnt/automounts

Then I've created soft links in rc0.d and rc6.d (shutdown and reboot):

sudo ln -s /etc/init.d/clearmnt.sh /etc/rc0.d/clear_mnt

sudo ln -s /etc/init.d/clearmnt.sh /etc/rc6.d/clear_mnt

And then I made clearmnt.sh executable:

sudo chmod +x /etc/init.d/clearmnt.sh

Then I did a reboot to see if it works. It did not, the /mnt/automounts folder is still there after reboot.

Any idea what I'm doing wrong, or what step I am missing? This is all done on a yocto build.

  • You want to rm -rf /mnt/automounts/*
    – waltinator
    Mar 4, 2021 at 16:57
  • That did also not remove the automounts folder here. Really looks like the script does not get executed Mar 5, 2021 at 6:50

1 Answer 1


Are you sure you really want this? Imagine the situation you described. You have an USB storage plugged in, and you shut down your system. Your shutdown script executes rm -rf /mnt/automounts (or rm -rf /mnt/automounts/*, doesn't matter).

Later you reboot and find out that all the files on your USB storage are deleted.

So what you should do is delete the directories after the reboot, when the USB storage is not mounted.

Or even easier, create a small tmpfs that is kept in RAM, you don't need much space because it contains just the directories for the mount points, and it is automatically empty when you boot.

  • You are right, I like the the small tmpfs kept in RAM idea. Thanx Mar 5, 2021 at 12:41

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .