Apple - |OSX 10.10.5] Script execution stops when screen locked?

EDIT: With macOS High Sierra (as of beta 5) you can achieve the same "lock screen" behaviour mentioned by @Phong simply through the  status bar menu:

What I think is happening is that your computer goes to sleep after a certain amount of time after you switched to the login window which causes your script to stop.

To prevent your system from sleeping while your script is running you can run caffeinate -s in Terminal (note: only works on MacBooks if AC power is connected) or include it in your script. To stop the command hit CTRL+C.

The way I go about securing my account while I'm still logged in is to enable the "require password" option in system preferences:

require password

Then, as soon as the display (not the system) goes to sleep (also configurable in preferences) you have to enter a password in order to access the account.

You can also manually trigger display sleep by hitting CTRL+SHIFT+EJECT (if your keyboard has an eject button) or by running pmset displaysleepnow.