通过Cron执行gsutil cp命令时遭遇‘OSError: Operation not permitted’错误的求助
通过Cron执行gsutil cp命令时遭遇‘OSError: Operation not permitted’错误的求助
各位好,我遇到了一个十分棘手的问题:我写了一个用于备份的PHP脚本,在终端里手动运行完全正常,但通过Cron执行时,脚本里的第一个命令就报错终止了。
我特意做了测试,把Cron任务设置为下一分钟执行,确认机器处于活跃状态,外接硬盘也正常挂载且能正常访问,但脚本依旧执行失败。
执行的命令
我在脚本里执行的是这条gsutil cp命令:
/Users/kenny/google-cloud-sdk/bin/gsutil cp \ gs://backups.mydomain.com/sql/mydomain-db.2023-06-17-1687023004.sql.gz \ /Users/kenny/extdrive/backup.mydomain/backups/2023/sql/2023-06-17/mydomain-db.2023-06-17-1687023004.sql.gz \ 2>&1
日志输出
日志里显示的内容如下:
Copying gs://backups.herdboss.com/sql/herdboss-db.2023-06-17-1687023004.sql.gz...
==> NOTE: You are downloading one or more large file(s), which would run significantly faster if you enabled sliced object downloads. This feature is enabled by default but requires that compiled crcmod be installed (see "gsutil help crcmod").
OSError: Operation not permitted.
ERROR: COMMAND EXIT CODE: 1
已排查的内容
- 针对日志里提到的crcmod提示:我已经按照
gsutil help crcmod的说明完成了crcmod的安装和配置,检查后确认配置是正常的。 - 能确定终端和Cron的执行环境存在差异,但暂时找不到具体的差异点在哪里。
相关PHP代码片段
我的PHP脚本中执行命令的部分代码如下:
$cmd = "$gsutil cp $gspath $backupdir/$filename"; execandlog($cmd); function execandlog(string $command, string $inputtext = null, bool $shouldhorkonerrorexitcode = true, int &$exitcode = null): ?string { $fullcommand = "$command 2>&1"; logmesg($fullcommand); $descriptors = [ 0 => ($inputtext === null) ? STDIN : ['pipe', 'r'], 1 => ['pipe', 'w'], 2 => STDERR, ]; $pipes = []; $process = proc_open($fullcommand, $descriptors, $pipes); $output = null; if (is_resource($process)) { if ($inputtext !== null) { if (false === fwrite($pipes[0], $inputtext)) { logmesg("ERROR: UNABLE TO WRITE TEXT TO INPUT PIPE"); exit(-1); } fclose($pipes[0]); } $output = ''; $commandoutputstream = $pipes[1]; while (false === feof($commandoutputstream)) { $line = trim(fgets($commandoutputstream, 4096)); $output .= "$line\n"; logmesg($line); } fclose($commandoutputstream); $status = proc_get_status($process); $exitcode = $status['exitcode']; proc_close($process); if ($exitcode !== 0) { logmesg("ERROR: COMMAND EXIT CODE: $exitcode"); if ($shouldhorkonerrorexitcode) { exit(-1); } } } return $output; }
有没有大佬能帮我分析下问题出在哪里?感激不尽!
备注:内容来源于stack exchange,提问作者Kenny Wyland
相关产品推荐
相关产品推荐

