Linux

sudo:對不起,你必須有一個 tty 才能執行 sudo

  • January 11, 2012

我正在嘗試將此 bash 腳本作為 cron 作業執行,但我收到一條錯誤消息,提示 sudo: sorry, you must have a tty to run sudo. 我在亞馬遜 EC2 linux 實例中執行這個 bash 腳本。

#!/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"

請幫我解決這個問題。

在 sudoers 配置文件中,有一個選項 - requiretty - 它將阻止您在沒有以真實使用者身份登錄的情況下執行 sudo。

see sudoers(5) man page

“requiretty”選項可能會解決您的問題。

為什麼不直接在您嘗試 sudo 的使用者下安裝 cron 作業?

引用自:https://serverfault.com/questions/348753