Server Fault is a question and answer site for system and network administrators. Join them; it only takes a minute:

Sign up
Here's how it works:
  1. Anybody can ask a question
  2. Anybody can answer
  3. The best answers are voted up and rise to the top

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

#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]*`

#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
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


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.