Urgent Help Needed with Script

Urgent Help Needed with Script

Post by Aaron Cra » Sun, 28 Feb 1999 04:00:00





> I need to write a shell that can return the name if it's parent
> process. In other words, I have a process named parent_sh. parent_sh calls
> a script child_sh.  I want child_sh to be able to print to the screen "I
> was called by parent_sh".

Do you really need this?  If you describe the problem you're trying to solve
in more detail, perhaps someone will be able to suggest a technique that
avoids this requirement.

Quote:> My initial thought was to go with ps and an awk script, but ps deals more
> with the command running from the script, rather than the script name.

I don't know what you mean by this last bit (from `but' onwards); it seems
to me that your idea of using ps is a pretty good one.  ps is (essentially)
the only way to translate a process id into a process name in a shell script
-- and it's certainly the most portable, though the code typically needs to
be tweaked to work on some systems.

POSIX sh and ksh provide the $PPID variable which contains the process id of
the parent process; you can parse the output of ps (carefully -- the number
may crop up elsewhere in the output) to find the line with that pid and the
name of the process it corresponds to.  One other thing: be aware that the
parent might have died by the time you run ps.

--

 ** Please send on-topic followups by Usenet, not email **

 
 
 

Urgent Help Needed with Script

Post by Aaron Cra » Tue, 02 Mar 1999 04:00:00








> > > I need to write a shell that can return the name if it's parent
> > > process. In other words, I have a process named parent_sh. parent_sh
> > > calls a script child_sh.  I want child_sh to be able to print to the
> > > screen "I was called by parent_sh".

> > Do you really need this?  If you describe the problem you're trying to
> > solve in more detail, perhaps someone will be able to suggest a
> > technique that avoids this requirement.

> That's pretty close to what I need. I'm writing a generic pager program,
> that can work within any parent shell, taking as little parameters as
> possible. My script is called within the error handling section to page
> the Sys Op. I want to avoid passing the name of the parent as a parameter
> to child_sh.

I'm sorry -- I'm really not understanding this.  What do you mean by `a
generic pager program that can work within any parent shell'?  By `pager
program', are you talking about programs such as less, more, and pg?  If so,
how could one of those not work with any parent shell?  I also don't
understand `called within the error handling section to page the Sys Op'.

Quote:> PS lists the command running from within the script, not the script name.
> Example:  Write a shell named test_sh. The only line within test_sh is
> 'sleep 1000'. Now execute test_sh, and put it in the background (suspend it).
> Now run ps.  When I run ps, it returns the following:

>    PID    TTY  TIME CMD
>  45830 pts/13  0:00 ps
>  56836 pts/13  0:00 sleep 1000
>  63094 pts/13  0:00 -ksh
>  63490 pts/13  0:00 -ksh

> Notice that it shows "sleep 1000", NOT test_sh. That's what I mean by the
> command name rather than the script name. If you know a ps option that would
> display "test_sh" rather than "sleep 1000", my problem is solved.

Ah, I see.  The problem is that the kernel is actually running the shell,
with the script as one of its args.  You can supply the `-f' option to ps to
get a full listing format.  This format includes the arguments to each
command, among which will be the name of the script.

Quote:> The idea of using the $PPID is a great one! Now if only I could figure out
> how to get ps to display the parent process name!

Just use the output of ps for that pid, and a little shell hackery will then
get you the command name:

    ps -p $PPID | sed -e 1d -e "s/^ *//" | tr -s " " " " | cut -d " " -f 4

Of course, this could still be a shell rather than a script.

--

 ** Please send on-topic followups by Usenet, not email **