简体   繁体   English

是否有一种编程方式来区分 bash 脚本是由用户执行还是通过 PHP 中的 shell_exec 执行?

[英]Is there a programmatic way to distinguish whether bash script was executed by user and via shell_exec in PHP?

I've written simple DB backup script that does the job with no issues whatsoever, but to avoid embedding any credentials, I pass everything as arguments.我编写了简单的 DB 备份脚本,可以毫无问题地完成这项工作,但为了避免嵌入任何凭据,我将所有内容作为参数传递。

private function dumpDatabase(): void
{
    $this->fileSystem->disk('local')->makeDirectory('temp');

    $relativePathToDump = 'storage/app/temp/dump.sql';

    $executableChunks = [
        'HOST=' . config('database.connections.mysql.host'),
        'PORT=' . config('database.connections.mysql.port'),
        'USER=' . config('database.connections.mysql.username'),
        'PASSWORD=' . config('database.connections.mysql.password'),
        'DATABASE_NAME=' . config('database.connections.mysql.database'),
        'RELATIVE_PATH_TO_DUMP=' . $relativePathToDump,
        base_path('bin/backup_database.sh'),
    ];

    $command = implode(' ', $executableChunks);

    shell_exec($command);
}

The script itself:脚本本身:

#!/usr/bin/env bash

mysqldump --force --routines -h $HOST --port=$PORT -u $USER -p$PASSWORD $DATABASE_NAME > $RELATIVE_PATH_TO_DUMP

Is there a way to immediately stop the script's execution if it was triggered by user manually via:如果用户通过以下方式手动触发脚本,是否可以立即停止脚本的执行:

./bin/backup_database.sh

but let it do the job when called via:但是当通过以下方式调用时让它完成工作:

shell_exec($command);

I could add more checks to see if arguments are present:我可以添加更多检查以查看是否存在参数:

if [[ -z "${HOST}" ]]; then
  echo "Database host (HOST=) has not been provided."
  exit 1
fi

but this is not about arguments but about who / what is the "executee" of the script.但这不是关于参数,而是关于谁/什么是脚本的“执行者” Is it doable?可行吗?

if [[ not triggered programatically by shell_exec ]]; then
  echo "Bye"
  exit 1
fi

You can look at the name of the parent process (see Get the name of the caller script in bash script ).您可以查看父进程的名称(请参阅在 bash 脚本中获取调用者脚本的名称)。 Look for php in the PARENT_COMMAND .PARENT_COMMAND查找php

caller=$(ps -o comm= $PPID)
if [[ "${caller}" =~ php ]]; then
   echo "Bingo"
fi

暂无
暂无

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

相关问题 通过php shell_exec执行bash脚本的权限错误 - Permission error by execute bash script via php shell_exec 通过PHP的shell_exec()从网页执行bash脚本,这需要另一个用户的权限吗? - Executing a bash script from a web page via PHP's shell_exec(), which requires another user's perms? Python 'No module named ...' 当 php 使用 shell_exec() 执行脚本时 - Python 'No module named …' when script is executed by php with shell_exec() 更改用户并通过 PHP shell_exec() function (Apache/Ubuntu) 在远程服务器上运行 bash 命令 - Change user and run bash command on remote server via PHP shell_exec() function (Apache/Ubuntu) Php、bash、sshpass、scp 无法通过 shell_exec 工作 - Php, bash, sshpass, scp is not working via shell_exec Bash 在 PHP 中使用 shell_exec 调用的脚本永远不会完成 - Bash script called with shell_exec in PHP never finish 使用zenity的bash脚本在终端中工作但不在php shell_exec中工作 - bash script using zenity works in terminal but not in php shell_exec (PHP) 将 shell_exec() 中的多个参数传递给 bash 脚本 - (PHP) Passing multiple parameters within shell_exec() to bash script 通过PHP调用Python脚本时将挂起shell_exec() - Python Script Hangs When Called via PHP shell_exec() c ++程序无法通过php脚本(例如exec(),shell_exec(),system()等命令)执行 - c++ program not getting executed using php script with commands like exec(),shell_exec(),system()
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM