How to mount/unmount SATA hotplug drives with one mouse click / script?

Thanks to this post I found out how to enable a SATA hotplug device. I have prepared a deskotp shortcut with Exec=sudo scsiadd -a 4 00 00 00 and added the drive to automount options.

With this shortcut it would be nice not to be asked for the password (it only runs with sudo).

In order to unmount and turn off the drive two commands are necessary (and again I need to enter the password):

sudo umount '/run/media/rh/SSD2'
sudo scsiadd -r 4 00 00 00

For multiple commands a shortcut doesn’t work, so a script is needed. And that’s where I am struggling to find a solution. What I have already tried after researching:

#!/bin/bash
commands () 
{
echo "mypassword" | sudo -S scsiadd -a 4 00 00 00 # supposed to execute as root for which the password is automatically filled – this works if I enter it manually in the terminal.

$SHELL # keep the terminal open after previous commands are executed so that I see errors in case they happen
}
export -f commands
konsole "bash -c 'commands'"

What happens: Nothing (the HD stays off), and he terminal window stays open without any messages.

What goes wrong, which other ways can be recommended? Would you advise against saving my password in the script? I am Linux beginner and looking forward to learn here!

Probably the simplest solution is add to sudoers entry which allow executing command without password, example for mount:

myprecioususer  ALL= NOPASSWD: /usr/bin/mount

Thanks, I will do that :slight_smile: Do you also know how to execute the two commands in one step?