# Catching script end?

**URL:** <https://rubytalk.org/t/catching-script-end/1094>\
**Category:** ruby-talk\
**Created:** [9 August 2002 16:21 UTC](https://rubytalk.org/t/catching-script-end/1094 "2002-08-09T16:21:40Z")\
**Posts on this page:** 4\
**Page:** 1

<div class="post-metadata">

**Author:** ![Laurent\_Julliard](https://avatars.discourse-cdn.com/v4/letter/l/919ad9/32.png) [@Laurent\_Julliard](https://rubytalk.org/u/Laurent_Julliard)\
**Post date:** [9 August 2002 16:21 UTC](https://rubytalk.org/t/catching-script-end/1094/1 "2002-08-09T16:21:40Z")

</div>

All,

Are you aware of anyway to catch the end of a Ruby script execution when  
there is no explicit exit instruction?

a) SystemExit seems to be raised only when ‘exit’ is used explicitely.  
b) I have found a workaround to catch exit! as well by redefining

But what to do when there is no exit at all ?

Thanks for your help

Laurent

---

<div class="post-metadata">

**Author:** ![ts1](https://avatars.discourse-cdn.com/v4/letter/t/a8b319/32.png) [@ts1](https://rubytalk.org/u/ts1)\
**Post date:** [9 August 2002 16:25 UTC](https://rubytalk.org/t/catching-script-end/1094/2 "2002-08-09T16:25:39Z")

</div>

> Are you aware of anyway to catch the end of a Ruby script execution when  
> there is no explicit exit instruction?

Something like this ?

pigeon% ruby -e 'at\_exit { p "end" }'  
"end"  
pigeon%

Guy Decoux

---

<div class="post-metadata">

**Author:** ![Waynefchin](https://avatars.discourse-cdn.com/v4/letter/w/aeb1de/32.png) [@Waynefchin](https://rubytalk.org/u/Waynefchin)\
**Post date:** [10 August 2002 00:05 UTC](https://rubytalk.org/t/catching-script-end/1094/3 "2002-08-10T00:05:21Z")

</div>

Laurent Julliard [Laurent.Julliard@xrce.xerox.com](mailto:Laurent.Julliard@xrce.xerox.com) wrote in message news:[3D53EC0C.5060405@xrce.xerox.com](mailto:3D53EC0C.5060405@xrce.xerox.com)…

> All,
> 
> Are you aware of anyway to catch the end of a Ruby script execution when  
> there is no explicit exit instruction?
> 
> a) SystemExit seems to be raised only when ‘exit’ is used explicitely.  
> b) I have found a workaround to catch exit! as well by redefining
> 
> But what to do when there is no exit at all ?
> 
> Thanks for your help
> 
> Laurent

How about using an END block?

In this example, I use an END block and its complementarhy BEGIN block:

BEGIN {  
puts ‘Welcome’  
}

puts ‘hello, world’

END {  
puts ‘Goodbye, cruel world.’  
}

gives:

Welcome  
hello, world  
Goodbye, cruel world.

---

<div class="post-metadata">

**Author:** ![Laurent\_Julliard](https://avatars.discourse-cdn.com/v4/letter/l/919ad9/32.png) [@Laurent\_Julliard](https://rubytalk.org/u/Laurent_Julliard)\
**Post date:** [13 August 2002 07:18 UTC](https://rubytalk.org/t/catching-script-end/1094/4 "2002-08-13T07:18:28Z")

</div>

ts wrote:

> > > > > > “L” == Laurent Julliard [Laurent.Julliard@xrce.xerox.com](mailto:Laurent.Julliard@xrce.xerox.com) writes:
> 
> > Are you aware of anyway to catch the end of a Ruby script execution when  
> > there is no explicit exit instruction?
> 
> Something like this ?
> 
> pigeon% ruby -e ‘at\_exit { p “end” }’  
> “end”  
> pigeon%

at\_exit is exactly what I was looking for. I combined this with aliasing  
exit! to exit (at\_exit doesn’t catch exit!) in my ruby module and now I  
catch all exit events whether they come from and explicit exit/exit! or  
from an implicit exit

Thanks for the tip!

Laurent
