Questions tagged [exec]

The exec() family of functions replaces the current process image with a new process image, retaining the pid and pipes of the old process. This tag is also used for the shell built-in which can be used to replace the current shell with a program or various redirection-related stuff.

Filter by
Sorted by
Tagged with
38
votes
4answers
5k views

What exactly happens when I execute a file in my shell?

So, I thought I had a good understanding of this, but just ran a test (in response to a conversation where I disagreed with someone) and found that my understanding is flawed... In as much detail as ...
33
votes
2answers
72k views

Using exec and tee to redirect logs to stdout and a log file in the same time [duplicate]

In a bash script, how can I redirect all standard outputs to a log file and tee the output on the screen using exec ? log_file="$HOME/logs/install.txt-`date +'%Y-%m-%d_%H-%M-%S'`" [ -f "$log_file" ] |...
23
votes
3answers
97k views

How to “correctly” start an application from a shell

I find it hard to phrase the question precisely but I will give my best. I use dwm as my default window manager and dmenu as my application launcher. I hardly use GUI applications aside from my ...
28
votes
3answers
3k views

What does 'exec {fd}</dev/watchdog' do in Bash

That is literal, {fd} isn't a placeholder. I have a script that does this, and does not source in anything, nor does it reference {fd} anywhere else. Is this valid bash? exec {fd}</dev/watchdog
31
votes
2answers
7k views

reason for exec in wrapper scripts

I have seen wrapper script examples which in a nutshell are following: #!/bin/bash myprog=sleep echo "This is the wrapper script, it will exec "$myprog"" exec "$myprog" "$@" As seen above, they ...
21
votes
2answers
2k views

Is there a POSIX way of setting zeroth argument of a target application?

In bash you can use exec -a and in zsh you can alternatively also set ARGV0 to execute a program with a certain zeroth argument but is there also a POSIX way of doing so? As suggested in this one ...
1
vote
1answer
2k views

How to understand the output of tracing running an external command in bash?

In Ubuntu, I run date directly in an interactive bash shell whose pid is 6913. $ date Wed Mar 2 23:57:44 EST 2016 At the same time, I trace the bash shell 6913 from another interactive bash shell ...
17
votes
6answers
6k views

Choose interpreter after script start e.g. if/else inside hashbang

Is there any way to dynamically choose the interpreter that's executing a script? I have a script that I'm running on two different systems, and the interpreter I want to use is located in different ...
13
votes
4answers
2k views

Use case / practical example for Bash' builtin exec

Consider this from the documentation of Bash' builtin exec: exec replaces the shell without creating a new process Please provide a use case / practical example. I don’t understand how this makes ...
8
votes
1answer
27k views

Changing the default shell without chsh or administrator privileges

I saw the following snippet in this thread: How to change from csh to bash as default shell sleep 2 if (-x /usr/local/bin/bash) then exec /bin/sh -c '. ~/.profile; exec /usr/local/bin/bash' endif ...
2
votes
1answer
1k views

find with -exec eval of $0 [duplicate]

In the following command I would like to know why $0 was evaluated to the file found by find and not the echo command. $ find . -type f -perm -u=x -exec bash -c ' /bin/echo $0 is the name of the ...
2
votes
0answers
1k views

How to understand the output of tracing running an external command in background via strace

In Ubuntu, I run date & in an interactive bash shell whose pid is 6913, and at the same time, trace the bash shell from another interactive bash shell by strace. The output of tracing the first ...
0
votes
1answer
522 views

What are differences between `do_execve()` and `_start` copying the command line arguments and environment?

Understanding The Linux Kernel says that execve() calls do_execve( ) which in turn copies the file pathname, command-line arguments, and environment strings into one or more newly allocated page ...
5
votes
3answers
5k views

Does running “exec echo some; echo test” in bash never print “some test”?

Does running exec echo "some "; echo "test" in bash never print "some test"? I would seek confirmation to this question, as I am writing a small shell script and I would like it to not contiue ...
3
votes
5answers
8k views

Bash rename 360 000 files using find -exec

I've got around 360 000 files like: ./001/1/1.jpg ./001/1/2.jpg ./001/2/1.jpg ./002/1/1.jpg ./003/1/1.jpg ... pattern: [60 dirs]/[1000 subdirs]/[4-10 files].jpg I want to rename files using rename ...
67
votes
4answers
190k views

Why use “nohup &” rather than “exec &”

I know that, nohup being a binary, it can be reached from any shell. But the exec built-in probably exists in every shell. Is there a reason to prefer one of them, to the other?
14
votes
3answers
7k views

Why do we have to pass the file name twice in exec functions?

I read Advanced Programming in the UNIX Environment by Stevens, 8th chapter. I read and understand all the six of exec functions. One thing I notice is, in all the exec functions: first argument is ...
17
votes
4answers
17k views

How do fork and exec work?

I don't have much experience, just trying to get involved into the processes how do they interpret to hardware from user level. So when a command is fired from a shell, fork() inherits a child ...
11
votes
4answers
19k views

Copy stdout and stderr to a log file and leave them on the console within the script itself [duplicate]

Using bash, how do I copy stderr and stdout to a log file and also leave them displayed on the console? I would like to do this within the script itself using an exec. I tried with exec &>&...
1
vote
2answers
3k views

Dropping filename extensions with find -exec

When using find, how can I drop the original filename extension (i.e. .pdf) from the second pair of -exec braces ({})? For example: find ~/Documents -regex 'LOGIC.*\.pdf' -exec pdf2svg {} {}.svg \; ...
9
votes
4answers
5k views

