简体   繁体   English

使用 SSH 连接和启动脚本运行 bash 脚本时出现不同的行为

[英]Getting different behavior when running bash script using SSH connection and startup-script

I am trying to run a script which is running on GCP startup-script, my startup script looks like below:我正在尝试运行在 GCP 启动脚本上运行的脚本,我的启动脚本如下所示:

#!/bin/bash
ulimit -n 100000
source ~/miniconda3/etc/profile.d/conda.sh
conda activate base
set -e
/root/miniconda3/bin/python3.8 /root/spider/src/rotate_ip.py & /root/miniconda3/bin/python3.8 /root/spider/src/main.py
gcloud compute instances stop scheduled-spider --zone asia-northeast1-b

This script behavior is not same as when I run the program connecting with SSH. It also doesn't show any log error.此脚本行为与我运行与 SSH 连接的程序时的行为不同。它也没有显示任何日志错误。 But when I run the program connecting SSH, it works perfectly.但是当我运行连接 SSH 的程序时,它运行得很好。 The way I run the program in SSH connection:我在SSH连接运行程序的方式:

/root/miniconda3/bin/python3.8 /root/spider/src/rotate_ip.py & /root/miniconda3/bin/python3.8 /root/spider/src/main.py

My assumption is when I run the program from startup script, it's probably not getting the same environment as in SSH connection.我的假设是当我从启动脚本运行程序时,它可能不会获得与 SSH 连接相同的环境。 Though I am using the following commands in the startup script to get the same environment but it's not working the same way.虽然我在启动脚本中使用以下命令来获得相同的环境,但它的工作方式不同。

source ~/miniconda3/etc/profile.d/conda.sh
conda activate base

In ssh connection: which bash command gives me /usr/bin/bash .在 ssh 连接中: which bash命令给我/usr/bin/bash I have also tried this shebang, but no result.我也尝试过这个 shebang,但没有结果。 Does anyone have any clue what else to try?有谁知道还有什么可以尝试的吗?

I have also tried something like below:我也尝试过类似下面的东西:

source ~/miniconda3/etc/profile.d/conda.sh
conda activate base
set -e
conda activate base && /root/miniconda3/bin/python3.8 /root/spider/src/rotate_ip.py & /root/miniconda3/bin/python3.8 /root/spider/src/main.py

But no output difference.但没有 output 的区别。

Were I doing this, I would use conda run and not muck around with manual activation.如果我这样做,我会使用conda run而不是手动激活。

#!/bin/bash
ulimit -n 100000
set -e
conda run -n base python /root/spider/src/rotate_ip.py & 
conda run -n base python /root/spider/src/main.py
gcloud compute instances stop scheduled-spider --zone asia-northeast1-b

If it needs interaction, then you may need additional flags (see conda run --help ).如果它需要交互,那么您可能需要额外的标志(请参阅conda run --help )。

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

相关问题 在 GCP 中运行修改后的启动脚本时出现问题 - Problem running a modified startup-script in GCP 如何在 GCP 中容器操作系统镜像的启动脚本中使用 gsutil? - How to use gsutil in startup-script on container-os image in GCP? 如何从 GCE 实例中的 Container-optimized OS 获取启动脚本日志? - How do I get startup-script logs from Container-optimized OS in a GCE instance? 启动脚本不在实例中运行 - Startup script NOT running in instance 谷歌云启动脚本未运行 - Google Cloud Startup Script Not Running SSH ProxyCommand 使用 aws SSM session 管理器和 bash 脚本与 select 命令 - SSH ProxyCommand using aws SSM session manager and bash script with select command 获取 /bin/bash^M:错误的解释器:在 Dataproc 上运行初始化脚本后没有这样的文件或目录 - Getting /bin/bash^M: bad interpreter: No such file or directory after running initialisation script on Dataproc Azure 启动脚本未执行 - Azure startup script is not executed metadata_startup_script 未使用 Terraform 脚本在 VM 实例 GCP 内安装 sql 服务器代理 - metadata_startup_script is not installing sql server proxy inside the VM instance GCP using Terraform script Bash 脚本设置环境变量在使用源时不起作用,仅适用于 bash -c - Bash script to set env variables does not work when using source and only works with bash -c
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM