Что делает «exec»?

Недавно я имел в виду запустить команду pkexec, но случайно удалось запустить команду exec самостоятельно без параметров или аргументов. Я просмотрел страницы man для описания exec, но я до сих пор не знаю, что это значит:

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 fur‐
       ther details about the replacement of the current process image.)

И на самом деле описание продолжается ... Но остальная часть описание тоже не помогло мне ... Так что это действительно мой вопрос, что делает команда exec? И сделал ли я что-либо, просто выполнив его сам по себе без параметров или аргументов, так же как exec? Имеет ли это отношение к команде pkexec?

1
задан 6 September 2015 в 23:27

7 ответов

в дополнение к тому, что сказал muru, exec - команда bash builtin. Следующее описание копируется из справочной страницы bash (вы можете использовать «man bash» для просмотра):

exec [-cl] [-a name] [command [arguments]] If command is specified, it replaces the shell. No new process is created. The arguments become the arguments to command. If the -l option is supplied, the shell places a dash at the beginning of the zeroth argument passed to command. This is what login(1) does. The -c option causes com- mand to be executed with an empty environment. If -a is supplied, the shell passes name as the zeroth argument to the executed command. If command cannot be executed for some reason, a non- interactive shell exits, unless the shell option execfail is enabled, in which case it returns failure. An interactive shell returns failure if the file cannot be executed. If command is not specified, any redirections take effect in the current shell, and the return status is 0. If there is a redirection error, the return status is 1.
1
ответ дан 23 May 2018 в 17:41

Выполняя man exec, вы столкнулись с семейством функций exec(), которые в основном обертывают системный вызов execve(2).

Вы выполнили exec оболочку:

$ type -a exec 
exec is a shell builtin

Теперь из help exec:

Replace the shell with the given command.

Execute COMMAND, replacing this shell with the specified program.
ARGUMENTS become the arguments to COMMAND.  If COMMAND is not specified,
any redirections take effect in the current shell.

Обычно новый процесс создается комбинацией системных вызовов fork(2)-exec(). Здесь fork() создаст дочерний процесс с новым PID, дублируя родительский процесс, а затем exec() заменит дочерний процесс желаемым исполняемым файлом, который мы хотим запустить. Например, когда мы хотим запустить исполняемый файл, скажем top, оболочка вызывает fork(2), создавая таким образом дочерний процесс, который является точно таким же, как оболочка с новым PID, а затем этот вновь созданный процесс заменяется на /usr/bin/top с помощью вызова exec().

Теперь в оболочке, встроенной в exec, fork() не вызывается, вместо этого exec() вызывается непосредственно, поэтому оболочка заменяется исполняемым файлом напрямую, а новый процесс будет наследовать PID оболочки.

Обратите внимание, что только exec без какого-либо аргумента или перенаправления ничего не сделает.

1
ответ дан 23 May 2018 в 17:41
  • 1
    Итак, бег exec сам по себе в Терминале ничего не делает? – Paranoid Panda 6 September 2015 в 23:55
  • 2
    @ParanoidPanda Это упоминается в моем последнем абзаце the fork() is not called, instead exec() is called directly so the shell is replaced by the executable directly and the new process will inherit the PID of the shell. – heemayl 6 September 2015 в 23:57
  • 3
    @ParanoidPanda Думаю, вы имели в виду только exec, тогда ничего не сделаете .. – heemayl 7 September 2015 в 00:02

в дополнение к тому, что сказал muru, exec - команда bash builtin. Следующее описание копируется из справочной страницы bash (вы можете использовать «man bash» для просмотра):

