hi Davide, there are a couple of ways to run a routine (script, whatever) from a sensor or push button event. You always need to decide in your design whether you want 'push' or 'pull'.... do you want to poll the device to 'see' if the button is pushed, or do you want an interrupt callback to handle the event of pushing the button without you having to check for it deliberately every so many seconds... poll vs interrupt.
You can attach an interrupt to a gpio pin which you set to detect on a rising or falling edge (can be either or both, high or low) and then you set an interrupt handler (its sometimes called a callback) to run when the event triggers the interrupt. Otherwise, you will need to 'read' the gpio pin you are interested in every so often (every thirty seconds, or whatever) and then 'do something' if the switch is closed (whatever that means) I usually 'pullup' my gpio pins with a resistor (they are normally high) and I check to see if they are LOW; the push botton grounds the input usually on my designs.
I am a big fan of interrupts (but they are an advanced topic, and they are tricky). Typically interrupts do not work well from a push button because of contact bounce. if you wan the switch to trigger the interrupt you will need to condition the switch (there are many ways to do this) so that the contact bounce is eliminated. You might use a resistor cap combo with a diode, or you might use a 555 timer one-shot , or you might use a flip-flop debouncer... its up to you; READ about it on-line and try to decide yourself.