编写systemd服务,开机自启

CentOS 7的服务systemctl脚本存放在:/usr/lib/systemd/,
有系统(system)和用户(user)之分,像需要开机不登陆就能运行的程序,
还是存在系统服务里吧,即:/usr/lib/systemd/system目录下
每一个服务以.service结尾,一般会分为3部分:[Unit]、[Service]和[Install]

编写开机自启服务,先创建一个脚本
root@21:52:29/opt # vim /opt/test.sh
root@21:53:11/opt # cat /opt/test.sh 
#!/bin/bash
bash /opt/myscript/httpd-time.sh
bash /opt/myscript/tomcat-time.sh
bash /opt/myscript/redis-time.sh
root@21:54:25~ # chmod -R +x /opt/myscript/
————————————————————————————————————————————————————————————————————————
然后把自己需要启动的脚本放到/opt/myscript下(目录可以自己设置)
root@21:56:57/opt/myscript # cat httpd-time.sh redis-time.sh tomcat-time.sh 
#!/bin/bash
date >> /opt/httpdtime
#!/bin/bash
date >> /opt/redistime
#!/bin/bash
date >> /opt/tomcatime
————————————————————————————————————————————————————————————————————————
接下来进入到/usr/lib/systemd/system目录下,编写xxx.service
root@21:58:00~ # cd /usr/lib/systemd/system
root@21:58:10/usr/lib/systemd/system # vim test-time.service
[Unit]
Description=test date-time script
After=network.target remote-fs.target nss-lookup.target

[Service]
Type=forking
ExecStart=/opt/test.sh
#ExecReload=edit command
#ExecStop=edit command
#PrivateTmp=true

[Install]
WantedBy=multi-user.target
——————————————————————————————————————————————————————————————————
保存退出后执行
root@22:03:07~ # systemctl daemon-reload  #重新加载一下
root@22:03:14~ # systemctl start test-time.service  #开启服务
root@22:03:24~ # systemctl enable test-time  #设置成开机自启
Created symlink from /etc/systemd/system/multi-user.target.wants/test-time.service to /usr/lib/systemd/system/test-time.service.
root@22:03:33~ # systemctl is-enabled test-time  #检查是否是开机自启
enabled
root@22:03:44~ # reboot  #测试
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

河 静

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值