The exec() family of functions replaces the current process image with a new process image, retaining the pid and pipes of the old process.

learn more… | top users | synonyms

23
votes
3answers
740 views

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

That is literal, {fd} isnt 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
22
votes
4answers
1k 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 ...
14
votes
4answers
43k 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?
11
votes
4answers
5k 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
1answer
3k 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 ...
10
votes
2answers
740 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 ...
8
votes
2answers
348 views

reason for exec in wrapper scripts

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

redirect and log script output

I am trying to tidy up the following snippets, design goals are to log all output from a script, and should not be a wrapper. Less lines are better. I don't care about user inputs (at this stage), ...
6
votes
4answers
336 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 ...
6
votes
1answer
4k views

What does exec 3<&1 do?

I understand exec can do I/O Redirection on current shell level. but I only sees usage like exec 6<&0 # Link file descriptor #6 with stdin. # Saves stdin. exec 6>&1 # ...
6
votes
2answers
81 views

direct execution of python scripts

I have noticed that sometimes python scripts are not being started directly, ie /foo/bar.py, but rather from a shell script, which does nothing else than /usr/bin/python -O /foo/bar.py $@ One such ...
5
votes
2answers
1k 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. ...
5
votes
2answers
12k 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" ] |...
5
votes
3answers
7k views

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 &>&...
5
votes
2answers
363 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 ...
4
votes
5answers
471 views

File descriptors across exec

By default file descriptors remain open across the exec functions. The benefit is perhaps understandable for descriptors 0-2. But is there a practical use case for keeping other descriptors open? Are ...
4
votes
3answers
846 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 ...
4
votes
2answers
215 views

What does p and e denote in exec family of calls?

l and v in exec calls denote whether the arguments are provided via list or array(vector). I read it somewhere that p denotes the user's path and e denote the environment but did not understand what ...
4
votes
5answers
40k views

php shell_exec() permission on Linux Ubuntu

