LoginSignup
2
2

More than 5 years have passed since last update.

LinuxのcrontabをCSVで出力するシェルスクリプト

Last updated at Posted at 2016-10-01

はじめに

sedコマンドを使って、LinuxサーバのcrontabをCSVファイルで出力するbashシェルスクリプトを作成してみました。
https://github.com/na0AaooQ/crontab-to-csv

実行方法を以下に記載致します。

環境

このスクリプトは、Amazon EC2インスタンス(Amazon Linux)で試しました。
Amazon EC2インスタンスは以下のAMIで作成しました。

・amzn-ami-hvm-2016.03.3.x86_64-gp2 (ami-374db956)

crontabをCSV形式で出力するスクリプト

(1) crontabをCSV形式で出力するスクリプトを作成します。

GitHubからcrontabをCSV形式で出力するスクリプトをダウンロードします。

[ec2-user@cloudwatch-example-server ~]$ git clone https://github.com/na0AaooQ/crontab-to-csv.git

[ec2-user@cloudwatch-example-server ~]$ cd crontab-to-csv

もしくは、GitHubへアクセス出来ないような環境の場合、以下のようにcrontabをCSV形式で出力するスクリプトを作成します。

[ec2-user@cloudwatch-example-server ~]$ vi crontab_to_csv.sh
crontab_to_csv.sh
#!/bin/bash

crontab_csv_file="crontab.csv"

echo "cron_host,cron_user,minutes,hours,days,months,day_of_the_week,cron_name,cron_parameter" > $crontab_csv_file
crontab -l | grep -v ^# | grep -v ^$ | grep -v MAILTO | sed -e "s/  */ /g" -e "s/ /,/1" -e "s/ /,/1" -e "s/ /,/1" -e "s/ /,/1" -e "s/ /,/1" | awk '{$0="'`hostname -s`','$USER'," $0 "";print}' >> $crontab_csv_file

if [ -f $crontab_csv_file ] ; then
  cat $crontab_csv_file
else
  echo "file not found $crontab_csv_file."
fi
[ec2-user@cloudwatch-example-server ~]$ chmod 755 crontab_to_csv.sh
[ec2-user@cloudwatch-example-server ~]$ cat crontab_to_csv.sh
#!/bin/bash

crontab_csv_file="crontab.csv"

echo "cron_host,cron_user,minutes,hours,days,months,day_of_the_week,cron_name,cron_parameter" > $crontab_csv_file
crontab -l | grep -v ^# | grep -v ^$ | grep -v MAILTO | sed -e "s/  */ /g" -e "s/ /,/1" -e "s/ /,/1" -e "s/ /,/1" -e "s/ /,/1" -e "s/ /,/1" | awk '{$0="'`hostname -s`','$USER'," $0 "";print}' >> $crontab_csv_file

if [ -f $crontab_csv_file ] ; then
  cat $crontab_csv_file
else
  echo "file not found $crontab_csv_file."
fi
[ec2-user@cloudwatch-example-server ~]$

crontabをCSV形式で出力するスクリプト実行例

(2) crontabにバッチを登録します。

例として、Linuxサーバのcrontabに適当なバッチを登録します。

[ec2-user@cloudwatch-example-server ~]$ id
uid=500(ec2-user) gid=500(ec2-user) groups=500(ec2-user),10(wheel)
[ec2-user@cloudwatch-example-server ~]$ crontab -e
[ec2-user@cloudwatch-example-server ~]$ crontab -l
MAILTO=************@example.com

##### EC2インスタンスのロードアベレージをCloudWatchに記録する
*/5 * * * * /home/ec2-user/cloudwatch_put_loadaverage.sh

##### EC2インスタンスのnginxプロセス数をCloudWatchに記録する
*/5 * * * * /home/ec2-user/cloudwatch_put_process.sh nginx

##### EC2インスタンスの / のディスク使用率をCloudWatchに記録する
*/5 * * * * /home/ec2-user/cloudwatch_put_disk_use.sh /

