How do I auto-run a script when I change User?

theGreaterDuck

New Member
Joined
Oct 25, 2020
Messages
11
Reaction score
1
Credits
109
Mint 20.04, Ryzen 5-4500:

Hi, I am developing (Lazarus-FPC) some bespoke internal+external email-software for my Son's business and I need to isolate it from the Internet while testing. I have created a Standard-User "Test" to work there and I need it to crash at "no Internet" to handle errors etc.

Currently I am manually turning all-Network off/on, but that gets a little tedious as I have to do other daily-work on the Admin side many times a day.

No matter what I set within User Test, unless it is all-off, it still gets access to the Network and Internet.

Can someone please point me at, or help with a script that will auto-run when I TTY to Test (TTY2) that will disable the Network?

Then, when I TTY back to Admin (TTY1) another script auto-runs to reinstate the Network for my normal use?

Thanks
 


Condobloke

Well-Known Member
Joined
Apr 30, 2017
Messages
3,761
Reaction score
3,369
Credits
18,550

KGIII

Super Moderator
Staff member
Gold Supporter
Joined
Jul 23, 2020
Messages
4,064
Reaction score
3,739
Credits
32,636
Take a look at this answer:

 

f33dm3bits

Gold Member
Gold Supporter
Joined
Dec 11, 2019
Messages
3,911
Reaction score
2,648
Credits
28,263
You could write a script that checks which checks which TTY you are using and depending on which TTY number it gets back that it executes a certain command. Just something quick, haven't tried it but just off the top of my head.
Bash:
if [[  $XDG_VTNR = 2 ]] && [[ $UID = "testuser" ]]
then
    echo "Disabling networking"
    nmcli connection down eth0

elif [[ $XDG_VTNR = 1 ]] && [[ $UID = "f33dm3bits" ]]
then
   echo "Enabling networking"
   nmcli connection up eth0
fi
Then add to the ~/.bashrc or ~/.bash_profile of both of your users(test user and normal user) and it will be executed each time you login, if this doesn't work you will at least have something to help you get started. When you are talking about TTY I assume you are using ctrl+alt+f{1-12} to access the console to then login as the one of the two users.

If not and your are logging into the system using the display manager than you could just create a file with the contents of what I posted ealier to /usr/local/bin/checkusr
Bash:
#!/bin/bash

if [[  $XDG_VTNR = 2 ]] && [[ $UID = "testuser" ]]
then
    echo "Disabling networking"
    nmcli connection down eth0

elif [[ $XDG_VTNR = 1 ]] && [[ $UID = "f33dm3bits" ]]
then
   echo "Enabling networking"
   nmcli connection up eth0
fi
Make it executable(chmod +x /usr/local/bin/checkusr) and then you could add the command to your startup applications so that it is launched when you login, that would would then look like this.
Code:
/usr/local/bin/checkusr &
 
Last edited:

stan

Well-Known Member
Joined
Mar 19, 2018
Messages
985
Reaction score
1,105
Credits
9,112
I don't understand how that offers help.
@theGreaterDuck: What @Condobloke did by using the @ symbol with those users is call on them to come and help you. The forum software sends a notification that "you have been mentioned in a post" when this is done. And this was soon followed by 2 of the 3 people called upon arriving and offering you advice. People here are in time zones all over the world... be patient while waiting on replies.

If your edited "p.s." non-Einstein quote is meant as a disrespectful commentary... you may find people will stop helping you. Your lack of understanding should not give you cause to be insulting.
 

wizardfromoz

Administrator
Staff member
Gold Supporter
Joined
Apr 30, 2017
Messages
6,642
Reaction score
5,404
Credits
19,758
Agreed.

Moving this to Command Line, where scripting inquiries are also handled.

Chris Turner
wizardfromoz
 

Condobloke

Well-Known Member
Joined
Apr 30, 2017
Messages
3,761
Reaction score
3,369
Credits
18,550
@theGreaterDuck ....you silence confirms (perhaps) your comment was meant as disrespectful commentary.
Your continued absence sets it in concrete.

What say you ?
 

dcbrown73

Well-Known Member
Joined
Jul 14, 2021
Messages
361
Reaction score
329
Credits
3,199
Add it to your user profile.
 
$100 Digital Ocean Credit
Get a free VM to test out Linux!

Members online


Top