简体   繁体   English

SSH 脚本在手动运行时运行良好,但在远程使用 PHP 执行时无法正常运行

[英]SSH Script runs fine manually running it but does not when exec with PHP remotely

Summary概括

I've created a shell script that is uploaded via SFTP using the phpseclib3 library.我创建了一个 shell 脚本,该脚本使用phpseclib3库通过 SFTP 上传。 The shell script is being executed, but all of the apt packages are not being installed when running the script using the below PHP Code.正在执行 shell 脚本,但使用以下 PHP 代码运行脚本时,未安装所有apt包。 It installs some packages, but hangs up on one somewhere.它安装了一些软件包,但在某个地方挂断了。 However, if I use FileZilla to upload the file using the same user, and then login to execute the same script all packages are being installed correctly.但是,如果我使用 FileZilla 使用同一用户上传文件,然后登录以执行相同的脚本,则所有软件包都已正确安装。 The output is not showing any errors either. output 也没有显示任何错误。

It's almost like the packages are all trying to be created once which is locking the packages up and causing an error that I can't find.几乎就像所有包都试图创建一次,这会锁定包并导致我找不到的错误。

Things I've tried我尝试过的事情

  • Have the script run silently让脚本静默运行
  • Made sure that PHP isn't timing out确保 PHP 没有超时
  • Used a different SSH composer package使用不同的 SSH 作曲家 package
  • Splitting the apt install into individual items instead of one long list将 apt 安装拆分为单个项目,而不是一个长列表
  • Added and removed sudo true to the top of the script在脚本顶部添加和删除sudo true
  • Tried to re-run apt install multiple times尝试多次重新运行apt install
  • Use apt-get instead of apt使用apt-get代替apt

PHP Code PHP 代码

$ssh = new SSH2($ip_address);
if (!$ssh->login('login', $key)) {
  // error handling
}

$ssh->setTimeout(0);
$ssh->setKeepAlive(10);

$process[] = $ssh->exec('chmod +x install.sh; sh install.sh;');

Shell Example Shell 示例

#!/bin/bash

sudo true

sudo dpkg --add-architecture i386

sudo apt update

sudo apt -y install curl wget file tar bzip2 gzip unzip bsdmainutils python util-linux ca-certificates binutils bc jq tmux netcat lib32gcc1 lib32stdc++6

echo steam steam/license note '' | sudo debconf-set-selections && echo steam steam/question select 'I AGREE' | sudo debconf-set-selections && sudo apt -y install steamcmd

wget -O linuxgsm.sh https://linuxgsm.sh && chmod +x linuxgsm.sh && bash linuxgsm.sh squadserver

yes Y | ./squadserver install

touch done.txt

Error Log错误日志

 sudo true
+ sudo dpkg --add-architecture i386
+ sudo apt update

WARNING: apt does not have a stable CLI interface. Use with caution in scripts.

Hit:1 http://security.ubuntu.com/ubuntu bionic-security InRelease
Get:2 http://mirrors.digitalocean.com/ubuntu bionic InRelease [242 kB]
Hit:3 http://mirrors.digitalocean.com/ubuntu bionic-updates InRelease
Hit:4 http://mirrors.digitalocean.com/ubuntu bionic-backports InRelease
Get:5 http://security.ubuntu.com/ubuntu bionic-security/main i386 Packages [902 kB]
Get:6 http://security.ubuntu.com/ubuntu bionic-security/restricted i386 Packages [13.5 kB]
Get:7 http://security.ubuntu.com/ubuntu bionic-security/universe i386 Packages [973 kB]
Get:8 http://security.ubuntu.com/ubuntu bionic-security/multiverse i386 Packages [9208 B]
Get:9 http://mirrors.digitalocean.com/ubuntu bionic/main i386 Packages [1007 kB]
Get:10 http://mirrors.digitalocean.com/ubuntu bionic/restricted i386 Packages [9156 B]
Get:11 http://mirrors.digitalocean.com/ubuntu bionic/universe i386 Packages [8531 kB]
Get:12 http://mirrors.digitalocean.com/ubuntu bionic/multiverse i386 Packages [144 kB]
Get:13 http://mirrors.digitalocean.com/ubuntu bionic-updates/main i386 Packages [1197 kB]
Get:14 http://mirrors.digitalocean.com/ubuntu bionic-updates/restricted i386 Packages [20.2 kB]
Get:15 http://mirrors.digitalocean.com/ubuntu bionic-updates/universe i386 Packages [1555 kB]
Get:16 http://mirrors.digitalocean.com/ubuntu bionic-updates/multiverse i386 Packages [12.7 kB]
Get:17 http://mirrors.digitalocean.com/ubuntu bionic-backports/main i386 Packages [10.0 kB]
Get:18 http://mirrors.digitalocean.com/ubuntu bionic-backports/universe i386 Packages [10.3 kB]
Fetched 14.6 MB in 11s (1370 kB/s)
Reading package lists...
Building dependency tree...
Reading state information...
74 packages can be upgraded. Run 'apt list --upgradable' to see them.
+ sudo apt -y install curl wget file tar bzip2 gzip unzip bsdmainutils python util-linux ca-certificates binutils bc jq tmux netcat lib32gcc1 lib32stdc++6

WARNING: apt does not have a stable CLI interface. Use with caution in scripts.

E: Could not get lock /var/lib/dpkg/lock-frontend - open (11: Resource temporarily unavailable)
E: Unable to acquire the dpkg frontend lock (/var/lib/dpkg/lock-frontend), is another process using it?
+ sudo debconf-set-selections
+ echo steam steam/license note ''
+ sudo debconf-set-selections
+ echo steam steam/question select 'I AGREE'
+ sudo apt -y install steamcmd

WARNING: apt does not have a stable CLI interface. Use with caution in scripts.

E: Could not get lock /var/lib/dpkg/lock-frontend - open (11: Resource temporarily unavailable)
E: Unable to acquire the dpkg frontend lock (/var/lib/dpkg/lock-frontend), is another process using it?

The issue was coming from this portion of the code:问题来自这部分代码:

sudo dpkg --add-architecture i386

sudo apt update

For whatever reason it was creating a lock file which was killing the other processes after it.无论出于何种原因,它正在创建一个锁定文件,该文件正在杀死它之后的其他进程。 To fix it, I just removed the lock and reconfigured dpkg by adding this portion of code after the update.为了修复它,我刚刚删除了锁并通过在更新后添加这部分代码来重新配置 dpkg。

sudo rm /var/lib/apt/lists/lock
sudo rm /var/cache/apt/archives/lock
sudo rm /var/lib/dpkg/lock*

sudo dpkg --configure -a

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

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