All operating systems have a setting that will enable the system to automatically lock the current user’s session if the user has been inactive for some period of time. The users have the preference to set how long the inactivity should be. Looking your screen is different from logging out of your system. A locked screen will help prevent unauthorized users from accessing your desktop and files while logging out of the system will terminate the session you have open. If you are looking to automatically log out your Mac after a set period or inactivity as opposed to only just locking it, OS X has an in-built function that allows you do just that. Here is where it is.
Open the System Preferences and go to Security & Privacy group of preferences. If the padlock icon at the bottom is locked, click it and enter the administrator password if asked to. With the preferences session unlocked, an “Advanced” button at the bottom will show up and the “Allow apps downloaded from:” option will no longer show grayed out. Now click the “Advance” Button.
The advanced security tab is where you can set the option to log out your system if you have been inactive for some period of time. If you click the Advanced button tab a pop-up that allows you enable the log out option and set the period of been inactive. Click “OK” and you are done.
Logging out makes it possible that if anyone does figure out your password, they cannot go into your web browser session or locate your files too easily. It also means you won’t be online on your messaging apps like Skype, Facebook messenger etc. when you are away from your desk. This means you will have to open multiple apps again when you return to your work. This also means you are effectively making a trade-off between productivity and security so choose carefully.