Zsh Mailing List Archive
Messages sorted by: Reverse Date, Date, Thread, Author

Re: exec



On Mon, Jun 3, 2024 at 10:54 AM Mark J. Reed <markjreed@xxxxxxxxx> wrote:
Eahat's the terminal supposed to do with nothing running in it anymore?

That was a rather creative typo for "What" at the start of that question. :)
 
 Exec has the same impact on the shell as exit - shell go bye bye - it just leaves another program in its place. 

In your tests, that other program is echo. Since that's a builtin, things are a little different. In bash or ksh, exec only works on external programs, so if you type exec echo, you'll be running /bin/echo or /usr/bin/echo (or wherever the binary lives) instead of the shell builtin. But in Zsh, exec'ed builtins are still builtins; the shell simulates the effect of exec by exiting after it executes the builtin command. So exec echo whatever is just a shorter way of writing echo whatever; exit

But if you exec an external program that takes a while to run, say exec sleep 300, then you can run ps in another window and see that the shell from which you launched the sleep is no longer there; the sleep has replaced it. In fact, you can see that the process ID formerly belonging to it has been taken over:

Window 1:
zsh% echo $$
42566
zsh% exec sleep 300

Window 2:
zsh% ps -fp42566
  UID   PID  PPID   C STIME   TTY           TIME CMD
  501 42566 42383   0 11:05AM ttys003    0:00.11 sleep 300


Mark J. Reed <markjreed@xxxxxxxxx>


On Mon, Jun 3, 2024 at 10:42 Ray Andrews <rayandrews@xxxxxxxxxxx> wrote:


On 2024-06-03 07:17, Mark J. Reed wrote:
The exec builtin replaces the running shell with whatever program you run.  The point is to avoid clogging the process table with shells that are just hanging out waiting to do nothing but exit as soon as their child process finishes.
I get that. 


In your case, the script exists to set things up in the environment and then run xfce4-session; there's nothing for it to do after xfce4-session completes, so it uses exec to tidy up.

Sure.  But then what? I understand that if a script or function has nothing more to do, it may as well pre-kill itself. But the difference is that 'exec' kills the entire terminal, it doesn't just return to the prompt in a more efficient way -- which would be easy to understand, as above.  exec seems to pull the rug out from under itself, not just end a script more efficiently.  In my case, from what I've heard control seems to pass to dbus.  Mind, if dbus called the script then that's what one might expect. 




--
Mark J. Reed <markjreed@xxxxxxxxx>


Messages sorted by: Reverse Date, Date, Thread, Author