I am trying to run this bash script as a cron job, but I am getting an error which says
sudo: sorry, you must have a tty to run sudo
. I am running this bash script in amazon EC2 linux instance.
#!/bin/bash -
#get instance id - used for putting metric
INSTANCE_ID=`GET http://169.254.169.254/latest/meta-data/instance-id`
#could be done using "free" or "vmstat" - use of less and grep is believed to provide widest compatibility - CJ 2011-11-24
memfree=`less /proc/meminfo | grep -i MemFree | grep -o [0-9]*`
swaptotal=`less /proc/meminfo | grep -i SwapTotal | grep -o [0-9]*`
swapfree=`less /proc/meminfo | grep -i SwapFree | grep -o [0-9]*`
swapused=$(($swaptotal-$swapfree))
#mon-put-data to put metrics
mon-put-data --metric-name MemoryFree --namespace EC2/Memory --value $memfree --unit Kilobytes --dimensions "InstanceId=$INSTANCE_ID"
mon-put-data --metric-name SwapUsed --namespace EC2/Memory --value $swapused --unit Kilobytes --dimensions "InstanceId=$INSTANCE_ID"
Kindly help me out to figure this out.
In sudoers config file, there is an option - requiretty - which will prevent you from running sudo without being logged as a real user.
The "requiretty" option may solve your problem.
Why not just install the cron job under the the user that you are attempting to sudo to?