0
0

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

More than 3 years have passed since last update.

Linuxでコマンドを定期実行する方法

Posted at

概要

LinuxでサーバのCPUやメモリの使用率など一定間隔で実行したい場合に、利用するスクリプトを作成する。
whileとsleepコマンドで実現する。

シェルスクリプトの作成

ソースコード

# !/bin/bash

# command_listに定期的に実行したいコマンドを追加する。
# コマンドの実行間隔をINTERVALに定義する。単位は秒。
# ログアウト後も実行する場合は、「nohup bash loop_exec.sh &」で実行する。
# スクリプトの実行は、「ctrl + C」もしくは「kill -9 <pid>」で停止する。

INTERVAL=3

function command_list() {
        date
        free -h
        lsblk
        df -h
        ss -atup
        ps aux
}

while true;
do
        command_list >> watch.log
        sleep ${INTERVAL}
done
0
0
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
0
0

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?