views:

80

answers:

2

I have a custom shell script that runs each time a user logs in or identity is assumed, its been placed in /etc/profile.d and performs some basic env variable operations. Recently I added some code so that if screen is running it will reattach it without needing me to type anything. There are some problems however. If I log-in as root, and su - to another user, the code runs a second time. Is there a variable I can set when the code runs the first time that will prevent a second run of the code?

I thought to write something to the disk but then I dont want to prevent the code from running if I begin a new terminal session. Here is the code in question. It first attempts to reattach - if unsuccessful because its already attached (as it might be on an interruped session) it will 'take' the session back.

screen -r

if [ -z "$STY" ]; then
    exec screen -dR
fi

Ultimately this bug prevents me from substituting user to another user because as soon as I do so, it grabs the screen session and puts me right back where I started. Pretty frustrating

A: 

I think you can get this right if you read the following post (and the man for your favorite shell) Question about login vs profile

Rickard von Essen
Well if I understand this article correctly then I am doing exactly what I should be doing (yet the bug persists). I added the system wide script to the /etc/profile.d - and it shouldnt run when I substitute user which should be on the level of a 'bashrc' action, if I am not mistaken .. maybe I am
Matt1776
+1  A: 

The ${PPID} of the shell you get when you su will be the su command. So the output of

ps -o command= $PPID

will begin with the letters su, so test for this.

Charles Stewart
THANK YOU - this is exactly what i needed
Matt1776