方法1:
添加自己的service:
vim /usr/lib/systemd/system/xxx.service
文件分三部分 [Unit]、[Service]和[Install]
# [Unit]部分主要是对这个服务的说明,内容包括Description和After,Description 用于描述服务,After用于描述服务类别
# [Service]部分是服务的关键,是服务的一些具体运行参数的设置.
# Type=forking是后台运行的形式,
# User=users是设置服务运行的用户,
# Group=users是设置服务运行的用户组,
# PIDFile为存放PID的文件路径,
# ExecStart为服务的具体运行命令,
# ExecReload为重启命令,
# ExecStop为停止命令,
# PrivateTmp=True表示给服务分配独立的临时空间
# 注意:[Service]部分的启动、重启、停止命令全部要求使用绝对路径,使用相对路径则会报错!
# [Install]部分是服务安装的相关设置,可设置为多用户的
# 创建完后添加可执行权限
chmod 754 /usr/lib/systemd/system/tomcat.service
[Unit]
Description=nginx
After=network.target
[Service]
Type=forking
PIDFile=/usr/local/nginx/logs/nginx.pid
ExecStart=/usr/local/nginx/sbin/nginx
ExecReload=/usr/local/nginx/sbin/nginx -s reload
ExecStop=/usr/local/nginx/sbin/nginx -s quit
PrivateTmp=true
KillMode=process
[Install]
WantedBy=multi-user.target
systemctl enable rsync.service
好了
方法2:
在centos7,rc.local权限被降低
让一个程序、脚本开机自启动,可以在/etc/rc.d/rc.local文件,尾部加上需要运行的命令即可,还要给予 chmod +x /etc/rc.d/rc.local 权限。
如:
touch /tmp/test.sh
/usr/sbin/apachectl start #启动apahce
让服务器在开机时运行一个程序
/usr/local/bin/php /web/app/zxd.php #前面是php的绝对路径,后面的部分是需要运行的程序的绝对路径
创建一个.sh脚本
Linux脚本开头一般都有这样一句代码(#!/bin/sh与#!/bin/bash),#!/bin/sh是指此脚本使用/bin/sh来解释执行,#!是特殊的表示符,其后面的是此解释此脚本的shell的路径。其实第一句的#!是对脚本的解释器程序路径,脚本的内容是由解释器解释的,我们可以用各种各样的解释器来写对应的脚本。
touch /etc/zxd.sh
chmod +x /etc/zxd.sh
编辑rc.locl
#!/bin/bash
# THIS FILE IS ADDED FOR COMPATIBILITY PURPOSES
#
# It is highly advisable to create own systemd services or udev rules
# to run scripts during boot instead of using this file.
#
# In contrast to previous versions due to parallel execution during boot
# this script will NOT be run after all other services.
#
# Please note that you must run 'chmod +x /etc/rc.d/rc.local' to ensure
# that this script will be executed during boot.
touch /var/lock/subsys/local
/etc/zxd.sh
重启,成功