简体   繁体   English

Shell 脚本问题 使用 SSH 远程运行命令

[英]Shell Script Issue Running Command Remotely using SSH

I have a deploy script in which I want to clear the cache of my CDN.我有一个部署脚本,我想在其中清除 CDN 的缓存。 When I am on the server and run my script everything is fine, however when I SSH in and run only that file (ie not actually getting into the server, cd ing into the directory and running it) it fails and states the my doctl command cannot be found.当我在服务器上并运行我的脚本时,一切都很好,但是当我在 SSH 中并仅运行该文件时(即实际上没有进入服务器, cd进入目录并运行它)它失败并声明我的doctl命令找不到。 This seems to only be an issue with this program over ssh, running systemctl --help works fine.这似乎只是这个程序在 ssh 上的问题,运行 systemctl --help 工作正常。

Please note that I have installed Digital Ocean's doctl using sudo snap install doctl and it is there.请注意,我已经使用sudo snap install doctl安装了 Digital Ocean 的doctl ,它就在那里。

Here is the .sh file (minus comments):这是.sh文件(减去注释):

#!/bin/sh
doctl compute cdn flush [MYID]  --files [*]   # static cache

So I am not sure what the issue is.所以我不确定问题是什么。 Anybody have an idea?有人有想法吗?

Again, if I get into the server and run the file all works, but here is the SSH command I use that returns the error:同样,如果我进入服务器并运行文件一切正常,但这是我使用的 SSH 命令返回错误:

ssh root@123.45.678.999 "/deploy/clear_digital_ocean_cache.sh"

And here is the error.这是错误。

/deploy/clear_digital_ocean_cache.sh: 10: doctl: not found

Well one solution was to change the command to be an absolute path inside my .sh file like so:一种解决方案是将命令更改为我的.sh文件中的绝对路径,如下所示:

#!/bin/sh
/snap/bin/doctl compute cdn flush [MYID]  --files [*]   # static cache

I realized that I could run my user commands with ssh (like systemctl ) so it was either change where doctl was located (ie in the user bin) or ensure that the command was called with an absolute path adding the /snap/bin/ in front of the command.我意识到我可以使用ssh (如systemctl )运行我的用户命令,因此要么更改doctl的位置(即在用户 bin 中),要么确保使用添加/snap/bin/的绝对路径调用命令命令前面。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM