Skip to content

Instantly share code, notes, and snippets.

@yuta-imai
Last active March 13, 2022 01:28
Show Gist options
  • Save yuta-imai/5857793 to your computer and use it in GitHub Desktop.
Save yuta-imai/5857793 to your computer and use it in GitHub Desktop.
With this user data script, you can set up cron job to put some custom metrics for CloudWatch at instance initiation.
#!/bin/sh
cd /home/ec2-user
wget http://ec2-downloads.s3.amazonaws.com/cloudwatch-samples/CloudWatchMonitoringScripts-v1.1.0.zip
unzip CloudWatchMonitoringScripts-v1.1.0.zip
rm CloudWatchMonitoringScripts-v1.1.0.zip
chown ec2-user:ec2-user aws-scripts-mon
echo "*/5 * * * * ec2-user /home/ec2-user/aws-scripts-mon/mon-put-instance-data.pl --mem-util --disk-space-util --disk-path=/ --from-cron" >> /etc/crontab
@lgawin
Copy link

lgawin commented Aug 16, 2016

It is not working for me.

Instead I write last line as
echo "*/5 * * * * ..." >> /var/spool/cron/ec2-user
to define crob task for ec2-user, or
echo "*/5 * * * * ..." >> /var/spool/cron/root
for root.

@chishiuanlu
Copy link

Thanks much for the info @Igawin. This works for me. :)
Also for whoever is trying to find related info and ending up here, using /var/spool/cron/ the user shouldn't be included in the syntax as it's already specified as the file name. Otherwise, the daemon will treat it as syntax error and ignore the task.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment