The macosxhints Forums

The macosxhints Forums (http://hintsforums.macworld.com/index.php)
-   UNIX - Newcomers (http://hintsforums.macworld.com/forumdisplay.php?f=15)
-   -   Baby beginner question about "w" command (http://hintsforums.macworld.com/showthread.php?t=4627)

vickishome 08-10-2002 12:02 AM

Baby beginner question about "w" command
 
My Unix books arrived! :D And I'm beginning my first baby steps into learning Unix. Which means I have a really baby beginner's question. Actually, I have two questions.

Why am I showing up 3 times in Unix?

And why does WHAT not show what I'm doing?

Code:

[10:48pm] [vicki] [~] You rang? % who
vicki    console  Aug  7 23:30
vicki    ttyp1    Aug  9 14:00
vicki    ttyp2    Aug  9 22:47

[10:48pm] [vicki] [~] You rang? % w
10:50PM  up 1 day, 23:20, 3 users, load averages: 0.06, 0.00, 0.00
USER    TTY FROM              LOGIN@  IDLE WHAT
vicki    co -                Wed11PM  1day -
vicki    p1 -                2:00PM    0 -
vicki    p2 -                10:47PM    2 -
[10:50pm] [vicki] [~] You rang? %

Oh, and another question. How does the console fit into Unix? I thought the terminal was the shell into Unix and the console was just some kind of app that displayed error logs when needed (hmm... so is the console always up and running? What calls it to spring into action to display the error logs when a crash occurs?).

Thanks for humoring me with my silly beginner's Unix questions. I gotta start somewhere. :)

vickishome 08-10-2002 12:07 AM

While I'm at it (asking baby Unix questions)... What happens if I enter the exit command? I invisioned all my apps closing down immediately, but then does the terminal stay up? Do I have to reboot? Silly question, I know, but I'd like to know what it would do before I try it.

Never mind this one... I gave it a try. It just closes the window! So much for thinking my who system would shut down. :D

But it didn't remove me from the list when I use the w command. I'm still showing up 3 times.

hombre 08-10-2002 02:36 AM

You show up for each instance of the (umm... well, I guess it would be the...) shell (or something like that). So, "console" is from when you logged in, ttyp1 is when you opened Terminal.app, and ttyp2 is from when you opened a new terminal window.

I don't know what WHAT is.

The application console that displays logs is different from the above console, as you seem to have discerned. Perhaps the latter is the equivalent of what you get when you log in as >console. Sort of.

If you issue the exit command, we all die.

Actually, it just terminates the process of your terminal window (or something). So it is like closing the window, except the window is not closed. You know what I mean. [Oh. You already figured that out.]

Maybe someone else knows what I mean.

hombre 08-10-2002 02:43 AM

I think, in fact, most of what I said above is off. As you point out, there is not one less of you, if, with multiple terminal windows open, you issue an exit command, or, for that matter, close a window. One does definitely multiply oneself by opening new windows, though.

mervTormel 08-10-2002 03:09 AM

the issue with w and who is that there is a bug. exiting a shell doesn't decrement the login file utmp or wtmp, i forget. it's a bug. shoud be corrected in jag, but don't rely on current incarnations of w and who.

as for what, what what does your book refer too? always refer to local
% man command as for what is what

takayuki 08-10-2002 04:57 AM

Vicki,

What books did you get? I'm about to get some unix/os x books myself and since you seem to do things quite thoroughly and thoughtfully, maybe I'll forgo the whole amazon.com research thing and just get what you got. Yeah, I'm lazy.

I read in TidBits that Mac OS X Unleashed was worth a read and covered the terminal, etc. That was my research.

Hombre,

"If you issue the exit command, we all die." Boy, that unix is some powerfull stuff.

takayuki

bassi 08-10-2002 05:05 AM

MacOSX Unleashed.......
 
.....is a very good book for neophytes, like ourselves. It has a great reference section for the Unix commands, and goes through alot of useful tips and tricks with a firm basis. It does hold your hand at litle then lets you go your own way.

vickishome 08-10-2002 07:34 AM

Quote:

Originally posted by hombre
If you issue the exit command, we all die.
Oh my! I guess that means you guys have to be nice to me now 'cuz I have the power! :D

Merv - So it's a bug. I was thinking maybe I was there twice, one for user and one for admin? I'm still not sure what console is all about. Maybe it will gel in my brain as I learn more?

Takayuki - I was lazy too! :D I took the recommendations that were in the OSXGuide. The book I'm going through first is Sams Teach Yourself Unix in 24 hours (Sams Publishing). I also bought Unix in a Nutshell (O'Reilly & Associates). I haven't gone through the books far enough to make any real evaluation on them, but I have always found the books by O'Reily to be very good. I bought Unix in a Nutshell as a reference book. Sams Teach Yourself Unix in 24 Hours seems - so far - to be exactly what I need as a baby beginner. It completely holds your hand. But what I like even better is that it is helping me to understand better what I'm doing. Anyone can throw a command at you, and you can type it into the terminal to see it work. But for me to truly learn it, I need to understand why I'm using a particular command, what my goal is, and why that command will accomplish that goal. So far, Sams Teach Yourself in 24 Hours appears to give me that necessary understanding. I'll be able to offer a better opinion on it as I read more of it.

Regarding WHAT is WHAT... I apologize for not being more clear. When I enter the w command, it's supposed to tell me a number of things, including WHAT each person is doing. The book says it's supposed to be showing that I'm doing the w command. But it's not. It's just showing a - (dash) under each of my 3 listings. So I was wondering why it's not telling me what I'm doing as it was supposed to do. Maybe another bug?
Quote:

[6:19am] [vicki] [~] You rang? % w
6:19AM up 2 days, 6:49, 3 users, load averages: 0.20, 0.05, 0.01
USER TTY FROM LOGIN@ IDLE WHAT
vicki co - Wed11PM 2days -
vicki p1 - 6:19AM 0 -
vicki p2 - 1:22AM 4:56 -
According to the book, this line should have given me this:
Quote:

vicki p1 - 6:19AM 0 w
So I can't use the man pages to tell me what is what. But I do see that Unix finally answers the age old question of who am I. We all die with exit, and I finally find out who am I. I can feel the power. :D

occjph 08-10-2002 11:26 AM

w is broken in OSX
 
The 'w' command is supposed to show you what what each user is doing on the machine, such as this output from my Sun workstation:

$ w
7:04am up 113 day(s), 20:45, 2 users, load average: 0.45, 0.43, 0.43
User tty login@ idle JCPU PCPU what
john pts/5 7:03am 1 find / -name core -ls
john pts/7 7:04am w
$


I am logged into two sessions, and each has it's own shell (in the case of Sun, a Bourne shell (/bin/sh), in the case of MacOSX a tcsh shell. I am running the 'find' command in one shell, and the 'w' command in the other shell.

In order to see what programs you ARE running in MacOSX, you can use the 'ps' (process list) command:

ps -ao user,command

USER COMMAND
john -tcsh (tcsh)
root ps -ao user command

We see User 'john' running a shell, and user 'root' running the command that generated the above output. There are many other options to the ps command - I suggest you try reading the man pages for ps (man ps) and play with it a bit.

Each instance of a 'terminal' window in MacOSX spawns a 'shell' for you to work in. The shell is associated with the Terminal program, thus when you quit the terminal (or close the terminal window), the shell is killed as well.

The 'console' is a special form of terminal. 'Console' in unixland is a device that programmers use to dump output from programs. Usually, output is directed to a log file, or the syslog daemon, but it can be directed to both a log file AND the console device. It's a quick way to see various status messages from programs running on your machine. The Console app has options to view other log files as well.

When you enter the 'exit' command you are telling the current instance of the terminal app to terminate. Any programs started under that instance of terminal app will also terminate. An 'instance' in this case does not refer to the terminal app itself, just the WINDOW in which you entered the command. If you have 10 windows open at once, and type 'exit' in one window, just that window will close - the other 9 will remain open.

vickishome 08-10-2002 12:08 PM

Quote:

w is broken in OSX.
That explains why WHAT isn't working. The ps command works great in its place.

Quote:

Each instance of a 'terminal' window in MacOSX spawns a 'shell' for you to work in. The shell is associated with the Terminal program, thus when you quit the terminal (or close the terminal window), the shell is killed as well.
But that's not what's happening for me.

For example, I did a restart and immediately opened the terminal. Issuing a w command showed 2 lines - 1 console and 1 user (me, vicki). Cool. So I got rid of that second user line that I had before.

So I opened a 2nd Terminal window. As expected, it created another user entry showing me (vicki) twice.

However, issuing the exit command in that second window did not remove the second listing when I reissued the w command. In fact, after opening 5 new terminal windows -- and closing each by issuing the exit command -- I am now listed 6 times (one for each of 5 shells, 4 of which are closed now, and 1 for the console). The only way I have found so far to remove myself from those 6 listings is to do a restart of my Mac.

Is this how it is supposed to work or is this merely a result of w being broken in OS X?

Not that any of this is truly important to me (right now), but I would like to understand what's happening. If it's due to a bug, then so be it. But if it's due to something I'm doing, then I'd like to correct my mistake.

So my question is... If I open 5 shell windows in Terminal, how do I remove myself from being listed 6 times when I issue the w command (other than doing a restart)? Is it possible? Or is this just part of the "w is broken in OS X" bug?

mervTormel 08-10-2002 12:30 PM

i thought we covered this...

"the issue with w and who is that there is a bug. exiting a shell doesn't decrement the login file /var/log/utmp or /var/log/wtmp, i forget. it's a bug. should be corrected in jag, but don't rely on current incarnations of w and who."
Quote:

But that's not what's happening for me.
it is happening, if you consult ps. w and who are lying.

as for the exit command, it merely quits the current shell level, returning to its caller. e.g., my prompt contains the value of the environment variable SHLVL>1, which is incremented for subshells and script invocations...
Code:

$ bash  # start a bash subshell

(2)$ tcsh  # start a tcsh subshell

(3)% exit
exit

(2)$ exit
exit

$


vickishome 08-10-2002 12:57 PM

Quote:

Originally posted by mervTormel
i thought we covered this...
Yes, so you did. I didn't understand. <sigh> I am trying to learn. I really want to learn Unix. I know I can learn Unix. But so much is being thrust upon me all at once (by my own doing) that I'm not comprehending everything on the first go around. Please bare with me. You're welcome to get the hammer out and use it on my head when my head has ceased to function.

Quote:

it is happening, if you consult ps. w and who are lying.
I get it now.

Quote:

e.g., my prompt contains the value of the environment variable SHLVL>1, which is incremented for subshells and script invocations...

$ bash # start a bash subshell

(2)$ tcsh # start a tcsh subshell

(3)% exit
exit

(2)$ exit
exit

$

You just flew completely over my head with this.

nkuvu 08-10-2002 02:50 PM

When you start up a Terminal window, what you are really doing is running a program, in this case tcsh. Now in that same window, if you type 'tcsh' again, you start a new process. But if you haven't changed your prompt, it looks exactly the same as it did before.

The way to tell the difference is with ps. If I enter tcsh, my ps looks like:
Code:

1459 std  Ss    0:00.14 -tcsh (tcsh)
 1481 std  R      0:00.14 -csh (tcsh)

If I type exit, it changes back to:
Code:

1459 std  Ss    0:00.14 -tcsh (tcsh)
You can type tcsh over and over and over, and each one starts a sub process. It's pretty easy to get lost -- "if I type exit now, do I lose the window?". So Merv set it up so he knows how deep into this he is. Most people who mess around with the Terminal won't need to do this. (everyone I've ever seen just opens a new window when they want another process)

Does that make a little more sense?

Also, part of the confusion when you ask about WHAT is that 'what' is a Unix command by itself... :)

mervTormel 08-10-2002 03:26 PM

thanks, nkuvu.

Quote:

You just flew completely over my head with this.
to demonstrate what the exit command really does, i needed to introduce subshell and shell level. you needn't grok that fully, but have an idea that, for example, script A can invoke script B which can invoke script C and that each invocation is an instance of a new level of the environment, like adding to a stack of plates. the exit command removes a plate and smashes it on the floor. exit can also return a value to the caller, indicating exit status.

get used to perusing the man pages and searching for keywords with the /pattern search operator, e.g.,
Code:

% man tcsh
...
/exit # <- you enter this to search for occurrences of 'exit'

# then hit the 'n' key to repeat the search (next occurrence),
# hit 'h' for help

see subshell - man tcsh doesn't cover subshell very well, so off to other sources
see environment variable SHLVL
Code:

% man tcsh
...
/shlvl # <- you enter this
...skipping...
shlvl (+)
              The number of nested shells.  Reset to 1 in  login
              shells.  See also loginsh
...

i realize this seems rather Byzantine at first, but as you exercise it, it becomes less. and less is more...

just to throw a wrench into this, set your pager to less (instead of more) to get a richer man page environment. put the following commands in your tcsh shell startup ~./login ...

setenv PAGER less
setenv LESS -FiMs~

as an exercise, find what less options FiMs~ means in the man pages for less

i promise, vicki, it will get easier and there will be a light at the end of the tunnel (that isn't an oncoming train if you exercise caution).

vickishome 08-11-2002 10:04 AM

Thank you! Yes, I understood the "stacked plate" concept as presented. In my mind, I relate this to routines and subroutines, including the passed exit values.

I did not understand how/where I am to enter /exit or /shlvl. However, I gather you're wanting me to do a page search on these "words" on the man pages for tsch. If so, I'm just using the Find Panel under the Edit menu.

I also have ManOpen, and can use the GUI Find to search through a displayed man page, as well.

Attempts to enter /exit or /shlvl in the terminal prompt failed (and methinks if you intended for me to enter those as commands, you would have prefaced them with % so I don't believe that's what you had in mind).

Whatever the method, I did search, find and read about exit in tsch. And it made sense. At least I think it did. In my own words, opening a new window in the terminal is simply starting a new process.

What I may be wrong about, and need clarification, is a subprocess versus an unrelated process (sorry, I don't know the proper words to use here). If I issue a command in a tcsh which invokes a subprocess, the the subprocess is a "related" process (another plate stacked on top). When that subprocess exits (successfully/unsuccessfully), it returns an exit value back to the process that originally invoked it. However, if I open two windows in the terminal, each are independant of each other (two separate stacks of plates). If one exits, it does not return any value to the other because they have no relation to each other. Is that correct? Did I make any sense in how I've said that?

Is there anything simple I can do at the terminal that will cause a second terminal window to open (a "related" process) -- and then run something in that second terminal window that will force it to exit (and close the window) which I can then test for the exit value in the first window/process? Again, I'm not sure if I'm making any sense here without knowing the proper terminology.
Quote:

put the following commands in your tcsh shell startup ~./login
I'm confused here. I looked for a file in ~ named .login or ./login (hidden file - didn't understand the "/" part at all), but I can't find one. % locate login and % locate .login found numerous files. % locate ./login found 0 files. Is this a file I am to create? If so, are you saying the filename would be ./login in my ~ dir (/Users/vicki/)?

I could not find any reference to FiMs~, even when looking at the man pages for less. I tried % apropos FiMs in the termanal and using ManOpen, but still came up empty handed.
Quote:

i promise, vicki, it will get easier and there will be a light at the end of the tunnel
I'm okay with that and I know I will learn Unix. I was once not only a programmer, but a very good one at that. I used to give presentations at user groups and had people (professional programmers!) come to me to debug their code and offer suggestions on how to accomplish things. I can do this. I have the mind to do this. But it's been one and a half decades since I've done anything like this (html code does even come close), and I'm extremely frustrated that I am making so many dumb mistakes and asking so many dumb questions, and I'm bugging you guys with it all. I hate going through the learning curve. I want to be "there" already.

mervTormel 08-11-2002 12:34 PM

load up the plate!

1. the pager program (more or less) is interactive. the "prompt" at the bottom reads commands. one command is 'h' for help. hit h and in more, you'll see a half screen of commands and one is /<regular expression - search... in less, you'll see a full screen of commands and under searching you'll see /pattern

meaning... enter the / char and the prompt starts to read your input, enter what to search for...
Code:

% man tcsh
...
/shlvl  # <-you type the blue stuff in the man pager
...

you should get to some occurrence of "shlvl", or that it wasn't found

2. regarding processes vs. subprocesses, you are correct. two terminal app windows are unrelated, except that they are owned by you and are subprocesses (children) of the terminal app process. in the ps long listing, PPID means parent process ID and if you follow the chain of command, all PIDs lead to parent PID 1, the init process...
Code:

$ ps al | colrm 30 60
  UID  PID  PPID CPU PRI NI        TIME COMMAND
  501  403  372  0  31  0    0:00.54 -bash (bash)
  501  507  372  0  31  0    0:00.43 -bash (bash)
  501  559  507  0  31  0    0:00.43 bash
    0  599  559  0  31  0    0:00.01 ps al
  501  600  559  0  31  0    0:00.00 colrm 30 60

$ ps wwlp 372 | colrm 30 60
  UID  PID  PPID CPU PRI NI        TIME COMMAND
  501  372    68  0  63  0    0:15.53 \
    /Applications/Utilities/Terminal.app/Contents/MacOS/Terminal...

$ ps wwlp 68 | colrm 30 60
  UID  PID  PPID CPU PRI NI        TIME COMMAND
  501    68    1  0  63  0    5:49.34 \
    /System/Library/CoreServices/WindowServer

$ ps wwlp 1 | colrm 30 60
  UID  PID  PPID CPU PRI NI        TIME COMMAND
    0    1    0  0  31  0    0:00.00 /sbin/init   



# PPID 0 ? not a process, but the kernel.
# colrm used to shorten lines

3.
Quote:

Is there anything simple I can do at the terminal that will cause a second terminal window to open (a "related" process) -- and then run something in that second terminal window that will force it to exit (and close the window) which I can then test for the exit value in the first window/process?
no, nothing simple. and it's not a very reasonable goal at this juncture. baby steps.

4. if you don't have ~/.login, just create it. as you get further into the shell, you'll be wanting to set up your shell startup environment by populating the shell startup scripts with your commands. ~/.login is one of those shell startup scripts. see man <your shell> and search for "startup"

5. less options. invoke man less and in the Options section, you'll see that it explains how you can set the env variable LESS to the less startup options. the options of FiMs~ are enumerated here (there) ; each char of FiMs~ is a seperate -switch so you won't find the string "FiMs~" but you will find the separate switches -F -i -M -s -~

vickishome 08-11-2002 01:30 PM

Uncle (I cry). I'm lost. I'm getting about 1/2 of what you're saying, but I'm stuck on the ./login file, and I can't go any further until I create it.

I tried to create it using BBEdit. It won't allow me to use the "/" character in the filename. Fine.

So I used pico. It seemed happy with naming the file ./login, but when I try ls -a, no such file/dir is listed.

I'm in my ~ dir (/Users/vicki/). That's where I thought I was saving the file.

Please throw me a clue. I can't proceed until I create the ./login file you recommended.

mervTormel 08-11-2002 01:39 PM

regrets.

./login is a typo

the file name is .login

it resides in ~/

it's full reference is ~/.login - not ./login

in a short time, you'll be able to spot and correct these little foibles.
Code:

$ ll ~/.login
-rw------- 1 merv wheel 21 Aug 11 10:35 /Users/merv/.login

pico is a user friendly text editor. try...
Code:

% cd  # cd to home

% pico .login
...

more...

./login refers to the filename login
Code:

$ touch ./login

$ ll ./login login
-rw-------    1 merv    wheel          0 Aug 11 10:47 ./login
-rw-------    1 merv    wheel          0 Aug 11 10:47 login

same file, different reference

vickishome 08-11-2002 01:45 PM

Ah! (light just switched on). So what I did was create .login in my / dir. Got it. Moved it. Now I can start chewing on the rest of your info.

And if you want a laugh... it took me about 30 minutes before I figured out what the "pager program" was. :D

Baby steps, indeed. :)

mervTormel 08-11-2002 01:58 PM

yeah, this is the point where a lot of people give up and go shoot skeet.

your tenacity will pay off; there'll soon be enough lights going off to be seen from space.

at an old job, the pager program was the annoying collection of code that dialed my beeper every time so much as a gnat farted near the machine room. so, it is with some disgust and revilement that i refer to the "file perusal filter for crt viewing" as a pager, but it is the argot of the day, and we must attempt to approach convention.

nkuvu 08-11-2002 02:06 PM

Hint on the FiMs~

This is a collection of options, not a single option. So I sincerely doubt you'd be able to find anything by looking for the full word.

:)

vickishome 08-11-2002 02:51 PM

Farting gnat! LOL! :D

No skeet shooting over here. I may feel like shooting the terminal, shooting my books and shooting my kids if they won't stop fighting while I'm trying to digest all of this (just kidding on the kids, of course), but no skeet shooting. Believe it or not, I'm actually enjoying this no matter how much hair I might end up with in the end. :)

1. I'm getting "pattern not found" with /shlvl. But if I do a "Find" in the terminal, it's there. And if I do a "Find" while in ManOpen, it's there too. My logic is now on tilt. :confused:

2. I follow the logic (so I think). I follow the numbers. I have no clue what you've displayed there. I've seen that info before (or something similar), but am not comprehending it. But it's the example that's important, not the details of the example, right?

3. Okee-dokee. There's always another day.

4. Done. :)

