LinuxReviws.org --get your your Linux knowledge
> Linux Reviews > Manual Pages (man) >

exec


  1. exec.1.man
  2. exec.3.man
  3. exec.9.man


1. exec.1.man

Invalid Manpage

Invalid Manpage

The requested file (stdin) is not a valid (unformatted) man page.

2. exec.3.man

Manpage of EXEC

EXEC

Section: Linux Programmer's Manual (3)
Updated: 2010-09-25
Index Return to Main Contents
 

NAME

execl, execlp, execle, execv, execvp, execvpe - execute a file  

SYNOPSIS

#include <unistd.h>

extern char **environ;

int execl(const char *path, const char *arg, ...);
int execlp(const char *file, const char *arg, ...);
int execle(const char *path, const char *arg,
..., char * const envp[]);
int execv(const char *path, char *const argv[]);
int execvp(const char *file, char *const argv[]);
int execvpe(const char *file, char *const argv[],
char *const envp[]);

Feature Test Macro Requirements for glibc (see feature_test_macros(7)):

execvpe(): _GNU_SOURCE  

DESCRIPTION

The exec() family of functions replaces the current process image with a new process image. The functions described in this manual page are front-ends for execve(2). (See the manual page for execve(2) for further details about the replacement of the current process image.)

The initial argument for these functions is the name of a file that is to be executed.

The const char *arg and subsequent ellipses in the execl(), execlp(), and execle() functions can be thought of as arg0, arg1, ..., argn. Together they describe a list of one or more pointers to null-terminated strings that represent the argument list available to the executed program. The first argument, by convention, should point to the filename associated with the file being executed. The list of arguments must be terminated by a NULL pointer, and, since these are variadic functions, this pointer must be cast (char *) NULL.

The execv(), execvp(), and execvpe() functions provide an array of pointers to null-terminated strings that represent the argument list available to the new program. The first argument, by convention, should point to the filename associated with the file being executed. The array of pointers must be terminated by a NULL pointer.

The execle() and execvpe() functions allow the caller to specify the environment of the executed program via the argument envp. The envp argument is an array of pointers to null-terminated strings and must be terminated by a NULL pointer. The other functions take the environment for the new process image from the external variable environ in the calling process.  

Special semantics for execlp() and execvp()

The execlp(), execvp(), and execvpe() functions duplicate the actions of the shell in searching for an executable file if the specified filename does not contain a slash (/) character. The file is sought in the colon-separated list of directory pathnames specified in the PATH environment variable. If this variable isn't defined, the path list defaults to the current directory followed by the list of directories returned by confstr(_CS_PATH). (This confstr(3) call typically returns the value "/bin:/usr/bin".)

If the specified filename includes a slash character, then PATH is ignored, and the file at the specified pathname is executed.

In addition, certain errors are treated specially.

If permission is denied for a file (the attempted execve(2) failed with the error EACCES), these functions will continue searching the rest of the search path. If no other file is found, however, they will return with errno set to EACCES.

If the header of a file isn't recognized (the attempted execve(2) failed with the error ENOEXEC), these functions will execute the shell (/bin/sh) with the path of the file as its first argument. (If this attempt fails, no further searching is done.)  

RETURN VALUE

The exec() functions only return if an error has have occurred. The return value is -1, and errno is set to indicate the error.  

ERRORS

All of these functions may fail and set errno for any of the errors specified for execve(2).  

VERSIONS

The execvpe() function first appeared in glibc 2.11.  

CONFORMING TO

POSIX.1-2001, POSIX.1-2008.

The execvpe() function is a GNU extension.  

NOTES

On some other systems, the default path (used when the environment does not contain the variable PATH) has the current working directory listed after /bin and /usr/bin, as an anti-Trojan-horse measure. Linux uses here the traditional "current directory first" default path.

The behavior of execlp() and execvp() when errors occur while attempting to execute the file is historic practice, but has not traditionally been documented and is not specified by the POSIX standard. BSD (and possibly other systems) do an automatic sleep and retry if ETXTBSY is encountered. Linux treats it as a hard error and returns immediately.

Traditionally, the functions execlp() and execvp() ignored all errors except for the ones described above and ENOMEM and E2BIG, upon which they returned. They now return if any error other than the ones described above occurs.  

