I wasn't trying to do anything apart from wondering why the developer had used
exec something &
instead of
something &
and why it effectively makes no difference.
Main Topics
Browse All TopicsOne of my developers came up with some unusual code that had me wondering what was going on.
See the below scripts. In particular, it's the line
exec /tmp/b &
that has me wondering the detailed process flow. In essence the exec makes no difference, but the question is why? Is it because a new process is forked and *then* that process is overlayed by exec?
#!/bin/sh
date
echo "A $$"
ps -fp $$
exec /tmp/b &
PID=$!
echo "PID $!"
wait $PID
echo "A END $$"
date
$ cat b
#!/bin/sh
echo "B $$"
ps -fp $$
sleep 5
echo "B END"
$ /tmp/a
Tue Sep 26 14:28:53 NZST 2006
A 9849
UID PID PPID C STIME TTY TIME CMD
tintin 9849 7976 0 14:28:53 pts/5 0:00 /bin/sh /tmp/a
PID 9852
B 9852
UID PID PPID C STIME TTY TIME CMD
tintin 9852 9849 0 14:28:53 pts/5 0:00 /bin/sh /tmp/b
B END
A END 9849
Tue Sep 26 14:28:58 NZST 2006
If you remove the exec and run again, you'll see that it has exactly the same behaviour.
This Question has been solved and asker verified All Experts Exchange premium technology solutions are available to subscription members.
Experts Exchange has been collecting answers to technology questions since 1996…3 million and counting! If you have a question, chances are we already have your answer.
If you can't find the exact answer you're looking for, ask our exclusive community of 50,000 experts. You’ll get a personalized answer from a trusted professional.
Thousands of free tech tips, tricks, how-to’s and tutorials are available in our peer reviewed articles section. See for yourself how smart our experts are, no login required.
Access the answers to your technology questions today.
30-day free trial. Register in 60 seconds.
Members of the expert community talk about why the experience at Experts Exchange is different than what you will find anywhere else.

Try it out and discover for yourself.
30-day free trial. Register in 60 seconds.
Join the community of experts here and help other tech pros by answering question in your area of expertise. You can earn FREE access to all Experts Exchange's premium features and resources.
The exec command specified by the arguments is executed in place of this shell without creating a new process.
In code above, It didn't run at the same shell because b script has "#!/bin/sh" which make exec useless
remove "#!/bin/sh" from the top of b script and you will see the difference
$ a_pid.sh
Wed Sep 27 12:43:45 EDT 2006
A 26196
UID PID PPID C STIME TTY TIME CMD
hhassan1 26196 14562 0 12:43:45 pts/116 0:00 /bin/sh a_pid.sh
PID 26199
B 26199
UID PID PPID C STIME TTY TIME CMD
hhassan1 26199 26196 0 12:43:46 pts/116 0:00 /bin/sh a_pid.sh
B END
A END 26196
Wed Sep 27 12:43:51 EDT 2006
as you see both PS CMD output have "/bin/sh a_pid.sh"
Removing #!/bin/sh from the exec script makes no difference on Solaris 10.
What Unix version are you running HamdyHassan?
Anyway, I think your analysis is incorrect. It's not the hash bang line that makes a difference to exec (at least on Solaris 10 with bourne shell), it's the execing a process in the background.
both Solaris 7 and Solaris 8 give the same difference
Here is output at Solaris 8
$ a_pid.sh
Thu Sep 28 10:01:46 EDT 2006
A 21010
UID PID PPID C STIME TTY TIME CMD
hhassan1 21010 20977 0 10:01:46 pts/8 0:00 /bin/sh a_pid.sh
PID 21013
B 21013
UID PID PPID C STIME TTY TIME CMD
hhassan1 21013 21010 0 10:01:46 pts/8 0:00 /bin/sh a_pid.sh
B END
A END 21010
Thu Sep 28 10:01:51 EDT 2006
$ uname -r
5.8
>>>>>>>>>>>> makes no difference on Solaris 10.
Please check Solaris 10 documents , may be they mentioned bug/enhancement about it
My mistake. Behaviour is the same on Solaris 10.
Looks like my initial idea is correct.
exec cmd &
Appears to do fork, then the forked process is exec'ed. If the exec'ed process is invoking another interpreter, then that interpreter will be used, otherwise exec will overlay the forked process with the current shell process (still end up with 2 processes though).
I'll give the points to HamdyHassan, as he pointed out the difference in running the second script with and without the #!/bin/sh
Business Accounts
Answer for Membership
by: ozoPosted on 2006-09-25 at 19:55:43ID: 17598335
Yes.
Were you trying to do something like
(/tmp/b &) &