akorb / SteamShutdown

Automatic shutdown after Steam download(s) has finished.
MIT License
362 stars 28 forks source link

Any way to shut down the PC after downloads are done on Linux? #52

Open deoxal opened 1 year ago

deoxal commented 1 year ago

I see this is written in C# and I know some devs don't like Mono and you don't have a Linux version already. Wine is an option but I'm hesitant to try it on this since I don't know how it can translate actions for the Windows DE to actions for all the Linux DEs - Cinnamon in my case.

akorb commented 1 year ago

I already want to completely rewrite the program to support Linux as well. Because in fact, I'm a full Linux user and hardly use Windows anymore. But since I use SteamShutdown hardly myself anymore, I can't find much motivation to do so. Also, I'm busy with my studies.

I considered using Rust for a rewriting. As a nice side-effect, it allows with static linking to easily distribute a self-contained executable, which is not so nice with .NET applications. I merge the SteamShutdown and JSON.NET assembly into one with ILMerge at the moment.

But I also thought about Go. So nothing concrete so far.

deoxal commented 1 year ago

Ah I see. Well I'd be willing to donate $100 to support your development if you have a kofi or something.

In the mean time, do you know if there's a hacky solution I could make with bash? Something along the lines of

While true
do
if [[ traffic on Steam's port number == 0 MB/s for 5 minutes ]] ; then
shutdown now
done

And good luck with your studies.

StefanLobbenmeier commented 1 year ago

ChatGPT is usually really good at bash I would recommend using process name though, port seems a bit too unstable for me.

https://chat.openai.com/share/d92cccd8-b7bb-4d35-b82b-fff7ac9f7115

#!/bin/bash

PROCESS_NAME="steam"
TIME_THRESHOLD=300 # 5 minutes in seconds

while true; do
    # Find the PID of the Steam process
    STEAM_PID=$(pgrep "$PROCESS_NAME")

    # If Steam process is running
    if [[ -n "$STEAM_PID" ]]; then
        # Get network activity of the Steam process
        TRAFFIC=$(iftop -t -s 1 -p -F "$STEAM_PID" 2>/dev/null | awk '/Total send rate/ {print $5}')

        # If traffic is 0 MB/s, start counting
        if [[ "$TRAFFIC" == "0.0" ]]; then
            ((TIME_COUNTER++))

            # If traffic has been 0 MB/s for the threshold time, shutdown
            if [[ $TIME_COUNTER -ge $TIME_THRESHOLD ]]; then
                echo "Shutting down due to inactivity on Steam ($PROCESS_NAME) process."
                shutdown now
            fi
        else
            TIME_COUNTER=0 # Reset counter if there's traffic
        fi
    else
        TIME_COUNTER=0 # Reset counter if Steam process is not running
    fi

    sleep 1 # Check every second
done

you will still want to test it a bit yourself, but rough inspections looks good to me