exec [-cl] [-a name] [command [arguments]] If command is specified, it replaces the shell. No new process is created. The arguments become the arguments to command. If the -l option is supplied, the shell places a dash at the beginning of the zeroth argument passed to command. This is what login(1) does. The -c option causes com- mand to be executed with an empty environment. If -a is supplied, the shell passes name as the zeroth argument to the executed command. If command cannot be executed for some reason, a non- interactive shell exits, unless the shell option execfail is enabled, in which case it returns failure. An interactive shell returns failure if the file cannot be executed. If command is not specified, any redirections take effect in the current shell, and the return status is 0. If there is a redirection error, the return status is 1.
1
ответ дан 23 May 2018 в 17:41

в дополнение к тому, что сказал muru, exec - команда bash builtin. Следующее описание копируется из справочной страницы bash (вы можете использовать «man bash» для просмотра):

exec [-cl] [-a name] [command [arguments]] If command is specified, it replaces the shell. No new process is created. The arguments become the arguments to command. If the -l option is supplied, the shell places a dash at the beginning of the zeroth argument passed to command. This is what login(1) does. The -c option causes com- mand to be executed with an empty environment. If -a is supplied, the shell passes name as the zeroth argument to the executed command. If command cannot be executed for some reason, a non- interactive shell exits, unless the shell option execfail is enabled, in which case it returns failure. An interactive shell returns failure if the file cannot be executed. If command is not specified, any redirections take effect in the current shell, and the return status is 0. If there is a redirection error, the return status is 1.
1
ответ дан 23 May 2018 в 17:41

в дополнение к тому, что сказал muru, exec - команда bash builtin. Следующее описание копируется из справочной страницы bash (вы можете использовать «man bash» для просмотра):

exec [-cl] [-a name] [command [arguments]] If command is specified, it replaces the shell. No new process is created. The arguments become the arguments to command. If the -l option is supplied, the shell places a dash at the beginning of the zeroth argument passed to command. This is what login(1) does. The -c option causes com- mand to be executed with an empty environment. If -a is supplied, the shell passes name as the zeroth argument to the executed command. If command cannot be executed for some reason, a non- interactive shell exits, unless the shell option execfail is enabled, in which case it returns failure. An interactive shell returns failure if the file cannot be executed. If command is not specified, any redirections take effect in the current shell, and the return status is 0. If there is a redirection error, the return status is 1.
1
ответ дан 23 May 2018 в 17:41

в дополнение к тому, что сказал muru, exec - команда bash builtin. Следующее описание копируется из справочной страницы bash (вы можете использовать «man bash» для просмотра):

exec [-cl] [-a name] [command [arguments]] If command is specified, it replaces the shell. No new process is created. The arguments become the arguments to command. If the -l option is supplied, the shell places a dash at the beginning of the zeroth argument passed to command. This is what login(1) does. The -c option causes com- mand to be executed with an empty environment. If -a is supplied, the shell passes name as the zeroth argument to the executed command. If command cannot be executed for some reason, a non- interactive shell exits, unless the shell option execfail is enabled, in which case it returns failure. An interactive shell returns failure if the file cannot be executed. If command is not specified, any redirections take effect in the current shell, and the return status is 0. If there is a redirection error, the return status is 1.
1
ответ дан 23 May 2018 в 17:41

в дополнение к тому, что сказал muru, exec - команда bash builtin. Следующее описание копируется из справочной страницы bash (вы можете использовать «man bash» для просмотра):

exec [-cl] [-a name] [command [arguments]] If command is specified, it replaces the shell. No new process is created. The arguments become the arguments to command. If the -l option is supplied, the shell places a dash at the beginning of the zeroth argument passed to command. This is what login(1) does. The -c option causes com- mand to be executed with an empty environment. If -a is supplied, the shell passes name as the zeroth argument to the executed command. If command cannot be executed for some reason, a non- interactive shell exits, unless the shell option execfail is enabled, in which case it returns failure. An interactive shell returns failure if the file cannot be executed. If command is not specified, any redirections take effect in the current shell, and the return status is 0. If there is a redirection error, the return status is 1.
1
ответ дан 23 May 2018 в 17:41

Другие вопросы по тегам:

Похожие вопросы: