Skip to content

Commit

Permalink
nicest: run a command with the highest nice/ionice/oom-adj
Browse files Browse the repository at this point in the history
  • Loading branch information
hydrargyrum committed Oct 8, 2024
1 parent 6397db3 commit 9dfddc0
Show file tree
Hide file tree
Showing 3 changed files with 64 additions and 0 deletions.
1 change: 1 addition & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -59,6 +59,7 @@ This repository hosts various small personal tools.
- [`moversleep`](moversleep): move incoming files from a dir into another
- [`multiman`](multiman): open your web browser to manual pages from various OSes
- [`mv-with-thumb`](mv-with-thumb): like mv, but also moves XDG thumbnails
- [`nicest`](nicest): run a command with the highest nice/ionice/oom-adj
- [`no-utf8-bom`](no-utf8-bom): just remove pointless UTF-8 BOM from stdin or files
- [`pass-ls-entries`](pass-ls-entries): list pass(1) entries in find(1) format, not tree(1) format
- [`password-prompt`](password-prompt): simply prompt a password on tty and then print it
Expand Down
46 changes: 46 additions & 0 deletions nicest/nicest
Original file line number Diff line number Diff line change
@@ -0,0 +1,46 @@
#!/bin/sh -eu
# SPDX-License-Identifier: WTFPL

usage () {
cat <<- EOF
usage: $0 COMMAND [ARGS...]
$0 -p PID
Run COMMAND ARGS... with lowest priority regarding CPU (nice(1)),
I/O (ionice(1)) and OOM-adj (choom(1)).
With -p, renice PID instead (and I/O nice and OOM-adj).
EOF
}

if [ "${1-}" = -h ] || [ "${1-}" = --help ]
then
usage
exit 0
elif [ $# -eq 0 ]
then
usage >&2
exit 64
fi

has () {
command -v "$1" > /dev/null 2> /dev/null
}

if [ $1 = -p ]
then
if [ $# -ne 2 ]
then
usage >&2
exit 64
fi
has renice && renice -n 19 -p $2
has choom && choom -n 1000 -p $2
has ionice && ionice -c 3 -p $2
exit 0
fi

has choom && set -- choom -n 1000 -- "$@"
has ionice && set -- ionice -c 3 "$@"
set -- nice -n 19 "$@"

exec "$@"
17 changes: 17 additions & 0 deletions nicest/test.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
#!/bin/sh -eu

cd "$(dirname "$0")"

die () {
echo "$*" >&2
exit 1
}

./nicest -p $$ > /dev/null

./nicest true

if ./nicest false
then
die "false should have exited with error"
fi

0 comments on commit 9dfddc0

Please sign in to comment.