下面是我的脚本,只要内存低于500 MB就会检查并发送电子邮件,如果我手动运行它会完美运行.. 如果我通过cron安排它 - 下面一行的输出没有被捕获到输出文件。
ps -A -opid | sudo xargs -n1 -I{} /bin/bash -c 'echo {} $(ls /proc/{}/fd | wc -l);' >> /home/utilization.txt
代码
#!/bin/bash
ip=`ifconfig | grep -oP '(?<=inet addr:)\d+\.\d+\.\d+\d+'`
free=$(free -mt | grep Total | awk '{print $4}')
if [[ "$free" -le 500 ]]; then
ps -eo pid,ppid,cmd,%mem,%cpu --sort=-%mem | head > /home/utilization.txt
echo -e "******************************************************************" >> /home/utilization.txt
echo -e "******************************************************************\n" >> /home/utilization.txt
echo -e "Current active Process and open files\n" >> /home/utilization.txt
ps -A -opid | sudo xargs -n1 -I{} /bin/bash -c 'echo {} $(ls /proc/{}/fd | wc -l);' >> /home/utilization.txt
file=/home/utilization.txt
echo -e "memory is running low on $ip Available memory: $free MB" | mailx -a "$file" -s "Check memory Status" myemail@gmail.com
fi
exit 0
如果通过cron运行输出文件
PID PPID CMD %MEM %CPU
1 0 /sbin/init 0.0 0.0
2 0 [kthreadd] 0.0 0.0
3 2 [migration/0] 0.0 0.0
4 2 [ksoftirqd/0] 0.0 0.0
5 2 [stopper/0] 0.0 0.0
6 2 [watchdog/0] 0.0 0.0
7 2 [migration/1] 0.0 0.0
8 2 [stopper/1] 0.0 0.0
9 2 [ksoftirqd/1] 0.0 0.0
******************************************************************
******************************************************************
Current active Process and open files
手动运行时输出文件
PID PPID CMD %MEM %CPU
1 0 /sbin/init 0.0 0.0
2 0 [kthreadd] 0.0 0.0
3 2 [migration/0] 0.0 0.0
4 2 [ksoftirqd/0] 0.0 0.0
5 2 [stopper/0] 0.0 0.0
6 2 [watchdog/0] 0.0 0.0
7 2 [migration/1] 0.0 0.0
8 2 [stopper/1] 0.0 0.0
9 2 [ksoftirqd/1] 0.0 0.0
******************************************************************
******************************************************************
Current active Process; their PIDs and the open files
PID 0
1 8
2 0
3 0
4 0
5 0
6 0
7 0
8 0
9 0
10 0
.....
....
我不确定我哪里出错了。