我想确保脚本只能在某个不错的级别以下运行。我四处寻找,我没有找到这样做的好方法,所以我想出了这个:
#!/usr/bin/env bash
set -euo pipefail
IFS=$'\n\t'
# use ps to get the niceness of the current process
# only look at the last line of output
# remove whitespace
readonly PRIORITY=$(ps -o '%n' $$ | tail -n 1 | tr -d '[[:space:]]')
if [[ "${PRIORITY}" -lt "10" ]]; then
exit 100
fi
这对我来说似乎很草率,如果有更简单的方法来做这件事会很好。我想我真的只是想要一个返回当前进程的调度优先级的小程序,但我不想自己编写 3 行 c 代码:-/