How to 'write' to a user, lgged from 'su'?

I have a situation, where some script (running background) need to use the 'write' command and get a text to appeared on any screen of some particular user.
Like:

>echo "$message"|write user1

But I surprisingly realized, that if a person logged under any other name and after that uses the 'su' command to log in as the 'user1', he will not be reachable by the 'write' command!

Is there any other way to send a message to anyone who currently logged as 'user1', even by 'su'?

Edit user1's $HOME/.login

What do you mean, glen.barber?
What and with what intention anything should be edited?

You can have a message displayed at login by editing $HOME/.login For instructions, read the login manpage for your particular OS.

As far as writing to a user's screen from a script, I'm not aware of a way to do that. You can send that particular user mail output in your script.

Hi,
The write program writes text to a users tty. That is what the OP was talking about.
Yo should use the who command to figure out what terminal/xterm/whatever tty the user is using. The root user more often than not has messaging turned off by default. Use the mesg command to configure those rules.

And have a look at "man write" and "man mesg" :slight_smile:

/Lakris

EDIT:
sorry, I guess I got carried away... I usually do an "ssh -X user@localhost" because I'm lazy and want X-forwarding, and then I always get a clean and obvious login. su'd users don't show up in who. So, I would find a way , with for example ps axjf to find the user You want to reach, find the tty, and find the owner of that tty...

this is very crude but I guess You get the idea...

lakris@landet:~$ ps axjf|grep 'su - grok'|grep -v grep
 7277  7300  7300  7277 pts/0     7306 S     1001   0:00      \_ su - grok
lakris@landet:~$ ps axu |grep 7277|grep -v grep
lakris    7277  0.0  0.3   5824  3200 pts/0    Ss   23:32   0:00 bash
lakris@landet:~$ write lakris pts/0
df sdf g
^Clakris@landet:~$ 

and in my other xterm it shows up as

lakris@landet:~$ su - grok
Password: 
grok@landet:~$ 
Message from lakris@landet on pts/2 at 23:44 ...
df sdf g
EOF

Hope it helps!

/L

It is not a solution to my situation - to have anything appeares on user's loggin!
First - I need it displayed right away (assuming, someone use that log name - user1 - in every given point of time)
Second - the 'user1' is not logging on - someone uses 'su' to use that account for some tasks!
The 'write' command perfectly works out what I need - it is showing a message on first found screen (tty) for a requested user, BUT it is fooled by the 'su' loggin.

Whell, it is a production control task that should inform about itself in some condition.
And you can guess, in different time different people performe 'production control' duties.
I do not need to have it write for a particular individual, but to an anyone who currently use the production control loggin 'user1'.

Than why the 'mailx' also is not comfortable.

Is there any idias for that task?

Lakris - thank you, now I've get it. I have to work through terminal and an actual user's id.
And thank you for example. I will try.

Glen, appreciate your responce, also!

Great, glad to help.
And You can refine Your script by checking for return values of the "ps axfj" just to see if there is a grok (or user1) su'd to write to and take proper action.

Good luck!

/Lakris

I am still not able to resolve this issue.
I am on SunOS 5.6 (I guess the "ps axfj" is on Linux) and 'ps' do not shows a tree, where I can find the 'su' usage (or I did not find a way; so, advise will be appreciated.)

Is there, on Sun UNIX, a way to figured it out who was used the 'su'?
I see the 'su' is logget in one special file - /var/adm/sulog, but it owned by 'root' and has only owner 'resd' permission.
Is there any way or utilite to read it or just to get who is logged bu 'su' command by any other way?

Ok, for one thing You can have a look at the non-BSD style of options for ps, so ps axfj would amount to ps -ejH, or something.

And I had a look at my box (yes, it's GNU/Linux) and it logs su actions in /var/log/auth.log. Maybe You have something similar.

Example, if I su to grok this will appear in the file:
Feb 4 00:42:01 landet su[6742]: + pts/1 lakris:grok
there can of course be several such lines, figure out which one is current.
And to write to the user su'd in as grok, use

write lakris pts/1

I guess it would work.

/Lakris

Appreciate your responce and effort, but does not help.
I see the same file, but permition!

-rw-------   1 root     sys      1575952 Feb  4 09:28 auth.log

(About ps -ejH - if it is specific, in our system the H is illegal option.)

Ok, well, if You can't read the log files, I guess You will just have to go back to examining output from ps, use man ps and google to find how You can extract info about who su'd to a specific user (parameters to ps) , info on parent process and who owns it.

/L