I'm trying to use Linux niceness to give games a higher scheduling priority. This works for native games, but Proton games often change their own niceness, so setting Steam's niceness beforehand doesn't reliably work. I want to use a Steam launch option that waits for the game to start, accepts the process name as an argument, finds the matching PID, runs `renice -n -10 -p PID`, and then exits. What's the simplest and safest way to implement this in Bash?
4 Answers
Consider using cgroups or a system resource-management tool instead of repeatedly changing niceness in a shell script. Grouping the game's processes lets you apply scheduling or CPU policies to the whole process tree, which is more reliable when Proton creates several helper processes. Also keep in mind that lowering niceness to a negative value usually requires appropriate privileges and can make other system processes less responsive.
A straightforward approach is to pass the process name as `$1`, wait in a loop, and use `pgrep` to find matching PIDs. For example: `process_name="$1"; shift; "$@" & while sleep 5; do pids=$(pgrep -x "$process_name") && { for pid in $pids; do renice -n -10 -p "$pid"; done; break; }; done; wait`. Be careful with partial matches, since they can select unrelated processes; `pgrep -x` is safer when you know the exact executable name.
If Steam can launch the actual game command directly, `nice -n 10 command` is simpler because child processes normally inherit their parent's niceness. However, this may not work reliably with Proton because its launcher or game processes can reset or override the value. In that case, finding the game PID after startup is necessary.
You can inspect `/proc/*/comm` instead of scanning the entire process table with external commands. Compare each process's name with the requested name, extract the numeric directory name as the PID, and then call `renice`. A loop that checks every few seconds is usually enough, but add a timeout so the script doesn't run forever if the game fails to launch.

I ended up using `pgrep` to locate the process and built a working script around that approach. It was also a useful way to learn how Bash interacts with running processes.