-4

UbuntuサーバーをシャットダウンするプログラムをJavaで書いています。

 echo password | sudo -S shutdown -h now
 sudo -p 'password' shutdown -h now

どちらのコマンドも機能しませんでした。

パスワードを渡してサーバーをシャットダウンおよび再起動するには、sudo コマンドが必要です。私はプログラムからこれを行っているので、パスワードを要求する必要はありません。パスワードもコマンドとともに送信する必要があります。どんな提案も役に立ちます.....

4

1 に答える 1

1

sudo の -p オプションは、パスワードではなくプロンプトを指定します。-A オプションを見てください。sudo man ページからの抜粋:

 -A          Normally, if sudo requires a password, it will read it from the current 
             terminal.  If the -A (askpass) option is specified, a
             helper program is executed to read the user's password and output the 
             password to the standard output.  If the SUDO_ASKPASS environment variable 
             is set, it specifies the path to the helper program.  Otherwise, the value
             specified by the askpass  option in sudoers(5) is used.

ヘルパー プログラムは、正しいパスワードを出力する必要があります。したがって、ヘルパー プログラムを指定するには、次のようにします。

 export SUDO_ASKPASS="/path/to/helper"
于 2013-03-16T09:59:05.173 に答える