Skip to content

Instantly share code, notes, and snippets.

@na0AaooQ
Last active October 5, 2016 15:37
Show Gist options
  • Save na0AaooQ/b500486ea63a6a412f50ef95427cda17 to your computer and use it in GitHub Desktop.
Save na0AaooQ/b500486ea63a6a412f50ef95427cda17 to your computer and use it in GitHub Desktop.
LinuxのcrontabをCSVで出力するシェルスクリプト ref: http://qiita.com/na0AaooQ/items/b944af792ac763f482ff
#!/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 ~]$ git clone https://github.com/na0AaooQ/crontab-to-csv.git
[ec2-user@cloudwatch-example-server ~]$ cd crontab-to-csv
[ec2-user@cloudwatch-example-server ~]$ vi crontab_to_csv.sh
[ec2-user@cloudwatch-example-server ~]$ chmod 755 crontab_to_csv.sh
[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 ~]$
[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 ~]$
[ec2-user@cloudwatch-example-server ~]$ ls -lrta crontab.csv
ls: cannot access crontab.csv: No such file or directory
[ec2-user@cloudwatch-example-server ~]$
[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 ~]$
[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 ~]$
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment