This has worked for me in the past:
Log out (with confirmation)
osascript -e 'tell app "System Events" to log out' Log out directly (no confirmation)
osascript -e 'tell app "System Events" to «event aevtrlgo»' or
osascript -e 'tell application "loginwindow" to «event aevtrlgo»' This way any running application will get noticed and can terminate in a safe fashion.