如何在Windows系统用Python实现脚本每30分钟重复运行?
Hey there! Let's figure out how to get your screen automation script (clicking, screenshotting, sending emails to Gmail) running every 30 minutes automatically. The solution depends on your operating system, so I'll walk you through the most common options below:
Windows: Use Task Scheduler
- Open Task Scheduler and click "Create Basic Task"
- Give your task a descriptive name (like "Auto Screenshot & Email") and click "Next"
- For the trigger, select "Daily", then set your start time. Check the box for "Repeat task every" and choose "30 minutes", set "for a duration of" to "Indefinitely", then click "Next"
- For the action, select "Start a program". If your script is a Python file, browse to your Python executable (e.g.,
C:\Python311\python.exe), then in "Add arguments", paste the full path to your script (likeC:\scripts\my_automation_script.py). If it's a batch file, just browse directly to the.batfile - Critical settings: Under "Security options", check "Run whether user is logged on or not" and "Run with highest privileges"—this avoids permission issues when the script tries to interact with the screen or send emails
- Review your settings and click "Finish"
Linux/macOS: Use Cron
Cron is the built-in job scheduler for Unix-like systems, and it's perfect for this use case:
- Open your terminal and run
crontab -eto edit your user's cron jobs - Add this line at the end of the file (replace paths with your actual script/executable paths):
# Run every 30 minutes */30 * * * * /full/path/to/your/script- Breakdown:
*/30means "every 30 minutes", the five asterisks represent minute, hour, day, month, weekday respectively
- Breakdown:
- If you're running a Python script, specify the full path to your Python interpreter (find it with
which python3):*/30 * * * * /usr/bin/python3 /full/path/to/your_script.py - Save and exit the editor (in vi, type
:wqto save and quit). Cron will automatically apply the new job - Key notes:
- Make your script executable with
chmod +x /full/path/to/your_script.py - On Linux, if your script needs GUI access (screenshot/click), add the
DISPLAYenvironment variable:*/30 * * * * DISPLAY=:0 /usr/bin/python3 /full/path/to/your_script.py - On macOS, if you get permission errors (e.g., can't capture the screen), go to System Settings > Privacy & Security and grant screen recording/accessibility permissions to Terminal (or cron, if needed)
- Make your script executable with
General Script Tips to Avoid Issues
- Use absolute paths everywhere: Don't rely on relative paths in your script (e.g., for saving screenshots). Cron/Task Scheduler runs in a different working directory than when you manually run the script, so absolute paths prevent "file not found" errors
- Fix Gmail authentication: Google no longer allows regular passwords for app access. Use an App Password (requires 2-step verification to be enabled) or set up OAuth2 authentication in your script—this ensures your email sends don't fail due to login issues
- Log output for debugging: Redirect your script's output to a log file so you can troubleshoot if something breaks. For cron, add this to your job line:
On Windows, you can add logging directly in your script or configure Task Scheduler to log events*/30 * * * * /usr/bin/python3 /full/path/to/your_script.py >> /var/log/automation_script.log 2>&1
内容的提问来源于stack exchange,提问作者scott.turner
相关产品推荐
相关产品推荐

