gpt4 book ai didi

linux - 在 shell 脚本中设置环境变量以在远程服务器上运行

转载 作者:太空狗 更新时间:2023-10-29 12:29:26 26 4
gpt4 key购买 nike

我有一个脚本可以在 Hive 中自动运行查询。我可以将脚本留在本地机器上,也可以将其放在网关机器上。如果我在远程机器上有它,我可以像这样运行 update_table

ssh user@gatewaymachinexyz.com ‘./update_table.sh'

但是,当我这样做时,它无法识别在远程机器上设置的环境变量。如果我通过 ssh 登录到远程机器,然后运行 ​​./update_table.sh 它运行良好。从本地计算机运行时如何让它识别环境变量?

最佳答案

当使用ssh 指定命令时,ssh 默认不会为 session 分配伪终端(tty)。因此,(可能)获得了一组不同的启动脚本。和/或根据 TERM 环境变量的存在/不存在,采用脚本中的不同分支。

参见 ssh man :

When the user's identity has been accepted by the server, the server either executes the given command in a non-interactive session or, if no command has been specified, logs into the machine and gives the user a normal shell as an interactive session.


要强制 ssh 分配 tty,请使用 -t 开关:

ssh -t user@gatewaymachinexyz.com ‘./update_table.sh'     

参见 ssh man :

-t Force pseudo-terminal allocation. This can be used to execute arbitrary screen-based programs on a remote machine, which can be very useful, e.g. when implementing menu services. Multiple -t options force tty allocation, even if ssh has no local tty.


虽然不是通过强制分配伪终端来解决这个问题,您应该更好地修复您的启动脚本,为所有 session 设置相同的环境。

关于linux - 在 shell 脚本中设置环境变量以在远程服务器上运行,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/32173216/

26 4 0
文章推荐: html - 是否可以防止 H2 标签后换行?
文章推荐: CSS - 为所有 div 添加边框
文章推荐: css -
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com