[英]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.