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.

276 questions
72
votes
4 answers

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?
loxaxs
  • 1,824
  • 1
  • 17
  • 14
52
votes
2 answers

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

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" ] || touch "$log_file" exec 1>> $log_file 2>&1 This…
4m1nh4j1
  • 1,823
  • 8
  • 29
  • 40
42
votes
4 answers

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 possible what exactly happens when I execute a…
Josh
  • 8,311
  • 12
  • 54
  • 73
39
votes
2 answers

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 use exec to replace the newly created shell almost…
Martin
  • 7,284
  • 40
  • 125
  • 208
36
votes
3 answers

What does `exec "$@"` do?

I've been seeing this in a lot of docker-entrypoint.sh scripts recently, and can't find an explanation online. My first thoughts are that it is something to do with signaling but that's a pretty wild guess.
John Von Neumann
  • 533
  • 1
  • 5
  • 11
32
votes
3 answers

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 browser. Most of my work is done directly from the…
lord.garbage
  • 2,323
  • 5
  • 27
  • 41
25
votes
4 answers

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

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 &>> log.out echo "This is stdout" echo "This is stderr"…
adarshr
  • 351
  • 1
  • 3
  • 5
25
votes
2 answers

Does POSIX guarantee the paths to any standard utilities?

From C, what's the easiest way to run a standard utility (e.g., ps) and no other? Does POSIX guarantee that, for example, a standard ps is in /bin/ps or should I reset the PATH environment variable to what I get with confstr(_CS_PATH, pathbuf, n);…
Petr Skocik
  • 28,176
  • 14
  • 81
  • 141
22
votes
1 answer

Can you remount /tmp partition to exec while live?

I have a server I need to re mount the /tmp partition to run exec. The code I want to use is: mount -o remount,exec /tmp Run the program that needs it. Than change it back. mount -o remount,noexec /tmp This is production server so I'm really…
Charles Yarbrough
  • 335
  • 1
  • 2
  • 7
22
votes
2 answers

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 comment you could create a (temporary) symbolic link…
phk
  • 5,893
  • 7
  • 41
  • 70
21
votes
6 answers

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 locations on the two systems. What I end up having…
dkv
  • 459
  • 1
  • 5
  • 10
20
votes
3 answers

Can I exec an entirely new process without an executable file?

Suppose my non-root 32-bit app runs on a 64-bit system, all filesystems of which are mounted as read-only. The app creates an image of a 64-bit ELF in memory. But due to read-only filesystems it can't dump this image to a file to do an execve on. Is…
Ruslan
  • 3,290
  • 3
  • 28
  • 49
20
votes
3 answers

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 the file name / path name (depends on the exec…
munjal007
  • 379
  • 3
  • 5
20
votes
4 answers

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 process of it and exec() loads the child process to the…
PriB
  • 469
  • 3
  • 8
  • 16
1
2 3
18 19