如何从crontab正确运行gsutil?

时间:2014-12-12 08:06:30

标签: google-cloud-storage gsutil

这是我在/ etc / crontab,CentOS 6.6中的条目:

0 0 */1 * * fredrik /home/fredrik/google-cloud-sdk/bin/gsutil -d -m rsync -r -C [src] [dst] &> [log]

我收到此错误:OSError: [Errno 13] Permission denied: '/.config'

如果在shell中执行,该命令运行正常。我注意到如果没有gsutil的完整路径我就无法运行0 0 */1 * * fredrik gsutil ...,所以我假设我在运行cron的环境中缺少某些东西......?

这是完整的追溯:

Traceback (most recent call last):
  File "/home/fredrik/google-cloud-sdk/bin/bootstrapping/gsutil.py", line 68, in <module>
    bootstrapping.PrerunChecks(can_be_gce=True)
  File "/home/fredrik/google-cloud-sdk/bin/bootstrapping/bootstrapping.py", line 279, in PrerunChecks
    CheckCredOrExit(can_be_gce=can_be_gce)
  File "/home/fredrik/google-cloud-sdk/bin/bootstrapping/bootstrapping.py", line 167, in CheckCredOrExit
    cred = c_store.Load()
  File "/home/fredrik/google-cloud-sdk/bin/bootstrapping/../../lib/googlecloudsdk/core/credentials/store.py", line 195, in Load
    account = properties.VALUES.core.account.Get()
  File "/home/fredrik/google-cloud-sdk/bin/bootstrapping/../../lib/googlecloudsdk/core/properties.py", line 393, in Get
    return _GetProperty(self, _PropertiesFile.Load(), required)
  File "/home/fredrik/google-cloud-sdk/bin/bootstrapping/../../lib/googlecloudsdk/core/properties.py", line 618, in _GetProperty
    value = callback()
  File "/home/fredrik/google-cloud-sdk/bin/bootstrapping/../../lib/googlecloudsdk/core/properties.py", line 286, in <lambda>
    'account', callbacks=[lambda: c_gce.Metadata().DefaultAccount()])
  File "/home/fredrik/google-cloud-sdk/bin/bootstrapping/../../lib/googlecloudsdk/core/credentials/gce.py", line 179, in Metadata
    _metadata_lock.lock(function=_CreateMetadata, argument=None)
  File "/usr/lib64/python2.6/mutex.py", line 44, in lock
    function(argument)
  File "/home/fredrik/google-cloud-sdk/bin/bootstrapping/../../lib/googlecloudsdk/core/credentials/gce.py", line 178, in _CreateMetadata
    _metadata = _GCEMetadata()
  File "/home/fredrik/google-cloud-sdk/bin/bootstrapping/../../lib/googlecloudsdk/core/credentials/gce.py", line 73, in __init__
    _CacheIsOnGCE(self.connected)
  File "/home/fredrik/google-cloud-sdk/bin/bootstrapping/../../lib/googlecloudsdk/core/credentials/gce.py", line 186, in _CacheIsOnGCE
    config.Paths().GCECachePath()) as gcecache_file:
  File "/home/fredrik/google-cloud-sdk/bin/bootstrapping/../../lib/googlecloudsdk/core/util/files.py", line 465, in OpenForWritingPrivate
    MakeDir(full_parent_dir_path, mode=0700)
  File "/home/fredrik/google-cloud-sdk/bin/bootstrapping/../../lib/googlecloudsdk/core/util/files.py", line 44, in MakeDir
    os.makedirs(path, mode=mode)
  File "/usr/lib64/python2.6/os.py", line 150, in makedirs
    makedirs(head, mode)
  File "/usr/lib64/python2.6/os.py", line 157, in makedirs
    mkdir(name, mode)
OSError: [Errno 13] Permission denied: '/.config'

5 个答案:

答案 0 :(得分:9)

感谢Mike和jterrace帮助我完成这项工作。最后,我不得不修改这些环境变量:PATH,HOME,BOTO_CONFIG(除了任何其他默认变量)。

PATH=/sbin:/bin:/usr/sbin:/usr/bin:/home/fredrik/google-cloud-sdk/bin
HOME=/home/fredrik
BOTO_CONFIG="/home/fredrik/.config/gcloud/legacy_credentials/[your-email-address]/.boto"

# Example of job definition:
# .---------------- minute (0 - 59)
# |  .------------- hour (0 - 23)
# |  |  .---------- day of month (1 - 31)
# |  |  |  .------- month (1 - 12) OR jan,feb,mar,apr ...
# |  |  |  |  .---- day of week (0 - 6) (Sunday=0 or 7) OR sun,mon,tue,wed,thu,fri,sat
# |  |  |  |  |
# *  *  *  *  * user-name command to be executed

  0  0 */1 *  * fredrik   gsutil -d -m rsync -r -C /local-folder/ gs://my-bucket/my-folder/ > /logs/gsutil.log 2>&1

> gsutil.log 2>&1将stdout和stderr管道连接到同一文件。此外,它将在下次运行gsutil时覆盖日志文件。要使其附加到日志文件,请使用>> gsutil.log 2>&1。这在Linux和OS X上应该是安全的。

我注意到调试标志-d在大数据量上创建了大量日志文件,因此我个人可能会选择退出该标志。

答案 1 :(得分:3)

从cron运行时,你可能会得到一个不同的boto配置文件。请尝试以下两种方式运行(以root身份,然后通过cron),看看是否为这两种情况获得了不同的配置文件列表:

gsutil -D ls 2>&1 | grep config_file_list   

发生这种情况的原因是cron在运行作业之前取消了大多数环境变量,所以你需要在运行gsutil之前在你的cron脚本中手动设置BOTO_CONFIG环境变量,即:

BOTO_CONFIG="/root/.boto"
gsutil rsync ...

答案 2 :(得分:3)

我相信您收到此错误,因为在cron下运行时未设置HOME环境变量。尝试设置HOME=/home/fredrik

答案 3 :(得分:0)

由于cron在非常有限的环境中运行,因此您需要获取.bash_profile来获取环境配置。

* * * * * source ~/.bash_profile && your_cmd_here

答案 4 :(得分:-1)

对于任何尝试使用PHP运行Apache的gsutil管理图像的人 -

创建一个名为 apache-shared 的新目录和 chgrp / chown&#39; d www-data(或者运行Apache的任何用户,运行&#34;顶部&#34;检查)。将.boto文件复制到目录中并运行以下内容而不会出现问题:

shell_exec('export BOTO_CONFIG=/apache-shared/.boto && export PATH=/sbin:/bin:/usr/sbin:/usr/bin:/home/user/google-cloud-sdk/bin && gsutil command image gs://bucket');