I'm developing a php application using Linux server. My problem is executing shell_exec() to call some exe file is not work(actually not exe, it's kind of linux executable file) echo shell_exec("...
4
votes
1answer
4k views

How does bash execute an ELF file?

When I'm on my Linux Box I use bash as a shell. Now I wondered how bash handles the execution of an ELF file, that is when I type ./program and program is an ELF file. I grepped the bash-4.3.tar.gz, ...
4
votes
1answer
95 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, ...
4
votes
1answer
131 views

How to execute as the logged-in user in a sudo script (OSX)?

Is there a way to switch contexts like that? The script in my specific case is an installer postinstall script, which requires sudo privileges only for some of the tasks and requires the logged-in ...
4
votes
2answers
175 views

What's the recommended way to run a service as a non-root user?

I have a simple init.d script which starts and stops a process. I call the init.d as root, but I'd like the process it controls to run as a specific user. The most common suggestion online seems to ...
4
votes
0answers
107 views

Is it possible to make an exec call that “returns”?

Is there a way to make the exec system call work like a function call that returns instead of totally replacing the current process image? My motivation is trying to integrate two shells with ...
3
votes
4answers
388 views

find -exec exit 1 \; Doesn't work neither does find -exec sh -c exit 1 \;

Using Enterprise Linux 5/6, Bash 4.x I want this type of logic: # IF temp file exists, exit because we are restarting already find /tmp/restarting_server -mmin -10 -exec exit 1 lsof -i TCP:1234 ...
3
votes
2answers
398 views

The «sh» being launched with execl() becomes a zombie

I am spent whole half a day, but still couldn't figure, why is the dash being launched with execl call just becomes a zombie. Below is a minimal test case — I'm just fork a child, make a duplicate of ...
3
votes
2answers
591 views

umask XXX (permission bits) to give execute permission to files

Because files created by default having permission of 666 and umask (in permission bits form) subtract bit-wise from this permission, can we do something to give execute permission without using ...
3
votes
3answers
4k views

What after exec() in ls command. Is the parent process printing the output to the console or the child?

I have a simple doubt on execution of the command ls. As per my understanding from the research I have done on the internet, I understood the below points. When we type ls command shell interprets ...
3
votes
1answer
64 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 ...
3
votes
2answers
107 views

Using find -exec how to print name of file in front of each line?

Similar to, but beyond: find . -iname "somepatter*txt" -print -exec cat {} \; How might the file name from each file be printed in front of each line with the results from find?
3
votes
1answer
117 views

How to I prevent exec from keeping the script running?

I'm using exec like this: #!/bin/bash exec > >(tee /tmp/mylog.log) 2>&1 bash -c 'sleep 12312' & I want the output of the commands to be in a log file, and then I want to start a ...
3
votes
1answer
72 views

GDB hangs forever on Solaris

GDB seems to hang everytime when I try run command from gdb prompt. When I ran ps, there are two gdb processes that have been spawned and pstack reveals the following - 15:47:02:/home/stufs1/...
3
votes
1answer
96 views

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 ...
2
votes
3answers
3k views

`solaris + xargs command for solaris

the command find /tmp -name 'core*' -type f -print0 | xargs -0 works fine on Linux, but xargs -0 option is not legal on Solaris what is the equivalent option ( xargs? ) for Solaris 10 second ...
2
votes
4answers
5k 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 ...
2
votes
1answer
127 views

Cp files into subdirectory with find -exec

Why is this command not copying the 17 .png files into a sub-directory within my current directory? $ find . -maxdepth 1 -mtime -3 | wc -l 17 $ find . -maxdepth 1 -mtime -3 -print0 | grep -z loader |...
2
votes
1answer
84 views

Push onto array with find exec

I want to iterate over all the files found by find and add each one to an array. Here's what I have so far: myarray=() find . -name '*.php' -exec myarray\+=\({}\) \; echo "${myarray[@]}" Instead, ...
2
votes
1answer
46 views

Permission denied when using -exec {} ls with find command

When run following command it gives me permission denied message for all the files. find /data/code/ -name "*.jar" -exec {} ls \; find: `/data/code/project/shared/build/thirdparty/log4j-1.2.8/...
2
votes
1answer
37 views

Using xargs for more commands

Is it possible to use find + xargs in order to invoke more than one command? I am trying to find all files whose size is greater than 100 chars. and less than 1000 chars , change their permissions and ...
2
votes
1answer
2k views

Why does chroot get ENOENT on an existing file?

;TL-DR - Answer: because the dynamic linker ld-linux-x86-64.so.2 was missing. I have mounted a squashfs (shouldn't matter) filesystem -ro,loop at /mnt/foo. It contains among other things the ...
2
votes
1answer
26 views

Embedding a binary and exec it, feasibility

I have a binary daemon that depends on another binary daemon at runtime to exist; I don't like this arrangement. (My code works on both OS X and Linux I say linux because I believe its okay to only ...
2
votes
1answer
166 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
1answer
2k views

GDB cannot exec my test program

My computer run with Ubuntu 14.04. GDB seems to be abnormal in different account. For example I make a very simple test. I write a file under ~/test/test.c like this: #include <stdio.h> #...
2
votes
0answers
57 views

Detach from tmux and rotate to ssh without dropping exec?

I've been using tmux launched by default as part of my shell at all times for some time now and find it very convenient. At the same time ssh'ing from inside tmux to another system is not ideal ...
2
votes
0answers
290 views

PHP exec and amixer

I'm trying to make php lower the dB level of amixer using exec, but it seems like php isn't able to perform the task which requires sudo-access. What should be the best way to solve this? I tried: ...
2
votes
1answer
142 views

exec cp failes from script, yet works when issued directly

I have a script that copies SQL backups to a windows server. Here's the line from /etc/fstab: //my.win.box/share$ /winshare cifs credentials=/etc/credfile,dom=mydomain,uid=0,gid=0,file_mode=...
1
vote
3answers
195 views

How do I create a copy of a file in multiple directories?

I have a folder structure conf/ widget1/ env1.cfg env2.cfg widget2/ env1.cfg env2.cfg widget3/ env1.cfg env2.cfg widget4/ env1....
1
vote
1answer
435 views

Redirect all bash script output (from inside the script) to two files: one append, one rewrite; but discard output to the console

I can easily redirect both standard and error output of a bash script to the file, while discarding an output to console with simple exec statement: exec &>>/var/log/backup.log But how do ...
1
vote
4answers
208 views

All subprocesses are Bash (!),

I was trying to use GDB, and found that whenever it attempted to spawn the program I was debugging, Bash was spawned instead (GDB would say starting myProg..., and then Bash would appear. When I ...
1
vote
1answer
851 views

How does -exec actually work

I am confused on the -exec command. E.g. in this case: find . -type f -name "*.c" -exec cat {} \;>all_c_files.txt It seems that I get cat file1 file2 file3 ... fileN While in this case: ...