I am trying to add a crontab so that I can get the cloudwatch metrics of diskspace used and disk space utilization every 5 minutes through a user data script. Below is my user-data script:
#!/bin/bash
sudo yum install -y perl-Switch perl-DateTime perl-Sys-Syslog perl-LWP-Protocol-https perl-Digest-SHA.x86_64
curl https://aws-cloudwatch.s3.amazonaws.com/downloads/CloudWatchMonitoringScripts-1.2.2.zip -O
unzip CloudWatchMonitoringScripts-1.2.2.zip && rm CloudWatchMonitoringScripts-1.2.2.zip && cd aws-scripts-mon
crontab<<EOF
*/1 * * * * ~/aws-scripts-mon/mon-put-instance-data.pl -mem-util --mem-used --mem-avail --swap-util --disk-space-util --disk-space-used --disk-space-avail --memory-units=megabytes --disk-path=/dev/xvda1 --from-cron
EOF
./mon-put-instance-data.pl -mem-util --mem-used --mem-avail --swap-util --disk-space-util --disk-space-used --disk-space-avail --memory-units=megabytes --disk-path=/dev/xvda1
All these steps are working properly when running from aws-terminal, also no steps are failing in cloud-init-logs. The first time I am able to get the cloud watch metrics but after that, they don't come through, so it means crontab is not working, how can this be fixed?
Don't use -e as that is edit mode, you want something like:
(crontab -l 2>/dev/null || echo ""; echo "*/5 * * * * /path/to/job -with args") | crontab -
Update: Added empty echo pipe, to avoid user data stopping due to failure in missing crontab list for root (default running user for userdata), which would return non-zero and fail the script.
Note: Also be aware that Userdata by default runs only once, not every time the instance is rebooted. So changing userdata when the instance is stopped doesn't let you test different ways unless you modify as per this document. If set to run each time, the script above would also concatenate the same rule over and over to crontab!
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With