原本开机启动是在/etc/init.d/rcS中配置,现在已经更改。
本文介绍 Linux 系统下其他的开机自启动配置。常见有两种类型,initd 方式和 systemd 方式。后者为更新更灵活高效的方式,本文介绍后者。
1、在 /lib/systemd/system/ 创建新的 hello.service 文件,指定需要启动的程序。
[Unit]
Description=helloworld
After=basic.service X.service thermal-zone-init.service //启动顺序
[Service]
Environment=DISPLAY=0:0
#设置应用的工作目录
WorkingDirectory=/myapp_path
ExecStartPre=/bin/echo "*******start to run service******"
ExecStart=/home/root/helloworld(需要执行的文件)
StandardOutput=tty(没有这个不打印echo)
KillMode=process(没有这个进程起不来)
KillSignal=SIGKILL
SendSIGKILL=yes
[Install]
WantedBy=multi-user.target
/home/root/helloworld
#!/bin/sh //这个一定要添加,否则运行有错误
/home/rs485 & //进程
echo "***!!!!!start!!!!"
2、$ systemctl -f enable /lib/systemd/system/hello.service (-f 选项指 force,可按需选择)。
常用命令与错误
#如果/etc/systemd/system/ helloworld.service有过修改,需要进行更新,执行下面这个命令
#systemctl daemon-reload
#查看服务是否被正确识别
#systemctl list-unit-files|grep helloworld
systemctl start helloworld.service 启动系统服务
systemctl stop helloworld.service 停止系统服务
systemctl status helloworld.service -l 查看系统服务是否成功启动
ExecStart参数指定启动脚本路径不对,会提示203错误。(改对就好了。)
启动程序脚本(.sh文件)中未指定脚本解析器,会提示203错误。(在文件开头添加:#!/bin/sh)
启动程序脚本(.sh文件)没有执行权限,会提示203错误。
systemctl enable hello.service 将服务设置成开机自启动
systemctl list-unit-files|grep helloworld
helloworld.service enabled
反向命令:
systemctl disable hello.service
systemctl list-unit-files|grep hello
hello.service disabled