SEE ALSO

sh(1), execve(2), fork(2), ptrace(2), fexecve(3), environ(7)  

COLOPHON

This page is part of release 3.32 of the Linux man-pages project. A description of the project, and information about reporting bugs, can be found at http://www.kernel.org/doc/man-pages/.


 

Index

NAME
SYNOPSIS
DESCRIPTION
Special semantics for execlp() and execvp()
RETURN VALUE
ERRORS
VERSIONS
CONFORMING TO
NOTES
SEE ALSO
COLOPHON

This document was created by man2html using the manual pages.
Time: 17:30:52 GMT, October 23, 2013

3. exec.9.man

Manpage of exec

exec

Section: Tcl Built-In Commands (n)
Updated: 8.5
Index Return to Main Contents



 

NAME

exec - Invoke subprocesses  

SYNOPSIS

exec ?switches? arg ?arg ...?



 

DESCRIPTION

This command treats its arguments as the specification of one or more subprocesses to execute. The arguments take the form of a standard shell pipeline where each arg becomes one word of a command, and each distinct command becomes a subprocess.

If the initial arguments to exec start with - then they are treated as command-line switches and are not part of the pipeline specification. The following switches are currently supported:

-ignorestderr
Stops the exec command from treating the output of messages to the pipeline's standard error channel as an error case.
-keepnewline
Retains a trailing newline in the pipeline's output. Normally a trailing newline will be deleted.
--
Marks the end of switches. The argument following this one will be treated as the first arg even if it starts with a -.

If an arg (or pair of args) has one of the forms described below then it is used by exec to control the flow of input and output among the subprocess(es). Such arguments will not be passed to the subprocess(es). In forms such as ``< fileName'', fileName may either be in a separate argument from ``<'' or in the same argument with no intervening space (i.e. ``<fileName'').

|
Separates distinct commands in the pipeline. The standard output of the preceding command will be piped into the standard input of the next command.
|&
Separates distinct commands in the pipeline. Both standard output and standard error of the preceding command will be piped into the standard input of the next command. This form of redirection overrides forms such as 2> and >&.
fileName
The file named by fileName is opened and used as the standard input for the first command in the pipeline.
<@ fileId
FileId must be the identifier for an open file, such as the return value from a previous call to open. It is used as the standard input for the first command in the pipeline. FileId must have been opened for reading.
<< value
Value is passed to the first command as its standard input.
fileName
Standard output from the last command is redirected to the file named fileName, overwriting its previous contents.
2> fileName
Standard error from all commands in the pipeline is redirected to the file named fileName, overwriting its previous contents.
>& fileName
Both standard output from the last command and standard error from all commands are redirected to the file named fileName, overwriting its previous contents.
>> fileName
Standard output from the last command is redirected to the file named fileName, appending to it rather than overwriting it.
2>> fileName
Standard error from all commands in the pipeline is redirected to the file named fileName, appending to it rather than overwriting it.
>>& fileName
Both standard output from the last command and standard error from all commands are redirected to the file named fileName, appending to it rather than overwriting it.
>@ fileId
FileId must be the identifier for an open file, such as the return value from a previous call to open. Standard output from the last command is redirected to fileId's file, which must have been opened for writing.
2>@ fileId
FileId must be the identifier for an open file, such as the return value from a previous call to open. Standard error from all commands in the pipeline is redirected to fileId's file. The file must have been opened for writing.
2>@1 
Standard error from all commands in the pipeline is redirected to the command result. This operator is only valid at the end of the command pipeline.
>&@ fileId
FileId must be the identifier for an open file, such as the return value from a previous call to open. Both standard output from the last command and standard error from all commands are redirected to fileId's file. The file must have been opened for writing.

If standard output has not been redirected then the exec command returns the standard output from the last command in the pipeline, unless ``2>@1'' was specified, in which case standard error is included as well. If any of the commands in the pipeline exit abnormally or are killed or suspended, then exec will return an error and the error message will include the pipeline's output followed by error messages describing the abnormal terminations; the -errorcode return option will contain additional information about the last abnormal termination encountered. If any of the commands writes to its standard error file and that standard error is not redirected and -ignorestderr is not specified, then exec will return an error; the error message will include the pipeline's standard output, followed by messages about abnormal terminations (if any), followed by the standard error output.

