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 #测试