An fuser trick I just picked up

Why did I not know this years ago. Thanks to Ralph Grothe via the ITRC forums.

I am currently working on a script and wanted to add a feature that checks if a mount point is in use before attempting a umount of the file system. Simple enough. Well I use perl all the time and I was kind of struggling with dealing with output from fuser. If you have run it before, you know the output usually looks like this:

fuser -c /home
/home: 20692c 12198c 12027c 21902c 110c 28692c 28979c 16873co 20281c 9209c 21157c 29603c 28789c 8219co 21680c 29439c 1441co 5962co 1293c 21736c 22109c 21820c 22177c 709c 28905c 13004c 14258c

So, I was capturing this data into an array and thought that I would just test to see if the array had 2 or more elements. Which worked fine. But the command would also spit the fuser output on the screen. So after googling a bit. I found the answer. Ralph Grothe posted it in the the ITRC post linked above. Just do this:

fuser -c /home 2>/dev/null
20692 12198 12027 21902 110 28692 3617 28979 16873 20281 9209 21157 29603 28789 8219 21680 1441 5962 1293 21736 22109 21820 22177 709 28905 13004 14258

Notice the difference. Now I was able to use an if statement that just tested if there were any elements in the array. It's also cleaner becasue the array only contains PIDs at this point.

Comments