##### EC2インスタンスの / のiノード使用率をCloudWatchに記録する
*/5 * * * * /home/ec2-user/cloudwatch_put_disk_inode_use.sh /

##### テストcron
15 11 3 1 2 /home/ec2-user/example_cron.sh example_parameter test 1 2 3
33 7  * * 2 /home/ec2-user/example_cron.sh example_parameter test 1 2 3
[ec2-user@cloudwatch-example-server ~]$

(3) crontabをCSV形式で出力するスクリプトを実行します。

実行前にCSVファイルが存在していない事を確認します。

[ec2-user@cloudwatch-example-server ~]$ ls -lrta crontab.csv
ls: cannot access crontab.csv: No such file or directory
[ec2-user@cloudwatch-example-server ~]$

作成したスクリプトを実行します。
crontabの登録内容がCSVで表示されます。

[ec2-user@cloudwatch-example-server ~]$ ./crontab_to_csv.sh
cron_host,cron_user,minutes,hours,days,months,day_of_the_week,cron_name,cron_parameter
cloudwatch-example-server,ec2-user,*/5,*,*,*,*,/home/ec2-user/cloudwatch_put_loadaverage.sh
cloudwatch-example-server,ec2-user,*/5,*,*,*,*,/home/ec2-user/cloudwatch_put_process.sh nginx
cloudwatch-example-server,ec2-user,*/5,*,*,*,*,/home/ec2-user/cloudwatch_put_disk_use.sh /
cloudwatch-example-server,ec2-user,*/5,*,*,*,*,/home/ec2-user/cloudwatch_put_disk_inode_use.sh /
cloudwatch-example-server,ec2-user,15,11,3,1,2,/home/ec2-user/example_cron.sh example_parameter test 1 2 3
cloudwatch-example-server,ec2-user,33,7,*,*,2,/home/ec2-user/example_cron.sh example_parameter test 1 2 3
[ec2-user@cloudwatch-example-server ~]$

(4) crontabに登録している内容がcsvファイルに出力された事を確認します。

前述のスクリプトを実行すると、以下のようにcrontabの内容がCSVファイルとして出力されます。

[ec2-user@cloudwatch-example-server ~]$ cat crontab.csv
cron_host,cron_user,minutes,hours,days,months,day_of_the_week,cron_name,cron_parameter
cloudwatch-example-server,ec2-user,*/5,*,*,*,*,/home/ec2-user/cloudwatch_put_loadaverage.sh
cloudwatch-example-server,ec2-user,*/5,*,*,*,*,/home/ec2-user/cloudwatch_put_process.sh nginx
cloudwatch-example-server,ec2-user,*/5,*,*,*,*,/home/ec2-user/cloudwatch_put_disk_use.sh /
cloudwatch-example-server,ec2-user,*/5,*,*,*,*,/home/ec2-user/cloudwatch_put_disk_inode_use.sh /
cloudwatch-example-server,ec2-user,15,11,3,1,2,/home/ec2-user/example_cron.sh example_parameter test 1 2 3
cloudwatch-example-server,ec2-user,33,7,*,*,2,/home/ec2-user/example_cron.sh example_parameter test 1 2 3
[ec2-user@cloudwatch-example-server ~]$

出力されるCSVファイルの各フィールドは以下になります。

項目 内容
cron_host cronを実行しているホスト名
cron_user cronを実行しているユーザ名
minutes cron実行タイミング(分)
hours cron実行タイミング(時間)
days cron実行タイミング(日)
months cron実行タイミング(月)
day_of_the_week cron実行タイミング(曜日)
cron_name cronバッチ名
cron_parameter cronバッチのパラメータ

crontabの実行タイミングを指定しているフィールドは「,」区切りで、cronバッチ名とcronバッチパラメータは「,」区切りではなく、crontabに登録されている内容をそのまま出力しています。


以上になります。

2
2
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
2
2