Jump to content

EmbyShutdown


BillOatman
 Share

Recommended Posts

BillOatman

Little Windows executable that performs a graceful shutdown of both Emby and the server once no more users are logged on.
More info, a Windows executable, and the source here.

Note that this is written in Net6 so versions of the executable could be made for non Windows operating systems.
If you would like a different operating system supported, let me know.

Edited by BillOatman
  • Thanks 1
Link to comment
Share on other sites

  • 3 weeks later...
siu99ss2

Hi, I’m interested in using this. 
 

Do I need to save the EmbyShutdown file anywhere specific and after I’ve generated the Api key do I need to do anything with it?
 

Thanks

Link to comment
Share on other sites

BillOatman
Posted (edited)
On 5/16/2022 at 6:38 AM, siu99ss2 said:

Hi, I’m interested in using this. 
 

Do I need to save the EmbyShutdown file anywhere specific and after I’ve generated the Api key do I need to do anything with it?
 

Thanks

You run the executable and give it your key as a parameter. The executable doesn't need to be anywhere specific, just on your Emby server.

EmbyShutdown API_KEY Emby-Server-Port

Edited by BillOatman
Link to comment
Share on other sites

siu99ss2

Thanks for the reply. Will give it a go tomorrow after work. 

Link to comment
Share on other sites

BillOatman
Posted (edited)

@Luke  (Or maybe @ebr ?)

This utility checks every 10 minutes for no users to be logged in this way:

 private const string SessionFormat = "http://localhost:8096/emby/Sessions?api_key={0}";           

string uriName = string.Format(SessionFormat, args[0]);
#pragma warning disable CS8600 // Converting null literal or possible null value to non-nullable type.
bool result = Uri.TryCreate(uriName, UriKind.Absolute, out uriResult) && uriResult.Scheme == Uri.UriSchemeHttp;
#pragma warning restore CS8600 // Converting null literal or possible null value to non-nullable type.
if (!result)
{
    Console.WriteLine("Invalid URI parameters");
    Console.WriteLine("EmbyShutdown API_KEY}");
    Console.WriteLine("To get Emby api key go to dashboard>advanced>security and generate one");
    return;
}

args[0] is the api key for the server.

        private bool CheckForActiveSessions(Uri uriResult)
        {
            HttpClient httpClient = new HttpClient();
            httpClient.DefaultRequestHeaders.Add("user-agent", "EmbyShutdown");
            string sessionJson = httpClient.GetStringAsync(uriResult).Result;
#pragma warning disable CS8600 // Converting null literal or possible null value to non-nullable type.
            List<EmbySessionData> sessionList = JsonConvert.DeserializeObject<List<EmbySessionData>>(sessionJson);
#pragma warning restore CS8600 // Converting null literal or possible null value to non-nullable type.

            bool userFound = false;
            if (sessionList != null)
            {
                foreach (EmbySessionData ed in sessionList)
                {
                    if (ed.UserName != null)
                    {
                        userFound = true;
                        Console.WriteLine($"User {ed.UserName} is currently logged in.");
                    }
                }
            }

            return userFound;
        }

I have it scheduled to start at 11pm. However about half the time even though no one is logged in at 11pm or later, it still sees a user logged in (me for example) and spins all night (8-10 hours) waiting for my login to go away.  No one is logged in during this time.  It shows a particular user still logged in, one that had been on that evening.  I shut it down manually in the morning when I notice it.  I was hoping that would be better with Windows server 4.7, but is is behaving the same.

Is there a better way than the above to see if any users are logged in?  Or can this be corrected?

Usually when I exit out of Emby on TiviMate (where I generally watch) I just hit the left arrow button until the Exit button appears, then press it.
I have noticed this happening with people using the Apple TV client as well.

Edited by BillOatman
Link to comment
Share on other sites

The sessions are going to stay there if we're still able to communicate with them (e.g. via push notifications), so you need to filter them on a last activity date of your choosing. So for example, sessions idle for at least X number of minutes.

Link to comment
Share on other sites

BillOatman
4 hours ago, Luke said:

The sessions are going to stay there if we're still able to communicate with them (e.g. via push notifications), so you need to filter them on a last activity date of your choosing. So for example, sessions idle for at least X number of minutes.

My Shield was off (at least at sleep) that entre time, but maybe it still responds to those pings.  In any event, that's a good idea thanks!

Link to comment
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now
 Share

×
×
  • Create New...