If the last character of the result or error message is a newline then that character is normally deleted from the result or error message. This is consistent with other Tcl return values, which do not normally end with newlines. However, if -keepnewline is specified then the trailing newline is retained.

If standard input is not redirected with ``<'', ``<<'' or ``<@'' then the standard input for the first command in the pipeline is taken from the application's current standard input.

If the last arg is ``&'' then the pipeline will be executed in background. In this case the exec command will return a list whose elements are the process identifiers for all of the subprocesses in the pipeline. The standard output from the last command in the pipeline will go to the application's standard output if it has not been redirected, and error output from all of the commands in the pipeline will go to the application's standard error file unless redirected.

The first word in each command is taken as the command name; tilde-substitution is performed on it, and if the result contains no slashes then the directories in the PATH environment variable are searched for an executable by the given name. If the name contains a slash then it must refer to an executable reachable from the current directory. No ``glob'' expansion or other shell-like substitutions are performed on the arguments to commands.  

PORTABILITY ISSUES

Windows (all versions)
Reading from or writing to a socket, using the ``fileId'' notation, does not work. When reading from a socket, a 16-bit DOS application will hang and a 32-bit application will return immediately with end-of-file. When either type of application writes to a socket, the information is instead sent to the console, if one is present, or is discarded.

The Tk console text widget does not provide real standard IO capabilities. Under Tk, when redirecting from standard input, all applications will see an immediate end-of-file; information redirected to standard output or standard error will be discarded.

Either forward or backward slashes are accepted as path separators for arguments to Tcl commands. When executing an application, the path name specified for the application may also contain forward or backward slashes as path separators. Bear in mind, however, that most Windows applications accept arguments with forward slashes only as option delimiters and backslashes only in paths. Any arguments to an application that specify a path name with forward slashes will not automatically be converted to use the backslash character. If an argument contains forward slashes as the path separator, it may or may not be recognized as a path name, depending on the program.

Additionally, when calling a 16-bit DOS or Windows 3.X application, all path names must use the short, cryptic, path format (e.g., using ``applba~1.def'' instead of ``applbakery.default''), which can be obtained with the ``file attributes fileName -shortname'' command.

Two or more forward or backward slashes in a row in a path refer to a network path. For example, a simple concatenation of the root directory c:/ with a subdirectory /windows/system will yield c://windows/system (two slashes together), which refers to the mount point called system on the machine called windows (and the c:/ is ignored), and is not equivalent to c:/windows/system, which describes a directory on the current computer. The file join command should be used to concatenate path components.

Note that there are two general types of Win32 console applications:

[1]
CLI --- CommandLine Interface, simple stdio exchange. netstat.exe for example.
[2]
TUI --- Textmode User Interface, any application that accesses the console API for doing such things as cursor movement, setting text color, detecting key presses and mouse movement, etc. An example would be telnet.exe from Windows 2000. These types of applications are not common in a windows environment, but do exist.

exec will not work well with TUI applications when a console is not present, as is done when launching applications under wish. It is desirable to have console applications hidden and detached. This is a designed-in limitation as exec wants to communicate over pipes. The Expect extension addresses this issue when communicating with a TUI application.

Windows NT
When attempting to execute an application, exec first searches for the name as it was specified. Then, in order, .com, .exe, and .bat are appended to the end of the specified name and it searches for the longer name. If a directory name was not specified as part of the application name, the following directories are automatically searched in order when attempting to locate the application:
*
The directory from which the Tcl executable was loaded.
*
The current directory.
*
The Windows NT 32-bit system directory.
*
The Windows NT 16-bit system directory.
*
The Windows NT home directory.
*
The directories listed in the path.

In order to execute shell built-in commands like dir and copy, the caller must prepend the desired command with ``cmd.exe /c '' because built-in commands are not implemented using executables.

Windows 9x
When attempting to execute an application, exec first searches for the name as it was specified. Then, in order, .com, .exe, and .bat are appended to the end of the specified name and it searches for the longer name. If a directory name was not specified as part of the application name, the following directories are automatically searched in order when attempting to locate the application:
*
The directory from which the Tcl executable was loaded.
*
The current directory.
*
The Windows 9x system directory.
*
The Windows 9x home directory.
*
The directories listed in the path.

