上QQ阅读APP看书,第一时间看更新
Setting up a cron job
Cron is an old-fashioned but reliable way of executing commands. It has been around since the 70s in Unix, and it's an old favorite in system administration to perform maintenance, such as freeing space, rotating logs, making backups, and other common operations.
This recipe is Unix-specific, so it will work in Linux and MacOS. While it's possible to schedule a task in Windows, it's very different and uses Task Scheduler, which won't be described here. If you have access to a Linux server, it can be a good way of scheduling periodic tasks.
The main advantages are as follows:
- It's present in virtually all Unix or Linux systems and configured to run automatically.
- It's easy to use, though a little deceptive.
- It's well-known. Almost anyone involved with admin tasks will have a general idea on how to use it.
- It allows for easy periodic commands, with good precision.
But, it also has some disadvantages, as follows:
- By default, it may not give much feedback. Retrieving the output, logging execution, and errors is critical.
- The task should be as self-contained as possible to avoid problems with environment variables, such as using the wrong Python interpreter, or what path should execute.
- It is Unix-specific.
- Only fixed periodic times are available.
- It doesn't control how many tasks run at the same time. Each time the countdown goes off, it creates a new task. For example, a task that takes one hour to complete, and that is scheduled to run once every 45 minutes, will have 15 minutes of overlap where two tasks will be running.
Don't understate the latest effect. Running multiple expensive tasks at the same time can have bad effects on performance. Having expensive tasks overlapping may result in a race condition where each task is making the others never finish! Allow ample time for your tasks to finish and keep an eye on them.