简体   繁体   English

如何在bash启动脚本中通过SCP传输文件?

[英]How to transfer files via SCP in a bash boot script?

I'm using Linode to deploy Ubuntu 18.04LTS VMs. 我正在使用Linode来部署Ubuntu 18.04LTS VM。 The following script is executed after boot-up (Linode calls it a "stackscript"). 启动后执行以下脚本(Linode将其称为“stackscript”)。 It works up and including to the point of installing java. 它的工作原理包括安装java。 The SCP command seems not to work. SCP命令似乎不起作用。 If I execute the commands manually on the booted VM it works like a charm. 如果我在启动的VM上手动执行命令,它就像一个魅力。 What's the problem? 有什么问题?

#!/bin/bash

#store ip
export nodeManagerIp=999.999.99.999

#install tools
sudo apt-get update
sudo apt-get --assume-yes install sshpass
sudo apt-get --assume-yes install openjdk-11-jre-headless

#add node manager to known hosts
ssh-keyscan $nodeManagerIp >> ~/.ssh/known_hosts

#get resources
sshpass -p 'thePassword' scp root@$nodeManagerIp:/opt/* /opt/

##start server##
#as many threads as cores
threads=$(cat /proc/cpuinfo | grep processor | wc -l)
#start server
java -cp app-1.0.0-SNAPSHOT-jar-with-dependencies.jar de.wipj.application.Server threads=$threads

Here is the log. 这是日志。 I ran the script as root. 我以root身份运行脚本。 The problem is with ssh-keyscan. 问题在于ssh-keyscan。 Why does it work when I execute the line manually on the VM? 当我在VM上手动执行该行时,为什么它可以工作?

ding debian:Secure_Global_CA.pem
Adding debian:AffirmTrust_Premium.pem
Adding debian:USERTrust_RSA_Certification_Authority.pem
Adding debian:OISTE_WISeKey_Global_Root_GB_CA.pem
Adding debian:SSL.com_EV_Root_Certification_Authority_ECC.pem
Adding debian:GDCA_TrustAUTH_R5_ROOT.pem
Adding debian:Certum_Trusted_Network_CA.pem
Adding debian:AffirmTrust_Premium_ECC.pem
Adding debian:ePKI_Root_Certification_Authority.pem
Adding debian:QuoVadis_Root_CA_2.pem
Adding debian:USERTrust_ECC_Certification_Authority.pem
Adding debian:TWCA_Root_Certification_Authority.pem
Adding debian:COMODO_RSA_Certification_Authority.pem
Adding debian:GlobalSign_Root_CA_-_R3.pem
Adding debian:D-TRUST_Root_Class_3_CA_2_2009.pem
done.
Processing triggers for libc-bin (2.27-3ubuntu1) ...
Processing triggers for ureadahead (0.100.0-21) ...
Processing triggers for systemd (237-3ubuntu10.21) ...
Processing triggers for ca-certificates (20180409) ...
Updating certificates in /etc/ssl/certs...
0 added, 0 removed; done.
Running hooks in /etc/ca-certificates/update.d...

done.
done.
+ sudo ssh-keyscan 999.999.99.999
/root/StackScript: line 13: /root/.ssh/known_hosts: No such file or directory
+ sudo sshpass -p thePassword scp 'root@999.999.99.999:/opt/*' /opt/
++ wc -l
++ grep processor
++ cat /proc/cpuinfo
+ export threads=1
+ threads=1
+ sudo java -cp /opt/app-1.0.0-SNAPSHOT-jar-with-dependencies.jar de.wipj.application.Server threads=5
Error: Could not find or load main class de.wipj.application.Server
Caused by: java.lang.ClassNotFoundException: de.wimj.application.Server

From the error messages: you don't have a home for root in initrd. 从错误消息:您在initrd中没有root用户。

Inside the initrd I prefer to fix this by export HOME=/ . 在initrd中我更喜欢通过export HOME=/来解决这个问题。 Now you have a home. 现在你有了一个家。 For good measure, ensuring /etc/password has / for root's home in initrd is a good idea. 为了更好地衡量,确保/ etc / password具有/在initrd中的root用户是一个好主意。

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

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