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.serviceresides in
/usr/lib/systemd/system.
Two ways to enable boot start:
systemctl method
<code>systemctl enable xxx # enable boot start
systemctl disable xxx # disable boot start</code> <code># Check status
systemctl status chronyd.service</code>Example output shows the service is loaded and active:
<code>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</code>List enabled or disabled units:
<code>systemctl list-unit-files --state=enabled
systemctl list-unit-files --state=disabled</code>chkconfig method (more complex)
<code>chkconfig --add xxx && chkconfig --level 3 xxx on/off</code>chkconfig works only with SysV scripts located in
/etc/init.dand linked through
/etc/rc.d/rc*.d. To list SysV services use:
<code>chkconfig --list network</code>For native systemd services, use
systemctl list-unit-filesor
systemctl list-dependencies [target].
Note: systemctl creates a symlink in
/etc/systemd/system/or
/etc/systemd/user/pointing to the original service file.
<code># systemctl enable mysqld
Created symlink from /etc/systemd/system/multi-user.target.wants/mysqld.service to /usr/lib/systemd/system/mysqld.service.</code>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
.shscript in
/etc/profile.d.
Method 3: Append the command to
/etc/rc.localas in Method 1.
3. Crontab Solution
crontab supports special keywords prefixed with
@that replace the five time fields.
<code>@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 * * * *")</code>Example using
@rebootfor boot start:
<code># crontab -e
@reboot /usr/local/daemonProcess/somescript.sh
* * * * * /usr/local/daemonProcess/somescript.sh</code>If both
@rebootand 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
<code># 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 start</code>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.