I know this is not knew - but equally it has not had any good answers in the past.
Is there anyway to do this in a graceful way i.e. not using  taskkill.exe as for all I know it might be writing to the database etc. Unless a Dev can confirm this is really safe?
I also don’t really want to use the “run as a service” add-on
The reason is that I want to create an overnight backup script.
Interesting script - thanks.
So this appears to use the taskkill command “taskkill /f /im $plexServerExeFileName /t”
So is it perfectly safe to use taskkill to stop PMS even though this surely can’t be a graceful shutdown? What if PMS is in the middle of house keeping jobs?
Run the script only outside of the housekeeping hours.
No, it is not safe, but there is no other way to stop the process. Ideally, it should be running as a Windows Service, in which case it could be easily stopped, paused, etc, but since it is running just as a desktop app, there is no way to shut it down gracefully. Unfortunately.
How do you know what the housekeeping hours are?
They are set n the PMS Settings under Scheduled Tasks.
I guess I could look at the PMSService add on again. https://github.com/cjmurph/PmsService, although as this does not fully support hardware acceleration/transcoding I think it is out.
I may just go with the once every 3 days database backup built into Plex and not bother taking PMS down when I do a file backup.
Running taskkill /im "Plex Media Server.exe" (without the /f) flag initiates a graceful shutdown of PMS.  Without the /f flag, the process is signaled to close (it appears from my logs that a WM_CLOSE message is sent).  Plex then shuts down various functions (network advertisement, plug-ins, tuner service, database connections) and exits.
Including the /f (force) flag forces a shutdown, whether or not it can be performed gracefully. However, without the flag, the application can choose to continue running. So it’s likely that your script would need to take that into account and monitor that the server has stopped before moving on.
Having said this, I’ve not tested during scheduled maintenance, so it might be a good idea to still run this outside those hours.
The database backup built into Plex is pretty much useless. It would only help if you get a database corruption (say, during an upgrade), but it is not enough for things like migrating Plex or recovering from a hard drive crash. And you absolutely need to take PMS down when you do file backup; otherwise, the risk of getting data corrupted is a lot higher during file backup compared to shutting down PMS.
On a separate note, I can add an option to kill the PMS process without the /f flag first and then if it fails to die, force it (say, after a minute or some sort of timeout).
Thanks for the warning.
I think an option to use no /f and then fallback to using it if it fails to stop PMS would be a great idea.
I want to make sure that in the process of being super cautious with backups etc that I don’t end up creating a problem that was not there before! It would be ironic if the backup process corrupted PMS.
I have the backups script out for over a year and it has been used by many people. So far, nobody complained about PMS corruption. Not that it is absolutely impossible, but it is not very likely.
Good to hear.
Also is it worth adding an option about excluding the cache folder which apparently is not required to be backed up on Windows and can be quite big? Its about 2.5GBytes on mine, mainly in a PhotoTranscoder subfolder.
The Cache folder is required for the backup. The PhotoTranscoder folder contains posters and other important files. If you exclude them, you media info will be messed up.
Also, I updated the script to stop PMS gracefully first and if it fails to exit within 60 seconds, then kill it forcefully.
If you run into any issues, please report at GitHub, so we can track it.
Quote from here https://support.plex.tv/articles/201539237-backing-up-plex-media-server-data/:
Tip! : For Windows and Linux systems, you can exclude the Cache directory, if desired. That can save space and time in the transfer.
Is this just wrong then?
Apparently. If you can auto-generate pictures, then it’s probably okay, but if you had custom images for your media, then, I think, you’d lose them. I asked before and was told that the Cache folder was needed.
I may be wrong, though. If you’re sure you do not need the Cache folder, just add it to the list of the excluded folders in the configuration file. (See Windows Plex Media Server minimal backup)
This topic was automatically closed 90 days after the last reply. New replies are no longer allowed.