回答:
free
コマンドを使用してメモリ使用量を取得できます。出力を少し解析すると、現在使用されているメモリがわかります:
free -m | awk '/buffers\/cache/ {print $3}'
次に、mail
コマンドを使用してメールを送信できます(メールチェーンがサーバーで既に構成されていると仮定):
echo -e "Mail content." | mail -s "Subject" "destination@expample.com"
これにより、sh
メモリ使用量をチェックし、必要に応じてメールを送信する小さなスクリプトを作成できます。
#!/bin/sh
memuse=$(free -m | awk '/buffers\/cache/ {print $3}')
if [ $memuse -ge 512 ]; then
message="RAM limit exceeded in server alpha.\nCurrent use is $memuse MiB."
echo -e "$message" | mail -s "RAM monitoring" "admin@example.com"
fi
ここで、このスクリプトを定期的に実行する必要があります。たとえばcron
、に追加することで、毎分実行することができます/etc/crontab
:
* * * * * /root/ram_monitoring.sh
(スクリプトを保存し/root/ram_monitoring.sh
、動作しているcronデーモンがあると仮定します。)
$2
が$3
、そうではありませんが、修正します。