In order to execute shell built-in commands like dir and copy, the caller must prepend the desired command with ``command.com /c '' because built-in commands are not implemented using executables.

Once a 16-bit DOS application has read standard input from a console and then quit, all subsequently run 16-bit DOS applications will see the standard input as already closed. 32-bit applications do not have this problem and will run correctly, even after a 16-bit DOS application thinks that standard input is closed. There is no known workaround for this bug at this time.

Redirection between the NUL: device and a 16-bit application does not always work. When redirecting from NUL:, some applications may hang, others will get an infinite stream of ``0x01'' bytes, and some will actually correctly get an immediate end-of-file; the behavior seems to depend upon something compiled into the application itself. When redirecting greater than 4K or so to NUL:, some applications will hang. The above problems do not happen with 32-bit applications.

All DOS 16-bit applications are run synchronously. All standard input from a pipe to a 16-bit DOS application is collected into a temporary file; the other end of the pipe must be closed before the 16-bit DOS application begins executing. All standard output or error from a 16-bit DOS application to a pipe is collected into temporary files; the application must terminate before the temporary files are redirected to the next stage of the pipeline. This is due to a workaround for a Windows 95 bug in the implementation of pipes, and is how the standard Windows 95 DOS shell handles pipes itself.

Certain applications, such as command.com, should not be executed interactively. Applications which directly access the console window, rather than reading from their standard input and writing to their standard output may fail, hang Tcl, or even hang the system if their own private console window is not available to them.

Unix       
The exec command is fully functional and works as described.
 

UNIX EXAMPLES

Here are some examples of the use of the exec command on Unix.

To execute a simple program and get its result:


exec uname -a

To execute a program that can return a non-zero result, you should wrap the call to exec in catch and check the contents of the -errorcode return option if you have an error:


set status 0
if {[catch {exec grep foo bar.txt} results options]} {
   set details [dict get $options -errorcode]
   if {[lindex $details 0] eq "CHILDSTATUS"} {
      set status [lindex $details 2]
   } else {
      # Some kind of unexpected failure
   }
}

When translating a command from a Unix shell invocation, care should be taken over the fact that single quote characters have no special significance to Tcl. Thus:


awk '{sum += $1} END {print sum}' numbers.list

would be translated into something like:

exec awk {{sum += $1} END {print sum}} numbers.list

If you are converting invocations involving shell globbing, you should remember that Tcl does not handle globbing or expand things into multiple arguments by default. Instead you should write things like this:


exec ls -l {*}[glob *.tcl]

 

WINDOWS EXAMPLES

Here are some examples of the use of the exec command on Windows.

To start an instance of notepad editing a file without waiting for the user to finish editing the file:


exec notepad myfile.txt &

To print a text file using notepad:


exec notepad /p myfile.txt

If a program calls other programs, such as is common with compilers, then you may need to resort to batch files to hide the console windows that sometimes pop up:


exec cmp.bat somefile.c -o somefile

With the file cmp.bat looking something like:

@gcc %1 %2 %3 %4 %5 %6 %7 %8 %9

Sometimes you need to be careful, as different programs may have the same name and be in the path. It can then happen that typing a command at the DOS prompt finds a different program than the same command run via exec. This is because of the (documented) differences in behaviour between exec and DOS batch files.

When in doubt, use the command auto_execok: it will return the complete path to the program as seen by the exec command. This applies especially when you want to run ``internal'' commands like dir from a Tcl script (if you just want to list filenames, use the glob command.) To do that, use this:


exec {*}[auto_execok dir] *.tcl

 

SEE ALSO

error(n), open(n)  

KEYWORDS

execute, pipeline, redirection, subprocess


 

Index

NAME
SYNOPSIS
DESCRIPTION
PORTABILITY ISSUES
UNIX EXAMPLES
WINDOWS EXAMPLES
SEE ALSO
KEYWORDS

This document was created by man2html using the manual pages.
Time: 17:30:52 GMT, October 23, 2013

ENGLISH - ENGLISH - cs - da - ENGLISH - ENGLISH - ENGLISH - ENGLISH - ja - nl - pl - ro - ENGLISH - zh_CN

Meet new people