从 运行 bash 脚本中确定当前进程的优先级
determine current process priority from within a running bash script
我想确保脚本只能 运行 低于特定的 nice 级别。我四处搜索,但没有找到这样做的好方法,所以我想到了这个:
#!/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
我觉得这有点草率,如果有更简单的方法就更好了。我想我真的只想要一个 returns 当前进程的调度优先级的小程序,但我不想自己编写 3 行 c 代码 :-/
GNU coreutils nice
命令的文档:
Usage: nice [OPTION] [COMMAND [ARG]...]
Run COMMAND
with an adjusted niceness, which affects process scheduling.
With no COMMAND
, print the current niceness.
但是,请注意
NOTE: your shell may have its own version of nice, which usually supersedes
the version described here. Please refer to your shell's documentation
for details about the options it supports.
如果你的 shell 确实有这样一个具有不同行为的内置函数,你会想要使用 运行 二进制而不是内置函数的常用技术。
我想确保脚本只能 运行 低于特定的 nice 级别。我四处搜索,但没有找到这样做的好方法,所以我想到了这个:
#!/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
我觉得这有点草率,如果有更简单的方法就更好了。我想我真的只想要一个 returns 当前进程的调度优先级的小程序,但我不想自己编写 3 行 c 代码 :-/
GNU coreutils nice
命令的文档:
Usage: nice [OPTION] [COMMAND [ARG]...]
Run
COMMAND
with an adjusted niceness, which affects process scheduling. With noCOMMAND
, print the current niceness.
但是,请注意
NOTE: your shell may have its own version of nice, which usually supersedes the version described here. Please refer to your shell's documentation for details about the options it supports.
如果你的 shell 确实有这样一个具有不同行为的内置函数,你会想要使用 运行 二进制而不是内置函数的常用技术。