5. Gotta problem. In the current shell I'm in (ttyp1), % man tcsh spits out lots of cool info. But I opened a new shell (new window - ttyp2), and suddenly % man tcsh isn't too happy (methinks the setenv commands are in affect in the new shell):
Code:

[1:16pm] [vicki] [~] Exit, and we all die: % man tcsh
man: Formatting manual page...
There is no -F option ("less --help" for help)

And so, of course, % less --help only serves to spit out the same error message and is rendered useless.

However, I do still have ttyp1 open. FiMs~ means (-i) ignore case (-M) long prompt (more verbose) (-s) squeeze blank lines and (~) make lines at EOF blank.

I ran across "F" without a dash (scroll forward), making me think that's what was intended, but I don't know why I'm having problems with -FiMs~. I tried F -iMs~, but, of course, that produces an error.

mervTormel 08-11-2002 03:32 PM

regrets. my less is fink's less which is more than OSX's less.

re: 1 & 5. with fink's less (same author, newer version) the -F switch...
Quote:

-F or --quit-if-one-screen
Causes less to automatically exit if the entire
file can be displayed on the first screen.
Code:

$ which less
/sw/bin/less

$ less -V
less 358
Copyright (C) 2000 Mark Nudelman
...

$ whereis less
/usr/bin/less

