#!/bin/bash
# 检查参数是否为空
if [[ -z $1 ]]; then
echo "Please provide the process software name as an argument."
exit 1
fi
# 获取软件的进程 ID
process_pid=$(pgrep "$1")
# 检查软件是否在运行
if [[ -z $process_pid ]]; then
echo "process software is not running."
exit 1
fi
# 获取当前时间戳
timestamp=$(date +"%Y-%m-%d %H:%M:%S")
# 获取资源占用情况
resource_usage=$(top -b -n 1 -p $process_pid | grep $process_pid)
# 保存带有时间戳的资源占用情况到文件
echo "$timestamp $resource_usage"
运行效果:
2、修改设置循环监测时间 示例中为2s监测一次
#!/bin/bash
# 检查参数是否为空
if [[ -z $1 ]]; then
echo "Please provide the process software name as an argument."
exit 1
fi
# 获取软件的进程 ID
process_pid=$(pgrep "$1")
# 检查软件是否在运行
if [[ -z $process_pid ]]; then
echo "process software is not running."
exit 1
fi
# 循环执行
while true; do
# 获取当前时间戳
timestamp=$(date +"%Y-%m-%d %H:%M:%S")
# 获取资源占用情况
resource_usage=$(top -b -n 2 -p $process_pid | grep $process_pid)
# 保存带有时间戳的资源占用情况到文件
echo "$timestamp $resource_usage" >> resource_usage.log
# 等待2秒,可自己修改
sleep 2
done