Put computer to sleep and have it ask for password on wake
There are multiple answers to the question of how to programmatically enable 'Ask for password after sleep or screensaver' -- the option that appears in the Security preferences panel.



Once you've enabled that option, you can use the following command to run an AppleScript to sleep the computer:

osascript -e 'tell application "Finder" to sleep'