When to call fork() and exec() by themselves?

I'm learning about fork() and exec() commands. It seems like fork() and exec() are usually called together. (fork() creates a new child process, and exec() replaces the current process image with a ...
8
votes
1answer
468 views

Is the $SHELL environment variable only for interactive shells?

I see two possible uses for the SHELL environment variable: It can be used to specify the interactive shell the user wants to use, and/or it can be used by processes to execute other commands with, ...
3
votes
1answer
4k views

fork() and exec() confusion

As when we do fork on current process, our process as parent process generates child process with same characteristics but different process IDs. So after that, when we do exec() in our child process, ...
2
votes
1answer
244 views

What properties of an unprivileged process are preserved during an `execve` call?

I am teaching an Operating Systems course and trying to wrap my mind around the fork/execve technique for creating new processes. My current understanding is that a fork make a complete copy of the ...
11
votes
2answers
3k views

Why is vfork() intended to be used when the child process calls exec() or exit() immediately after creation?

Operating System Concepts and APUE say With vfork(), the parent process is suspended, and the child process uses the address space of the parent. Because vfork() does not use copy-on-write, if ...
8
votes
2answers
2k views

What logic does the command “exec tail -n +3 $0” from grub2 config have?

Creating custom menu entry, got stuck on this command: exec tail -n +3 $0 Tried it in terminal, got weird result, cannot understand, what this command exactly does and why grub needs it. Could you ...
5
votes
3answers
3k views

Is it possible to exec some commands in a subshell without immediately exiting afterwards?

I use the fish shell and would like to be able to "source" some shell scripts written with sh-compatible syntax, which fish cannot read. For example lots of software expects you to source some shell ...
1
vote
1answer
481 views

How does `execve()` call a special start-up routine and the main() function?

When a C program is executed by the kernel—by execve(), where does execve() call a special start-up routine crt0 which is called before the main function is called? where does execve() call ...
0
votes
3answers
5k views

Is reparenting from the shell possible?

This question is closely related to How to "correctly" start an application from a shell but tries to tackle a more specific problem. How can I spawn an application from a shell and thereby ...
6
votes
1answer
733 views

Why does ` exec 2>somefile` hang? [duplicate]

Why does exec >somefile return immediately, while exec 2>somefile doesn't and hang on indefinitely? Thanks.
6
votes
2answers
3k views

How does exec bash builtin work internally?

From this link I get the following about exec bash builtin command: If command is supplied, it replaces the shell without creating a new process. How does it exactly replace the shell (i.e. how ...
2
votes
0answers
676 views

Socat exec script in tunnel

I am running a tunnel like this: socat TCP-LISTEN:9090,fork TCP:192.168.1.3:9090 I would like to run a script to execute code with the strings passing through the tunnel. The script does not change ...
2
votes
3answers
1k views

Specifying redirection options through a variable, in an exec command

I want to be able to specify the redirection command/options via a variable (that I might set based on certain conditions, etc.). But when I run this bash script for example : REDIRECT=">>test" ...
2
votes
1answer
632 views

What kinds of commands can run with exec?

What kinds of commands can run with exec? For example, executable files, builtin commands, shell script, compound commands (e.g. looping, conditional branching), pipeline, command list, commands ...
1
vote
1answer
495 views

What happens to the old stack, heap, and (initialized and uninitialized) data segments after execve() call?

Manpage of execve() says execve() executes the program pointed to by filename. This causes the program that is currently being run by the calling process to be replaced with a ...
1
vote
2answers
492 views

find + how to verify that all files and folders are with groups and owner hdfs:hadoop

we want to check that all files and folders under /hadoop/hdfs are with permissions - hdfs:hadoop is it possible to do this test with find command ? in case find capture files/folder that not have ...
1
vote
1answer
325 views

Change the default executable for file with potentially missing shebang

I have a library - users are to create executable files, potentially with a hashbang to tell exec which executable to use. If they omit the hashbang, then I think most systems default to /bin/sh, I ...
0
votes
1answer
45 views

fork(), exec() sequence with object file formats?

It's my understanding that POSIX systems rely on certain linker formats to organize memory during a fork() exec() sequence of calls. a.out, COFF and ELF are widely known formats of binary object ...
0
votes
3answers
3k views

How to sudo + exec?

I need to use sudo to run process as a different user. But how to use sudo with exec like: sudo -u www-data exec php -r 'sleep(2); echo 5;' to replace the sudo process? Why and requirements? ...
0
votes
3answers
1k views

Bash in php exec in webpage don't colors for png and mp3 files on Linux

I have code like this in php: header("Content-Type: text/plain"); exec("/bin/bash -c 'ls --color=always'", $result); echo implode("\n", $result); and I've got result (escape is not visible) ...
0
votes
1answer
51 views

Why would `read -s` behaves differently than `read` if killed with `ctrl-c`?

Consider this script: #!/bin/bash echo "hi there $(whoami)" [ "`whoami`" = "root" ] || { exec sudo -u root "$0" "$@" echo "this is never called" } read -s -p "enter stuff: " stuff echo "answer:...
-2
votes
1answer
733 views

The zeroth argument to a command executed by exec [closed]

From Bash Manual exec [-cl] [-a name] [command [arguments]] If command is supplied, it replaces the shell without creating a new process. If the -l option is supplied, the shell places a ...