On AmazonLinux, set cron and execute AWS commands. As a prerequisite, the IAM Role of that command is required for EC2. I will not write the details this time.
OS Linux/Unix, Amazon Linux 2018.03
#cat /etc/os-release
NAME="Amazon Linux AMI"
VERSION="2017.09"
.
.
# cat /etc/crontab
SHELL=/bin/bash
PATH=/sbin:/bin:/usr/sbin:/usr/bin
MAILTO=root
HOME=/
# For details see man 4 crontabs
# Example of job definition:
# .---------------- minute (0 - 59)
# | .------------- hour (0 - 23)
# | | .---------- day of month (1 - 31)
# | | | .------- month (1 - 12) OR jan,feb,mar,apr ...
# | | | | .---- day of week (0 - 6) (Sunday=0 or 7) OR sun,mon,tue,wed,thu,fri,sat
# | | | | |
# * * * * * user-name command to be executed
I referred to here. https://kb.iu.edu/d/afiz
Let's execute the AWS command that outputs the EC2 Arn list in Tokyo
!/bin/sh
/usr/bin/aws ec2 describe-instances --region ap-northeast-1 |grep Arn |awk -F \" '{print $4}' >> /root/ec2-file
Be sure to try running the file here and check for any errors.
Edit with ** crontab -e ** I want to test the operation, so let it run every minute
* * * * * bash /root/awscron
** Setting completed **
tail -f /var/log/cron
You can check the execution log with
Don't forget to delete the settings when you no longer need them ** Delete settings with crontab -r **
※ -r deletes everything set with crontab -e, so if it's already in operation You can comment out with -e
Tips Running crontab -e as root user edits / var / spool / cron / root Therefore, in this case, it is not necessary to specify the user as before.
As mentioned above, if you make an operation, the cron file will disappear, so I will describe how to not use crontab
Create a file under /etc/cron.d/ for the cron setting file.
For example /etc/cron.d/Crontest
* * * * * root sh /root/awscron
This time, I only described the simple setting. I will summarize in more detail in the future.
cron - wikipedia https://en.wikipedia.org/wiki/Cron
Dangers of crontab -r https://qiita.com/NACK/items/c0c0feda4e7a8030346f
Recommended Posts