27 Crontab Automatic Startup Script
In previous tutorials, we briefly introduced how to disable the automatic startup of the product's main program. The method used is to comment out the command to run the product's main program in the Crontab file. In this chapter, you will learn more about Crontab and why we use Crontab instead of Services for automatic startup.
Crontab is a tool in the Linux system used for scheduling periodic tasks. Through Crontab, users can set specific times, dates, or intervals to execute specific commands or scripts. Here are some important concepts and usages of Crontab:
Crontab File
The Crontab file is where scheduling information for periodic tasks is stored. Each user has their own Crontab file for storing their own scheduling information. Crontab files are usually stored in the /var/spool/cron directory, named after the user's username.
Crontab Format
Each line in the Crontab file represents a scheduled task. Each line consists of five fields representing minutes, hours, date, month, and day of the week. You can use the # symbol to comment out a line to disable the corresponding task scheduling.
Usage
To edit the Crontab file, you can use the crontab -e command. This command opens a text editor, allowing the user to edit their Crontab file. After editing, save and exit the editor, and the Crontab file will be updated. Common options:
- -e: Edit the user's Crontab file.
- -l: List the contents of the user's Crontab file.
- -r: Remove the user's Crontab file.
- -u: Specify the user to operate on.
Comparison
In contrast, using services to achieve automatic startup at boot time is typically done by executing a series of predefined services or scripts when the system starts up. These services can be found in the /etc/init.d/ directory and started, stopped, or restarted using system service management tools like systemctl.
Advantages of Crontab
- Flexibility: Tasks can be scheduled very flexibly, including specifying minutes, hours, dates, months, and days of the week.
- Simplicity: Crontab configuration is relatively simple, making it convenient for scheduling simple periodic tasks.
- User Independence: Each user has their own Crontab file, allowing them to manage their own tasks without affecting other users.
Advantages of Services
- Reliability: Services implemented through services are often more stable and reliable as they are system-level services that are automatically loaded and run when the system starts.
- Management: System administrators can more easily manage services, including starting, stopping, restarting, and checking status.
- Control Permissions: For tasks requiring privileged execution, using services provides better control over permissions to ensure security.
Special Advantages of Crontab in this Product
Lower resource usage: Through our testing and comparison, the CPU resource usage of the same Python script using Crontab is 1/4 of that using services. For applications like complex robot main programs, using Crontab for automatic startup is a better choice. Services are more suitable for important services or applications that need to be executed at system startup. to be executed at system startup.