Take the 2-minute tour ×
Server Fault is a question and answer site for professional system and network administrators. It's 100% free, no registration required.

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.

share|improve this question

2 Answers 2

up vote 8 down vote accepted

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
share|improve this answer

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?

share|improve this answer

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.