【问题标题】:Can't schedule python script with crontab无法使用 crontab 安排 python 脚本
【发布时间】:2020-04-29 17:33:06
【问题描述】:

我有这个我在 anaconda 中做的 python 脚本,并以 .py 格式下载到我的本地工作区

#!/usr/bin/env python
# coding: utf-8

# In[33]:


#!/usr/bin/env python
#
# Copyright 2016 Google Inc. All Rights Reserved.
#
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
#      http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.

"""This example downloads a criteria performance report as a string with AWQL.

To get report fields, run get_report_fields.py.

The LoadFromStorage method is pulling credentials and properties from a
"googleads.yaml" file. By default, it looks for this file in your home
directory. For more information, see the "Caching authentication information"
section of our README.

"""


from googleads import adwords
import io
import pandas as pd


adwords_client = adwords.AdWordsClient.LoadFromStorage()

  # Initialize appropriate service.
report_downloader = adwords_client.GetReportDownloader(version='v201809')

  # Create report query.
report_query = (adwords.ReportQueryBuilder()
                .Select('CampaignId', 'AdGroupId', 'Id', 'Criteria',
                          'CriteriaType', 'FinalUrls', 'Impressions', 'Clicks',
                          'Cost')
                .From('CRITERIA_PERFORMANCE_REPORT')
                .Where('Status').In('ENABLED', 'PAUSED')
                .During('LAST_7_DAYS')
                .Build())
    
output = io.StringIO()
    
report_downloader.DownloadReportWithAwql(
      report_query, 'CSV', output, skip_report_header=True,
      skip_column_header=False, skip_report_summary=True,
      include_zero_impressions=True)

output.seek(0)


df = pd.read_csv(output)
    
print(df.head())


# In[44]:


df.to_csv("/Users/ezerivarola/Desktop/Google_ADS_API/report1.csv",index=False)


# In[ ]:

我正在尝试使用 crontab 使用以下命令来安排它:

* * * * * /usr/local/bin/python3 /Users/ezerivarola/Desktop/Google_ADS_API/Report1_DF.py

但是,虽然我没有收到任何错误,并且在查看邮件时我看到它正在运行,但没有生成脚本的 csv 文件。

有没有人知道这可能是什么问题?

【问题讨论】:

  • 如果您从终端手动运行它,它会运行吗? /usr/local/bin/python3 /Users/ezerivarola/Desktop/Google_ADS_API/Report1_DF.py
  • 是的,完美执行
  • 您是否尝试过在 cron 命令中指定用户?就像是。 * * * * * 用户 /usr/local/bin/python3 /Users/ezerivarola/Desktop/Google_ADS_API/Report1_DF.py
  • 我试过这个:* * * * * ezerivarola /usr/local/bin/python3 /Users/ezerivarola/Desktop/Google_ADS_API/Report1_DF.py。但什么也没发生

标签: python cron schedule


【解决方案1】:

您需要更改开头的 5 个 * 以匹配您希望它运行的时间段,

# ┌───────────── minute (0 - 59)
# │ ┌───────────── hour (0 - 23)
# │ │ ┌───────────── day of the month (1 - 31)
# │ │ │ ┌───────────── month (1 - 12)
# │ │ │ │ ┌───────────── day of the week (0 - 6) (Sunday to Saturday;
# │ │ │ │ │                                   7 is also Sunday on some systems)
# │ │ │ │ │
# │ │ │ │ │
# * * * * * command to execute

下面将每小时运行一次,

0 * * * * /usr/local/bin/python3 /Users/ezerivarola/Desktop/Google_ADS_API/Report1_DF.py

【讨论】:

  • 我明白这一点,但我正在使用 * * * * * 每分钟刷新一次数据以测试它是否有效。
【解决方案2】:

我已经解决了我的问题。 Crontab 没有执行 python 脚本,因为它无权访问磁盘。我留下一个链接,提供更多解决方案的详细信息:https://blog.bejarano.io/fixing-cron-jobs-in-mojave/

谢谢大家

【讨论】:

    猜你喜欢
    • 2019-08-28
    • 2021-05-25
    • 1970-01-01
    • 2016-06-18
    • 2021-09-26
    • 1970-01-01
    • 2015-08-18
    • 2016-03-06
    • 2012-10-26
    相关资源
    最近更新 更多