【问题标题】:How to run shell script with sudo inside through nohup on remote machine如何在远程机器上通过 nohup 运行带有 sudo 的 shell 脚本
【发布时间】:2016-02-03 03:21:45
【问题描述】:

我从本地机器运行了以下命令:

ssh -i key remote_host "nohup sh test.sh > nohup.out 2> nohup.err < /dev/null &"

然后我得到错误:sudo: sorry, you must have a tty to run sudo

我添加了-tt 选项:

ssh -tt -i key remote_host "nohup sh test.sh > nohup.out 2> nohup.err < /dev/null &"

我检查了遥控器,test.sh 没有运行(没有进程 ID)。

我取出nohup,一切正常,ssh -tt -i key remote_host "sh test.sh" 但我需要使用nohup。有人能帮我吗?非常感谢!

一个remote_host:test.sh脚本:

#!/bin/bash
sudo iptables -A OUTPUT -p tcp --dport 443 -j DROP
sleep 30
sudo iptables -D OUTPUT -p tcp --dport 443 -j DROP

【问题讨论】:

  • 可能更适合服务器故障论坛。
  • 我使用 -i 密钥文件来 ssh,它不会要求我输入密码。

标签: bash


【解决方案1】:

sudo 可能试图提示您输入密码。你需要在你的 remote_host 的 sudoers 文件中设置 NOPASSWD 或者你可以使用expect

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2023-03-10
    • 2017-02-28
    • 1970-01-01
    相关资源
    最近更新 更多