zsh-workers
 help / color / mirror / code / Atom feed
* using trap function to cleanup and exit?
@ 2022-04-10 15:46 Greg Klanderman
  2022-04-10 15:57 ` Peter Stephenson
  0 siblings, 1 reply; 17+ messages in thread
From: Greg Klanderman @ 2022-04-10 15:46 UTC (permalink / raw)
  To: Zsh list


Hi,

I've been trying to ensure some cleanup is done when a script I'm
writing exits, whether normally, with an error, or when killed (other
than with -9, of course).

I had thought a try/always block would handle signals as well, but
it appears not.

So I've been trying to use either trap functions or the trap builtin
(list traps) with no success.

[zsh below is debian/5.8.1-1 (Debian testing)]

Here's a simple test script (the sleep has been substituted for an
external command that waits on a condition and outputs something; my
actual use case also has that within a loop):

<script>

#!/bin/zsh -f

cleanup () {
  echo "cleanup"
}

do_something () {
  echo "in do_something"
  local foo
  read -u 0 foo
  echo "done do_something"
}

TRAPTERM () {
  echo "in TRAPTERM"
  cleanup TERM
  exit $(( 128 + $1 ))
}

foo () {
  echo "in foo"
  sleep 1234567 | do_something
}

foo

</script>

When run, it immediately prints:

| in foo
| in do_something

then when hit with SIGTERM:

| in TRAPTERM
| cleanup

however, the process and its child (sleep) remain running.

I expected both to exit.

Additionally, when I hit it with some other signal (SIGINT), the
script does get killed, however, the child sleep process remains.
Is that expected?

I tried TRAPEXIT as well, both at top level and within the function
foo, but that seems not to handle signals.

How can I ensure that a script and non-disowned children are killed
when a signal is received, after doing some cleanup?

Is there a way to do that for all appropriate signals, or do I have to
explicitly list the desired signals?

thank you,
Greg


^ permalink raw reply	[flat|nested] 17+ messages in thread

end of thread, other threads:[~2022-04-20  8:16 UTC | newest]

Thread overview: 17+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2022-04-10 15:46 using trap function to cleanup and exit? Greg Klanderman
2022-04-10 15:57 ` Peter Stephenson
2022-04-10 16:12   ` Greg Klanderman
2022-04-10 16:30     ` Greg Klanderman
2022-04-10 18:15       ` Lawrence Velázquez
2022-04-14  5:13         ` Greg Klanderman
2022-04-10 20:49       ` Bart Schaefer
2022-04-10 20:55         ` Bart Schaefer
2022-04-14  5:57         ` Greg Klanderman
2022-04-14 21:29           ` Bart Schaefer
2022-04-14 23:35             ` Bart Schaefer
2022-04-17 17:08               ` Daniel Shahaf
2022-04-15 22:27             ` Bart Schaefer
2022-04-19 18:42               ` Peter Stephenson
2022-04-19 18:55                 ` Peter Stephenson
2022-04-19 21:22                   ` Bart Schaefer
2022-04-20  8:16                     ` Peter Stephenson

Code repositories for project(s) associated with this public inbox

	https://git.vuxu.org/mirror/zsh/

This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for NNTP newsgroup(s).