Linux开机运行程序是指在Linux系统启动时自动运行指定的程序或脚本。这通常通过系统初始化脚本来实现,例如/etc/rc.local
、systemd
服务文件等。
/etc/rc.local
文件。.service
)。在/etc/rc.local
文件中添加以下内容:
#!/bin/sh -e
#
# rc.local
#
# This script is executed at the end of each multiuser runlevel.
# Make sure that the script will "exit 0" on success or any other
# value on error.
#
# In order to enable or disable this script just change the execution
# bits.
#
# By default this script does nothing.
/usr/bin/my_program
exit 0
确保/etc/rc.local
文件有执行权限:
chmod +x /etc/rc.local
创建一个新的服务文件/etc/systemd/system/my_program.service
:
[Unit]
Description=My Program
After=network.target
[Service]
ExecStart=/usr/bin/my_program
Restart=always
[Install]
WantedBy=multi-user.target
启用并启动服务:
sudo systemctl enable my_program.service
sudo systemctl start my_program.service
原因:
解决方法:
原因:
解决方法:
通过以上方法,你可以实现Linux开机运行程序,并解决常见的问题。
领取专属 10元无门槛券
手把手带您无忧上云