在 CentOS 7 中,有多个地方可以配置程序以在系统启动时自动启动。以下是主要的启动管理位置和方法:
1. Systemd 服务
sudo systemctl enable httpd
sudo systemctl disable httpd
systemctl list-unit-files --type=service
2. init.d 脚本
sudo chkconfig service_name on
sudo chkconfig service_name off
3. /etc/rc.d/rc.local
sudo nano /etc/rc.d/rc.local
sudo chmod +x /etc/rc.d/rc.local
4. Cron 作业
crontab -e
@reboot /path/to/your/script.sh
5. Systemd 定时器
示例:
创建一个服务单元 /etc/systemd/system/example.service
和定时器单元 /etc/systemd/system/example.timer
。
sudo systemctl enable example.timer
6. /etc/profile 和 ~/.bash_profile
sudo nano /etc/profile
nano ~/.bash_profile
7. Desktop Environment (图形用户界面)
创建
.desktop
文件:
[Desktop Entry] Type=Application Exec=/path/to/application Hidden=false NoDisplay=false X-GNOME-Autostart-enabled=true Name=YourApplication Comment=Start Your Application
总结
systemd 服务:主要方式,通过 systemctl 管理。
/etc/init.d/ 脚本:旧版方法,但在现代系统中使用较少。
/etc/rc.d/rc.local:自定义启动脚本。
cron 作业:使用 @reboot 选项安排任务。
systemd 定时器:用于定期任务。
用户启动文件:/etc/profile 和 ~/.bash_profile。
图形用户界面:~/.config/autostart/ 目录中的 .desktop 文件。
本文链接:https://blog.runxinyun.com/post/317.html 转载需授权!
留言0