Automatically Unmount All Removable Devices At Each Logout

Write tutorials here
There are more tutorials here http://community.linuxmint.com/tutorial/welcome
Forum rules
Please don't add support questions to tutorials,start your own thread in the appropriate sub-forum instead. Before you post please read this
Post Reply
User avatar
sebastjava
Level 2
Level 2
Posts: 74
Joined: Wed Jun 28, 2017 8:01 pm
Location: Montréal
Contact:

Automatically Unmount All Removable Devices At Each Logout

Post by sebastjava » Sun May 27, 2018 11:23 am

Could not display /media/_ANOTHER_USER_/_DEVICE.
Do you get this message? That is what happens every time you try to open a removable media device if that device was already mounted for another user. You must enter your password to use your administrator privileges to unmount that device and then mount it again, but this time in your actual session. It is not a big deal but it is not pretty nice. And it gets cumbersome when you have to do it many times every day.

Automatically Unmount All Removable Devices At Each Logout
There is a nice little script to automatically unmount all removable devices at each logout. These removable devices are still being mounted automatically at each login. So they are always available to every user, as long as they have the required permissions, of course. I tested it on my computer with an extra hard drive partition being mounted as well as a USB key and a SDHC card. When i logout to let another user login, the two removable(s) are automatically unmounted from the first session and they get mounted again for the new session. And the hard drive partition remains untouched. I think that is exactly how it should be for everyone. And that is exactly what this little script will do if you install it.

This script is reprinted below but it was originally found there:
https://ask.fedoraproject.org/en/questi ... at-logout/
It was written by: https://ask.fedorapr ... 046/hello/

Code: Select all

#!/bin/bash

for device in /sys/block/*
do
    if udevadm info --query=property --path=$device | grep -q ^ID_BUS=usb
    then
        echo Found $device to unmount
        DEVTO=`echo $device|awk -F"/" 'NF>1{print $NF}'`
        echo `df -h|grep "$(ls /dev/$DEVTO*)"|awk '{print $1}'` is the exact device
        UM=`df -h|grep "$(ls /dev/$DEVTO*)"|awk '{print $1}'`
        if sudo umount $UM
          then echo Done umounting
        fi
    fi
done
It comes from Fedora but it works perfectly on my Linux Mint Mate 18.3. I first tested it by pasting and running it directly into my Terminal. There were no error messages. The only differences from Fedora is about where to put your script and how to call it. Here on Linux Mint and Ubuntu, it goes this way:
  1. "Select All" and copy this script.
  2. Paste it into a text editor like Xed or Sublime or another.
  3. Save it in plain text format and name it "unmountonlogout". (without any extension)
  4. Open your /usr/bin/ folder as administrator and place a copy of your script in it.
  5. Open Terminal (or also known as command line) by pressing together Ctrl-Alt-T or by finding it in dash.
  6. Run this command:
    sudo xed /etc/lightdm/lightdm.conf
    (Replace "xed" with your text editor's real name, like "gedit". This opens the text editor with lightdm.conf file located in /etc/lightdm folder with administrator privileges.)
  7. In the file you should have this line: [SeatDefaults] or this line: [Seat:*]. Bellow this line, enter:
    session-cleanup-script=/usr/bin/unmountonlogout
    Note: make sure your script is set to executable by running:
    sudo chmod +x /usr/bin/unmountonlogout
  8. Save and exit.
  9. Restart your computer. (Because lightdm must be restarted.)
Part of these instructions about where to put your script and how to call it are taken from there:
https://askubuntu.com/questions/624374/ ... -at-logout

Post Reply

Return to “Tutorials”