Welcome to the most active Linux Forum on the web.
Go Back > Forums > Linux Forums > Linux - Desktop
User Name
Linux - Desktop This forum is for the discussion of all Linux Software used in a desktop context.


  Search this Thread
Old 01-19-2008, 11:21 AM   #1
LQ Newbie
Registered: Jan 2008
Posts: 18

Rep: Reputation: 0
Using exec in a shell script

First, let me ask: Is there a more appropriate place to be asking
newbie shell script questions?

From a Google search on using exec:

The exec ( exec [ argument ] ) command reads in the command specified by arguments and executes them in place of this shell without creating a new process. Input an output arguments may appear and, if no others are given, will cause the shell input and or output to be modified.

When try this in a shell script the second command (rm) will never execute

exec /usr/local/bin/streamripper -l 30 -c -D $HOME/test

rm $HOME/CBC-Radio/World-Report/incomplete/start

If you remove the exec command the script moves on to the second command
after completing the first.

Could someone tell me why this functionality would be required in a shell
Old 01-19-2008, 11:47 AM   #2
Registered: Oct 2007
Location: Norway
Distribution: Slackware 14.1
Posts: 446

Rep: Reputation: 59
Well, its kind of hard to explain. Lets take it from the start. When your computer first boots, it starts of one process. A mother process of all other processes. This is called init. When you want to start a new application it needs a way to do that. For this purpose there are two systemcalls. The first call is "fork". This forks a new process. It does so by copy the existing PD (process descriptor), and gives it a new PID (Process ID). But since the process descriptor is the same, and the same program code is used, it is at this point rather useless. Now we use the next systemcall. Exec.

What exec does, it replace the program code of the existing program (But keeps the rest of the PD intact), and that way the new application will start. As you can see, when you combine these two you start a new application. This is what normally happen when you start a new program. Fork and exec. Because of this, when you start a new program it looks like the program is running inside bash, but it is actually a new process with a new PID. If you on the other hand start a program with exec, it replaces the programcode with your new program and that way a new program will inherit the PID. I use this sometimes when I ssh to another computer to start X. Then I start a failsafe X-server on my local machine, ssh to the other computer, and exec fluxbox


Thread Tools Search this Thread
Search this Thread:

Advanced Search

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off

Similar Threads
Thread Thread Starter Forum Replies Last Post
exec the expect command in a shell script wanghao Linux - Networking 6 11-23-2007 11:06 PM
practical uses for exec in shell dazdaz Linux - Software 1 08-24-2007 02:40 PM
Assistance with 'find -exec cp' shell script dick.swift Linux - Software 6 01-23-2006 10:00 AM
shell script problem, want to use shell script auto update IP~! singying304 Programming 4 11-29-2005 05:32 PM
exec a C prg in a CGI Perl script jagman Linux - Networking 2 05-31-2005 10:40 AM > Forums > Linux Forums > Linux - Desktop

All times are GMT -5. The time now is 03:17 AM.

Main Menu
Write for LQ is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
Facebook: linuxquestions Google+: linuxquestions
Open Source Consulting | Domain Registration