繁体   English   中英

无法使用 crontab 安排 python 脚本

[英]Can't schedule python script with crontab

我有这个 python 脚本,我在 anaconda 中执行并下载到我的本地工作区 as.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 文件。

有谁知道这可能是错的吗?

您需要更改开头的 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

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

谢谢你们

暂无
暂无

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

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM