# How to end the script

**URL:** <https://in-thread.sonic-pi.net/t/how-to-end-the-script/5271>\
**Category:** Support, Help & Resources\
**Created:** [March 17, 2021, 8:18pm UTC](https://in-thread.sonic-pi.net/t/how-to-end-the-script/5271 "2021-03-17T20:18:51Z")\
**Posts on this page:** 1\
**Showing post:** 2

<div class="post-metadata">

**Author:** ![Nechoj](https://dub1.discourse-cdn.com/flex017/user_avatar/in-thread.sonic-pi.net/nechoj/32/1814_2.png) [@Nechoj](https://in-thread.sonic-pi.net/u/Nechoj)\
**Post date:** [March 17, 2021, 8:24pm UTC](https://in-thread.sonic-pi.net/t/how-to-end-the-script/5271/2 "2021-03-17T20:24:23Z")

</div>

I have done it using the stop command inside the live loop. This can be triggered by a cue set by the control loop. See example [here](https://in-thread.sonic-pi.net/t/play-along-the-jig-saw-puzzle-blues/5121)  
The relevant line says

```ruby
stop if (get :run) == 0

```

Here an example from SPI’s tutorial with a control loop added to make it stop:

```ruby
# Compus Beats

# Coded by Sam Aaron
# control loop added by Nechoj

use_sample_bpm :loop_compus, num_beats: 4

set :run, 1

live_loop :control do
  if tick == 12
    set :run, 0
    stop
  end
  sleep 1
end

live_loop :loopr do
  stop if (get :run) == 0
  sample :loop_compus, rate: [0.5, 1, 1, 1, 1, 2].choose unless one_in(10)
  sleep 4
end

live_loop :bass do
  stop if (get :run) == 0
  sample :bass_voxy_c, amp: rrand(0.1, 0.2), rate: [0.5, 0.5, 1, 1,2,4].choose if one_in(4)
  use_synth :mod_pulse
  use_synth_defaults mod_invert_wave: 1
  play :C1, mod_range: 12, amp: rrand(0.5, 1), mod_phase: [0.25, 0.5, 1].choose, release: 1, cutoff: rrand(50, 90)
  play :C2, mod_range: [24, 36, 34].choose, amp: 0.35, mod_phase: 0.25, release: 2, cutoff: 60, pulse_width: rand
  sleep 1
end

```

---

_[View the full topic](https://in-thread.sonic-pi.net/t/how-to-end-the-script/5271)._
