ubuntu下使用systemd设置程序开机自启动

一:编写系统服务文件

#vi /usr/lib/systemd/system/mydriver.service下添加如下内容

[Unit]
Description=mydriver service

[Service]
Type=simple
WorkingDirectory=/root
ExecStart=/root/mydriver.sh
ExecStop=/bin/kill -s TERM $MAINPID

[Install]
WantedBy=multi-user.target

systemctl daemon-reload              service文件改动后要重新转载一下

赋予权限chmod 777 /usr/lib/systemd/system/mydriver.service

二:查看系统服务文件是否被识别

# systemctl list-unit-files|grep mydriver
mydriver.service                            disabled

三:编写程序启动脚本

# vi /root/mydriver.sh中添加如下内容

#!/bin/bash
init.sh --na
sleep 1
echo "insmod max9286"
modprobe max9286 id=0x1 lccen_gpio=4 bit_resolution=14
sleep 2
echo "insmod infrared_raw14_brg"
modprobe ar_infrared_raw14_brg addr=0x39
sleep 2
test_tuning -a &
sleep 2
test_encode --check-disable 1
sleep 2
test_encode -i0 --cvbs 480i

最后赋予脚本执行权限

#chmod 777 mydriver.sh

5 测试系统服务
5.1 启动系统服务

# systemctl start mydriver.service

5.2 停止系统服务

# systemctl stop mydriver.service

5.3 查看系统服务是否成功启动

# systemctl status mydriver.service -l

6 将服务设置成开机自启动

# systemctl enable mydriver.service
# systemctl list-unit-files| grep mydriver
--------------------------
myapp.service                      enabled

反向命令:

# systemctl disable mydriver.service
# systemctl list-unit-files| grep mydriver
--------------------------
myapp.service                      disabled

 

附systemctl常见命令:

systemctl is-enabled servicename.service #查询服务是否开机启动
systemctl enable *.service #开机运行服务
systemctl disable *.service #取消开机运行
systemctl start *.service #启动服务
systemctl stop *.service #停止服务
systemctl restart *.service #重启服务
systemctl reload *.service #重新加载服务配置文件
systemctl status *.service #查询服务运行状态
systemctl --failed #显示启动失败的服务
 

 

 

 

你可能感兴趣的