Master Linux Boot Startup: Systemd, chkconfig, and Crontab Strategies
This guide explains how to configure Linux services to start on boot using systemd (systemctl), the legacy chkconfig tool, general startup scripts, and crontab’s special @reboot keyword, and provides production‑grade recommendations and example scripts for reliable automation.
1. System Services
If a component is installed as a system service, the default service file xxx.service resides in /usr/lib/systemd/system.
Two ways to enable boot start:
systemctl method
systemctl enable xxx # enable boot start
systemctl disable xxx # disable boot start # Check status
systemctl status chronyd.serviceExample output shows the service is loaded and active:
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 CSTList enabled or disabled units:
systemctl list-unit-files --state=enabled
systemctl list-unit-files --state=disabledchkconfig method (more complex)
chkconfig --add xxx && chkconfig --level 3 xxx on/offchkconfig works only with SysV scripts located in /etc/init.d and linked through /etc/rc.d/rc*.d. To list SysV services use: chkconfig --list network For native systemd services, use systemctl list-unit-files or systemctl list-dependencies [target].
Note: systemctl creates a symlink in /etc/systemd/system/ or /etc/systemd/user/ pointing to the original service file.
# systemctl enable mysqld
Created symlink from /etc/systemd/system/multi-user.target.wants/mysqld.service to /usr/lib/systemd/system/mysqld.service.To disable boot start:
systemctl disable xxx
Remove the corresponding symlink or service file under /etc/systemd/system/ or
/etc/systemd/user/2. General Solutions
Regardless of whether a component is a system service, you can achieve boot start by using startup scripts. Ensure the script is executable ( chmod +x).
Method 1: Append the start command to /etc/rc.local (or /etc/rc.d/rc.local, which is a symlink).
Method 2: Place a .sh script in /etc/profile.d.
Method 3: Append the command to /etc/rc.local as in Method 1.
3. Crontab Solution
crontab supports special keywords prefixed with @ that replace the five time fields.
@reboot : Run once after reboot (executes 1‑2 s earlier than /etc/rc.d/rc.local)
@yearly : Run once a year ("0 0 1 1 *")
@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 boot start:
# crontab -e
@reboot /usr/local/daemonProcess/somescript.sh
* * * * * /usr/local/daemonProcess/somescript.shIf both @reboot and a regular schedule are used, ensure they do not cause duplicate execution within the first minute unless the service tolerates it.
4. Production Environment Recommendation
In production, adopt only one of the three schemes for all components to simplify operations, reduce maintenance cost, and avoid errors during service upgrades.
Appendix: Boot Script Example
# chmod +x /etc/rc.d/rc.local
# /usr/local/AutoStartOnBoot.sh
chmod +x /usr/local/AutoStartOnBoot.sh
source /etc/profile
# redis
/usr/local/redis/redis-5.0.13/bin/redis-server /usr/local/redis/redis-5.0.13/conf/redis.conf
# zookeeper (remove old pid then start)
rm -f /data/zookeeper/data/zookeeper_server.pid
/usr/local/apache-zookeeper-3.6.3-bin/bin/zkServer.sh 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
# kafka-manager
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 &
# 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.
Raymond Ops
Linux ops automation, cloud-native, Kubernetes, SRE, DevOps, Python, Golang and related tech discussions.
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.
