如何通过Cronjob定时执行含Bash命令的TXT文件?
Got it, let's walk through getting your script to run automatically every day via cron on macOS. Here's a step-by-step breakdown tailored to your setup:
Step 1: Grant Execute Permissions to Your Scripts
First, make sure cron can actually run your files. Right now, your .txt scripts might not have execute permissions. Run these commands in your terminal (replace the paths with your actual folder location):
chmod +x /path/to/folder/containing/berlin_run.txt chmod +x /path/to/folder/containing/spider_apartments_run.txt chmod +x /path/to/folder/containing/spider_rooms_run.txt
Step 2: Open Your Crontab for Editing
Cron jobs are managed through the crontab utility. To edit your user-specific cron schedule:
- If you prefer a simpler editor like Nano over the default Vim, set it temporarily first:
export EDITOR=nano - Open the crontab editor:
crontab -e
Step 3: Add the Daily Cron Job Entry
At the bottom of the crontab file, add a line to define when and where to run your script. For example, to run it every day at 2 AM, use this cron expression:
0 2 * * * /full/path/to/folder/containing/berlin_run.txt >> /full/path/to/a/log/folder/berlin_run.log 2>&1
Let’s break this down:
0 2 * * *: The cron schedule (minute, hour, day, month, weekday) — this means "run at minute 0, hour 2, every day of every month, regardless of the weekday"./full/path/to/berlin_run.txt: Always use the absolute path to your script in cron (it doesn’t inherit your terminal’s working directory).>> /full/path/to/a/log/folder/berlin_run.log 2>&1: Redirects both output and errors to a log file, so you can debug if something fails. Create the log folder first withmkdir -p /path/to/log/folderif it doesn’t exist.
Step 4: Fix Environment Quirks in Cron
Cron runs with a minimal environment, which can break scripts that rely on your terminal’s PATH or specific executable locations. Here’s how to fix this:
- Use absolute paths for Python: In your
berlin_run.txtscript, replacepythonwith the full path to your Python executable. Find this path withwhich pythonin your terminal (it might look like/usr/local/bin/pythonor/usr/bin/python3).
Example modified line:/usr/local/bin/python berlin_apartments_ads.py; /usr/local/bin/python berlin_rooms.py - Update nested script paths: Instead of
./spider_apartments_run.txt, use the full absolute path (e.g.,/path/to/folder/containing/spider_apartments_run.txt) to ensure cron finds them.
Step 5: Test the Cron Job First
Before trusting the daily schedule, test it with a frequent run to confirm it works. Add this line to your crontab to run the script every 5 minutes:
*/5 * * * * /full/path/to/berlin_run.txt >> /full/path/to/log/folder/berlin_run.log 2>&1
Wait 5-10 minutes, then check the log file to verify the script ran successfully. Once confirmed, replace this test line with your daily schedule line.
Step 6: Confirm the Cron Job is Active
To list all your active cron jobs and make sure your entry is there, run:
crontab -l
内容的提问来源于stack exchange,提问作者LucSpan

