Linux 系统下的开机自启动配置

原本开机启动是在/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
  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值