Is there any way to know if a process has started to run from a call of exec() or has started from the terminal by the user?
-
3Processes started from the terminal are also started by means of `exec()`. – JFMR Jan 16 '18 at 09:46
-
Interesting (+1). Out of interest, you can do this in Windows. Which somewhat legitimises this question, IMHO at least. The various UNIXs are "purer" in this respect though, so quite possibly not. – Bathsheba Jan 16 '18 at 09:47
-
1May the [parent ID](http://man7.org/linux/man-pages/man2/getpid.2.html) can be a hint? – CijcoSistems Jan 16 '18 at 09:49
-
Check the return value of exec(). – danglingpointer Jan 16 '18 at 10:03
-
You can build process tree to distinguish processes started in terminal from other processes. – ks1322 Jan 16 '18 at 10:15
-
@LethalProgrammer: there's no point in checking the return value of any of the `exec()` functions. If it succeeded, it doesn't return; if it returns, it failed (and `errno`, not the return value, will tell you more about why it failed). – Jonathan Leffler Jan 16 '18 at 15:08
3 Answers
Helpful to you: child and parent process id;
getppid() returns the process ID of the parent of the calling process. This will be either the ID of the process that created this process using fork(), or, (!!!CARE!!!) if that process has already terminated, the ID of the process to which this process has been reparented;
I would also consider adding additional program arg.

- 649
- 5
- 9
-
I choose this as the answer because of the last comment: I think the best is using an argument in the call of exec. So I can read the arguments argv and distinguis from there whether the process has been called by an exec from another process, or it has started from the console. Thank you – Daniel Ortega Jan 17 '18 at 08:22
All programs are started by a call to exec
family of functions.
When you type a command in the terminal, for example, it searches for the binary executable, fork
s and calls exec
in the child process. This will substitute the binary image of the calling process (the terminal) for the binary image of the new program. The program will execute and the terminal process will wait
.
There is this absolutely awesome answer by paxdiablo on the question Please explain exec() function and its family that will surely help you understand how exec
works.

- 730,956
- 141
- 904
- 1,278

- 3,029
- 1
- 14
- 24
-
Very interesting. I did not know that all programs are started by a call to exec. The article you reffered was very didactic. Thak you for your answer. – Daniel Ortega Jan 17 '18 at 08:24
In Unix, all processes are created by using the fork
system call, optionally followed by the exec
system call, even those started by a user (they are fork/exec'd by the user's shell).
Depending on what you really want to do, the library function isatty() will tell you if stdin
, stdout
or stderr
are file descriptors of a tty device. i.e. input comes from a terminal, output goes to a terminal or errors go to a terminal. However, a command like
myprog < somefile 1>someotherfile 2>errorfile
will fool code using isatty
. But maybe that is what you want. If you want to take different actions based on whether there is a user typing input from a keyboard or input is coming from a file, isatty
is what you need.

- 730,956
- 141
- 904
- 1,278

- 84,577
- 15
- 123
- 161
-
Your answer seems interesting but in this case it is not what I need. I actually needed to know if a program needs to launch some IPC functions and contact the other process that has launched it, or in case it is an isolated execution, to forget about those verifications. – Daniel Ortega Jan 17 '18 at 08:23