Monday, August 1, 2016

linux - sudo: sorry, you must have a tty to run sudo



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.


Answer



In sudoers config file, there is an option - requiretty - which will prevent you from running sudo without being logged as a real user.




see sudoers(5) man page

No comments:

Post a Comment

linux - How to SSH to ec2 instance in VPC private subnet via NAT server

I have created a VPC in aws with a public subnet and a private subnet. The private subnet does not have direct access to external network. S...