Master Linux Startup: Systemd, chkconfig, and Crontab Methods Explained
This guide details how to configure Linux services to start on boot using systemd (systemctl), legacy SysV init (chkconfig), generic startup scripts, and crontab @reboot, and offers best‑practice recommendations for production environments.
1. System Services
If a component is installed as a system service, the default service file is xxx.service located in /usr/lib/systemd/system.
Two ways to enable boot start:
systemctl method
systemctl enable/disable xxx # enable or disable boot start
# Check boot start status
systemctl status chronyd.service
# Example output (truncated)
● chronyd.service - NTP client/server
Loaded: loaded (/usr/lib/systemd/system/chronyd.service; enabled; vendor preset: enabled)
Active: active (running) since Thu 2022-07-28 18:33:39 CST; 4 months 4 days ago
Docs: man:chronyd(8) man:chrony.conf(5)
Main PID: 790 (chronyd)
Memory: 1.0M
CGroup: /system.slice/chronyd.service
└─790 /usr/sbin/chronyd
# List unit files and their enable state
systemctl list-unit-files --state=enabled
systemctl list-unit-files --state=disabled
# Help for --state option
systemctl --state=helpchkconfig method (more complex)
chkconfig --add xxx && chkconfig --level 3 xxx on/off
# This method requires the service script to be in /etc/init.d/ and linked in /etc/rc.d/rc0.d~rc6.d.
# Check boot start status for SysV services
chkconfig --list network
# Note: output shows only SysV services, not native systemd services.
# To list systemd services use:
# systemctl list-unit-files
# To see services enabled for a specific target use:
# systemctl list-dependencies [target]Special note: systemctl effect
The systemctl method creates a symbolic link for the .service file under /etc/systemd/system/ or /etc/systemd/user/, representing system‑wide or user‑specific services. The target directory (e.g., multi-user.target.wants) determines when the service starts.
# Enable MySQL service
systemctl enable mysqld
# Result: Created symlink from /etc/systemd/system/multi-user.target.wants/mysqld.service to /usr/lib/systemd/system/mysqld.service.To disable boot start, either run systemctl disable xxx or remove the corresponding symlink or service file.
2. General Solution
Regardless of whether a component is already a system service, you can use a boot‑start script. Remember to make the script executable ( chmod +x).
Add the start command to the default script /etc/rc.local (or its symlink /etc/rc.d/rc.local).
Create a .sh script and place it in /etc/profile.d.
Append the execution command to /etc/rc.local or /etc/rc.d/rc.local from a custom script.
3. Crontab Solution
Crontab supports special keywords prefixed with @ to replace the five time fields. Common forms:
@reboot : Run once after reboot (executes earlier than /etc/rc.d/rc.local).
@yearly : Run once a year ("0 0 1 1 *").
@annually : Same as @yearly.
@monthly : Run once a month ("0 0 1 * *").
@weekly : Run once a week ("0 0 * * 0").
@daily : Run once a day ("0 0 * * *").
@hourly : Run once an hour ("0 * * * *").Example using @reboot for a daemon:
# Edit crontab
crontab -e
# Add job definition
@reboot /usr/local/daemonProcess/somescript.sh
# Optional daemon loop (run every minute)
* * * * * /usr/local/daemonProcess/somescript.shSpecial note: Decide whether to use both @reboot and a regular schedule based on your application’s tolerance for duplicate starts.
4. Production‑Environment Startup Recommendation
In production, adopt a single, unified startup method for all components to simplify operations. Mixing different methods can increase maintenance cost and error probability, especially when services are added, removed, or upgraded.
Appendix: Startup Script Example
# Make rc.local executable
chmod +x /etc/rc.d/rc.local
# Example startup script
/usr/local/AutoStartOnBoot.sh
chmod +x /usr/local/AutoStartOnBoot.sh
# Load environment variables
source /etc/profile
# Start Redis
/usr/local/redis/redis-5.0.13/bin/redis-server /usr/local/redis/redis-5.0.13/conf/redis.conf
# Start Zookeeper (remove old pid first)
rm -f /data/zookeeper/data/zookeeper_server.pid;
/usr/local/apache-zookeeper-3.6.3-bin/bin/zkServer.sh start
# Start Kafka
JMX_PORT=9999 /usr/local/kafka_2.13-2.7.1/bin/kafka-server-start.sh -daemon /usr/local/kafka_2.13-2.7.1/config/server.properties
# Start Kafka‑Manager (remove old pid first)
rm -f /usr/local/kafka-manager-2.0.0.2/RUNNING_PID
nohup /usr/local/kafka-manager-2.0.0.2/bin/kafka-manager -Dconfig.file=/usr/local/kafka-manager-2.0.0.2/conf/application.conf -Dhttp.port=9002 >/dev/null 2>&1 &
# Start MySQL
service mysqld startSigned-in readers can open the original source through BestHub's protected redirect.
This article has been distilled and summarized from source material, then republished for learning and reference. If you believe it infringes your rights, please contactand we will review it promptly.
MaGe Linux Operations
Founded in 2009, MaGe Education is a top Chinese high‑end IT training brand. Its graduates earn 12K+ RMB salaries, and the school has trained tens of thousands of students. It offers high‑pay courses in Linux cloud operations, Python full‑stack, automation, data analysis, AI, and Go high‑concurrency architecture. Thanks to quality courses and a solid reputation, it has talent partnerships with numerous internet firms.
How this landed with the community
Was this worth your time?
0 Comments
Thoughtful readers leave field notes, pushback, and hard-won operational detail here.
