完整文章请参考https://www.jianshu.com/p/79059b06a121或者google
配置文件目录
- systemctl脚本目录:/usr/lib/systemd/
- 系统服务目录:
/usr/lib/systemd/system/ - 用户服务目录:
/usr/lib/systemd/user/
在/usr/lib/systemd/system目录下新建service-name.service文件:
[UNIT]#服务描述Description=Media wanager Service#指定了在systemd在执行完那些target之后再启动该服务After=network.target[Service]#定义Service的运行类型,一般是forking(后台运行)Type=forking#定义systemctl start|stop|reload *.service 的执行方法(具体命令需要写绝对路径)#注:ExecStartPre为启动前执行的命令ExecStartPre=/usr/bin/test "x${NETWORKMANAGER}" = xyesExecStart=/home/mobileoa/apps/shMediaManager.sh -startExecReload=ExecStop=/home/mobileoa/apps/shMediaManager.sh -stop#创建私有的内存临时空间PrivateTmp=True[Install]#多用户WantedBy=multi-user.target
新建好service文件后,要执行命令 重载系统服务:systemctl daemon-reload
设置开机启动:systemctl enable *.service
启动服务:systemctl start *.service
停止服务:systemctl stop *.service
重启服务:systemctl reload *.service
示例集
注意:需要以754的权限保存在目录/usr/lib/systemd/system/
开机启动: systemctl enable nginx.service
heart-beat.service(自定义服务)
[Unit]Description=heart-beatAfter=network.target remote-fs.target nss-lookup.target[Service]Type=forkingExecStart=/home/admin/dashboard-heart-beat/start.shExecReload=ExecStop=/home/admin/dashboard-heart-beat/stop.sh[Install]WantedBy=multi-user.target
tomcat.service
[Unit]Description=tomcatAfter=network.target remote-fs.target nss-lookup.target[Service]Type=forkingExecStart=/usr/local/tomcat/bin/startup.shExecReload=ExecStop=/usr/local/tomcat/bin/shutdown.sh[Install]WantedBy=multi-user.target
nginx.service
[Unit]Description=nginx - high performance web serverAfter=network.target remote-fs.target nss-lookup.target[Service]Type=forkingExecStart=/usr/local/nginx/sbin/nginx -c /usr/local/nginx/conf/nginx.confExecReload=/usr/local/nginx/sbin/nginx -s reloadExecStop=/usr/local/nginx/sbin/nginx -s stop[Install]WantedBy=multi-user.target--------------------------或者---------------[Unit]Description=The nginx HTTP and reverse proxy serverAfter=network.target remote-fs.target nss-lookup.target[Service]Type=forkingPIDFile=/run/nginx.pid# Nginx will fail to start if /run/nginx.pid already exists but has the wrong# SELinux context. This might happen when running `nginx -t` from the cmdline.# https://bugzilla.redhat.com/show_bug.cgi?id=1268621ExecStartPre=/usr/bin/rm -f /run/nginx.pidExecStartPre=/usr/sbin/nginx -tExecStart=/usr/sbin/nginxExecReload=/bin/kill -s HUP $MAINPIDKillSignal=SIGQUITTimeoutStopSec=5KillMode=processPrivateTmp=true[Install]WantedBy=multi-user.target
mysql.service
[Unit]Description=mysqlAfter=network.target remote-fs.target nss-lookup.target[Service]Type=forkingExecStart=/usr/local/mysql/support-files/mysql.server start#ExecReload=/usr/local/mysql/support-files/mysql.server restart#ExecStop=/usr/local/mysql/support-files/mysql.server stop#PrivateTmp=true[Install]WantedBy=multi-user.target
php-fpm.service
[Unit]Description=phpAfter=network.target remote-fs.target nss-lookup.target[Service]Type=forkingExecStart=/usr/local/php/sbin/php-fpm[Install]WantedBy=multi-user.target
redis.service
[Unit]Description=RedisAfter=network.target remote-fs.target nss-lookup.target[Service]Type=forkingExecStart=/usr/local/bin/redis-server /etc/redis.confExecStop=kill -INT `cat /tmp/redis.pid`User=wwwGroup=www[Install]WantedBy=multi-user.target
supervisord.service
[Unit]Description=Process Monitoring and Control DaemonAfter=rc-local.service[Service]Type=forkingExecStart=/usr/bin/supervisord -c /etc/supervisord.confSysVStartPriority=99[Install]WantedBy=multi-user.target
