Download as pdf or txt
Download as pdf or txt
You are on page 1of 2

process - Why is pstree showing the name of my...

http://unix.stackexchange.com/questions/32341...

sign up

log in

tour

Here's how it works:

Unix & Linux Stack Exchange is a


question and answer site for users of
Linux, FreeBSD and other Un*x-like
operating systems. Join them; it only
takes a minute:

Anybody can ask


a question

Sign up

help

Anybody can
answer

The best answers are voted


up and rise to the top

Why is pstree showing the name of my script instead of the name of the shell that is interpreting it?
I have written a simple shell script as follows:
#!/bin/bash
sleep 90

After running this shell I run pstree in a separate shell to see the process tree structure.
Here is what I see
-gnome-terminal-+-bash---sleepy.sh---sleep

I was expecting this to be like


-gnome-terminal-+-bash---bash---sleep

Why is a shell script being represented as a process by pstree? The ps command correctly shows the command being executed as
10150

8771

0 08:13 pts/1

00:00:00 /bin/bash ./sleepy.sh

Here the process is bash and sleepy.sh is its argument (this makes sense to me). In my view a process has to be an Executable Linkable Format
binary (ELF). Bash is an ELF executable but a shell script is not and hence I think pstree should not be showing it as such?
/ shell

/ process

edited 12 hours ago

Arthur2e5
878

asked 14 hours ago

sshekhar1980
19

16

Because it's a process. Ipor Sircer 14 hours ago


That's not what the questioner is asking, Ipor Sircer. The title is badly phrased. It would be better to read something
along the lines of Why is pstree showing the name of my script instead of the name of the shell that is interpreting it?
JdeBP 14 hours ago
Did you run this script by name, or did you run this script using 'bash sleepy.sh'? Andy Dalton 13 hours ago

1 Answer

pstree retrieves the process name from /proc/<pid>/stat . This is whatever was given to the
kernel via execve(2) 's first parameter; see proc(5) and What exactly happens when I execute
a file in my shell? for details. You'll see from the latter that the kernel can run shell scripts directly
(and many other "binaries" see How is Mono magical?), but the shell also steps in in some
cases.

Thus, if you run


./sleepy.sh

with a shebang line at the start of the script, you'll see sleepy.sh in pstree 's output because
that's what the shell asks the kernel to run. If instead you run
sh ./sleepy.sh

you'll see sh in pstree 's output.


ps -f or ps u (and pstree -a ) read /proc/<pid>/cmdline instead to retrieve the command
line, which is different it's the argv parameter given to the execve system call. When
running a shell script with a shebang, this is changed to include the shebang, which is no doubt
why in your case ps shows
/bin/bash ./sleepy.sh

(see How programs get run for lots more on this).


edited 10 hours ago

1 of 2

answered 11 hours ago

11/16/2016 04:53 AM

process - Why is pstree showing the name of my...

http://unix.stackexchange.com/questions/32341...

Stephen Kitt
46.8k

2 of 2

74

102

11/16/2016 04:53 AM

You might also like