iMac restarts instead of shutting down I have a problem with my iMac restarting when it should be shutting down. This doesn't happen all the time but appears to happen more frequently when I've had the computer on for a few hours.

What happens


I shut the computer down via Apple Menu > Shut Down
The shut down cycle completes
The computer immediately reboots


It doesn't behave like this Mac OS X never shuts down. The Mac responds to being shut down immediately.

Fixes I've tried

First I should state I've pulled out all USBs etc. The only wire in is the power cable.

Disk Utility

I've been in touch with Apple Support about this issue, and the only advice they could give was to run First Aid in the Disk Utility. Sometimes this allows me to shut down. Other times not.

Resetting the SMC

I've reset the SMC which doesn't help. This often leads to another issue which is freezing on reboot (Apple logo and progress bar that doesn't move). To fix this I reset the PRAM/VRAM

Reset the PRAM/VRAM

Resetting the PRAM allows me to shut down most of the time. Even then I have to do it 2 or 3 times before it works.

Complete reinstall/upgrade

I've also reinstalled the system twice, one time upgrading from El Capitan to Sierra. This has temporarily fixed the issue, but it soon comes back after a week or so.

Setup

iMac (27-inch, Late 2013)

3.4 GHz Intel Core i5

6 GB 1600 MHz DDR3

NVIDIA GeForce GTX 775M 2048 MB

macOS Sierra 10.12.5

EDIT: Current Energy Saver Settings



I'd really appreciate your help here guys, this has been going on a while and it's becoming a bit of a nuisance.

EDIT: This question appears to highlight a pretty common occurrence. I'm happy to invest more into the bounty to get an answer that resolves or at least identifies the problem causing it to happen.

UPDATE:

After going through the steps in the answer provided by @monomeeth, Apple Diagnostics uncovered an error NDT001: There may be an issue with the Thunderbolt hardware. I've run Diagnostics before, but this time I ran it straight after the Mac restarted from a failed shut down. I'll be taking it into Apple for repair, so I'll keep this answer up to date with the outcome.
Troubleshooting is a process of elimination and you seem to have started off well, doing most of the obvious first steps. Below are some additional steps I'd try next, especially since reinstalling macOS only resolved the problem for a week or so.

Test your iMac in Safe Mode

Follow these steps to boot your iMac into Safe Mode:


Fully shut down your iMac
Restart your iMac
Immediately press the Shift key and keep it down 
Let go of the Shift key when you see the login window (NOTE: If you have FileVault enabled you may need to log in twice).
Take a note of what happens (i.e. can you shut down normally or is the issue still present)
Exit Safe Mode by restarting your iMac as normal
Now test again after the reboot


Now, depending on the above testing, we can narrow down how to proceed:


If the problem is still present during Safe Mode and afterwards, proceed to 1. Run Apple Diagnostics (see below). 
If the problem is not present during Safe Mode then it's time to investigate login items, fonts, and kernel extensions. If that's the case, proceed to 2. Investigating login items, fonts, and kernel extensions (see below).



1. Run Apple Diagnostics

Follow the steps below to run Apple Diagnostics:


Fully shut down your iMac
Restart your iMac
Immediately press the D key and keep it down until you see the Diagnostics screen appear
Wait for Diagnostics to finish (this typically only takes a few minutes)
Once complete, one of two things will appear on the screen:


a No issues found message
a brief description of any errors found plus further instructions

If the diagnostics test does find errors, take a note of what they are


Note: If pressing and holding the D key at Step 3 doesn't work, start again at Step 1 and, at Step 3 press and hold both the OptionD keys instead. This will try and run diagnostics from the internet instead, so you will need to allow more time for it to complete. 


2. Investigating login items, fonts, and kernel extensions

Let's start with Login Items:


Startup normally
Go to System Preferences > Users &amp; Groups
Select the Login Items tab
Take a note of your Login Items 
Now remove all of them by highlighting them and clicking on the - button below
Restart your computer


Now your Mac will boot up without those login items loading. Test this and see if the problem persists: 


If the problem goes away you know one (or more) of the Login items were causing the problem. You can then add them back in one by one until you've identified the culprit. Problem solved.
If the problem does persist, it's either a problem with fonts you've installed yourself or with a 3rd party kernel extension. A little more info below:


Fonts:- You can remove any of the fonts you've installed yourself to see if this makes a difference.

Kernel Extensions:- You can open Terminal (found within your Utilites folder) and enter the following command:

kextstat | grep -v com.apple

The above command will list 3rd party kernel extensions (you may need to stretch the Terminal window so it's easier to read). Look for anything you don't remember installing, or something that belongs to software you've removed, or just anything that looks out of place. Remember though, this is a list of 3rd party kernel extensions and therefore any of them could be causing the problem.