-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathcrontab usage
More file actions
85 lines (62 loc) · 2.54 KB
/
crontab usage
File metadata and controls
85 lines (62 loc) · 2.54 KB
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
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
20230503
https://stackoverflow.com/questions/48339383/crontab-does-not-run-my-python-script
https://www.jcchouinard.com/python-automation-with-cron-on-mac/
https://unix.stackexchange.com/questions/52330/how-to-redirect-output-to-a-file-from-within-cron
https://crontab.guru/#20_*_*_*_*
Above are severa link that I found could be useful.
The example of crontab job:
0 9 * * * /Path/to/pythonOrShell script >> f.log 2>&1 # only so the stdout and stderr could be correctly output to the f.log
Then you need to allow the privacy requirement of the cronjob.
>>> below is the code that I used to autodownload the picture from APVOD, and set it as the plot of my screen.
```
#!/usr/bin/env python3
import os
import requests
import datetime
import subprocess
import matplotlib.pyplot as plt
#import BeautifulSoup
# Define the URL of the Astronomy Picture of the Day (APOD) website
APOD_URL = "https://apod.nasa.gov/apod/"
# Create a directory to store the downloaded images
IMAGE_DIR = os.path.join(os.path.expanduser("~"), "Pictures", "APOD")
os.makedirs(IMAGE_DIR, exist_ok=True)
# Get the current date in YY-MM-DD format
today = datetime.date.today().strftime("%y-%m-%d")
print(datetime.datetime.now(), today)
# Construct the URL of the APOD image for today
image_url = APOD_URL + "ap" + today.replace("-", "") + ".html"
print(image_url)
# Send a request to the APOD website and get the HTML response
response = requests.get(image_url)
# Extract the URL of the APOD image from the HTML response
image_start = response.text.find('href="image/') + len('href="image/')
## extract the url of the picture today:
# try-1
image_suffix = ['.jpg"', '.png"', '.jpeg"']
for suffix in image_suffix:
image_end = response.text.find(suffix)
print(image_end)
if (image_end != -1) and (image_end > image_start) and (image_end < image_start + 100):
break
print('The postion of the image start and end',image_end, image_start)
image_path = response.text[image_start:image_end+4]
#print(image_path)
image_url = APOD_URL +'image/'+ image_path
#print(image_url)
# Download the image from the URL
response = requests.get(image_url)
# Save the image to the local directory
image_path = os.path.join(IMAGE_DIR, today + ".jpg")
with open(image_path, "wb") as f:
f.write(response.content)
# Display the image
# Set the downloaded image as the desktop background
SCRIPT = """/usr/bin/osascript<<END
tell application "Finder"
set desktop picture to POSIX file "%s"
end tell
END"""
subprocess.Popen(SCRIPT%image_path, shell=True)
print('Here, the crontab has runned the code')
```