-
Notifications
You must be signed in to change notification settings - Fork 0
/
cronJob.py
31 lines (25 loc) · 1.27 KB
/
cronJob.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
# To write a cron job in Python to fetch files at a specific time, you can use the built-in cron module in Python.
#Here's an example script that fetches files every day at 3:00 am:
import os
import datetime
import requests
def fetch_files():
# Replace this URL with the URL of the files you want to fetch
url = "https://example.com/files"
response = requests.get(url)
# Replace the path with the directory where you want to save the files
save_path = "/home/user/files/"
# Create the directory if it doesn't exist
if not os.path.exists(save_path):
os.makedirs(save_path)
# Save the files with the current date and time as the filename
filename = datetime.datetime.now().strftime("%Y-%m-%d_%H:%M:%S") + ".txt"
with open(save_path + filename, "wb") as f:
f.write(response.content)
if _name_ == '_main_':
fetch_files()
# To schedule this script to run every day at 3:00 am using cron, follow these steps:
# Open your crontab file by running crontab -e in your terminal.
# Add the following line to the end of the file: 0 3 * * * /usr/bin/python3 /path/to/your/script.py
# Save the file and exit.
# This will run your script every day at 3:00 am. Make sure to replace /path/to/your/script.py with the actual path to your script.