Configure Linux Services for Automatic Startup with systemd, chkconfig, and crontab
This guide explains how to enable Linux components to start automatically at boot using systemd's systemctl commands, the legacy chkconfig tool, generic rc scripts, and crontab's @reboot syntax, and offers recommendations for production environments.
1. System Services
If a component is installed as a system service, its unit file (e.g., xxx.service) resides in /usr/lib/systemd/system. Two common ways to enable boot start are described.
systemctl method
systemctl enable xxx # enable boot start
systemctl disable xxx # cancel boot startCheck status with: systemctl status chronyd.service List enabled or disabled services:
systemctl list-unit-files --state=enabled
systemctl list-unit-files --state=disabledUse --state together with list-units for more detailed queries.
chkconfig method (more complex)
chkconfig --add xxx && chkconfig --level 3 xxx on/offView SysV services (does not include native systemd units): chkconfig --list network To list systemd services, use systemctl list-unit-files or systemctl list-dependencies [target].
Special note: The systemctl enable command creates a symlink under /etc/systemd/system/ (or /etc/systemd/user/) pointing to the original unit file, e.g.:
# 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, either run systemctl disable xxx or remove the corresponding symlink.
2. Generic Solutions
Regardless of whether a component is already a system service, you can use boot scripts. All scripts must be executable ( chmod +x).
Method 1: Append the start command to /etc/rc.local (or its symlink /etc/rc.d/rc.local).
Method 2: Place a .sh script in /etc/profile.d.
Method 3: Write a .sh script and add its execution line to /etc/rc.local or /etc/rc.d/rc.local.
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 rc.local by ~1‑2 seconds)
@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 to start a daemon after reboot:
# crontab -e
@reboot /usr/local/daemonProcess/somescript.shIf a regular interval task is also needed, decide whether @reboot and the periodic schedule overlap; avoid duplicate starts when the service cannot handle them.
4. Production‑Environment Recommendation
In production, adopt a single boot‑start method for all components to simplify operations. Mixing methods (systemd, rc scripts, crontab) can increase maintenance effort and error risk, especially when services lack native systemd units.
Appendix: Sample Startup Script
# Make rc.local executable
chmod +x /etc/rc.d/rc.local
# Example startup script
/usr/local/AutoStartOnBoot.sh
chmod +x /usr/local/AutoStartOnBoot.sh
source /etc/profile # load environment before rc.local runs
# Start services
/usr/local/redis/redis-5.0.13/bin/redis-server /usr/local/redis/redis-5.0.13/conf/redis.conf
rm -f /data/zookeeper/data/zookeeper_server.pid
/usr/local/apache-zookeeper-3.6.3-bin/bin/zkServer.sh start
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
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 &
service mysqld startLiangxu Linux
Liangxu, a self‑taught IT professional now working as a Linux development engineer at a Fortune 500 multinational, shares extensive Linux knowledge—fundamentals, applications, tools, plus Git, databases, Raspberry Pi, etc. (Reply “Linux” to receive essential resources.)
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.