$ /usr/bin/less -V  # here, env var LESS=-F breaks /usr/bin/less
There is no -F option ("less --help" for help)

$ echo $LESS
-FiMs~

$ LESS=-iMs~  # fix env var LESS, remove the F

$ /usr/bin/less -V
less 340
Copyright (C) 1999 Mark Nudelman
...

since your env variable LESS has -F in it, it gaks /usr/bin/less and breaks all the rest of the switch values, which means you don't get case-blind searches (-i) and /shlvl will fail to find anything.

/SHLVL would find it, right?

so, remove the F from the LESS varliable and restart your shells (to re-initialize your shell environment). grokking now?

re: 2. consult % man ps

get familiar with ps output - ps wwaxl - all processes have a process ID (PID) and a parent PID (PPID)

my example merely climbed up the parent tree from a bash shell process, whose parent is terminal whose parent is windowserver whose parent is init whose parent is kernel.


re: fink vs. vanilla OSX, i will try to remain OSX vanilla with commands so as not to stuff up these issues.

vickishome 08-11-2002 06:43 PM

It must have been all of those farting gnats that made ya do it. :p

And good news. By golly, I think I understood all of your last message! :D

While I had the F flag in my env for less, neither /shlvl or /SHLVL would work. I errored out before I even got the chance to try.

Now that I have it corrected, both work as advertised. :)

I've just gone back to read this thread again, and I'm beginning to grok it. :D In the processes you were showing me, the relationships are kernal to WindowServer to Terminal to whatever bash is (a process you're running). Yeah, I know you spelled it out for me. I wanted to spell it myself. ;) So if I want to see what's called what, I start by looking up the PPID for the process to see who's its parent. I can look up that PPID to find its parent. And so on and so forth until I finally reach the kernal.

AND this means I can look to see if a process I'm running has called another process (related) or is just another process running along side of it (unrelated). Right?

BTW, nkuvu, I'm not ignoring you. I appreciate your help and have been reading and learning from your help as well. Thanks. :)


All times are GMT -5. The time now is 10:31 PM.

Powered by vBulletin® Version 3.8.7
Copyright ©2000 - 2014, vBulletin Solutions, Inc.
Site design © IDG Consumer & SMB; individuals retain copyright of their postings
but consent to the possible use of their material in other areas of IDG Consumer & SMB.