systemd的程序自启动脚本编写

以FreeSWITCH的自启动脚本为例。

一、 编写freeswitch.service文件

 1 [Unit]
 2 Description=FreeSWITCH
 3 After=syslog.target network.target
 4 After=postgresql.service mysqld.service
 5 
 6 [Service]
 7 User=root
 8 EnvironmentFile=/etc/systemd/freeswitch.conf
 9 # RuntimeDirectory is not yet supported in CentOS 7. A workaround is to use /etc/tmpfiles.d/freeswitch.conf
10 #RuntimeDirectory=/run/freeswitch
11 #RuntimeDirectoryMode=0750
12 WorkingDirectory=/usr/local/freeswitch/run
13 PIDFile=/usr/local/freeswitch/run/freeswitch.pid
14 ExecStart=/usr/bin/freeswitch -nc -nf $FREESWITCH_PARAMS
15 ExecReload=/usr/bin/kill -HUP $MAINPID
16
17 [Install]
18 WantedBy=multi-user.target

二、 freeswitch.service的文件路径

可能会和具体的Linux版本有关系,给出ubuntu的例子:

/lib/systemd/system/

三、 编写步骤一中声明的配置文件

1. 文件路径

/etc/systemd/freeswitch.conf

2. 文件内容

1 ## Type: string

2 ## Default: ""
3 ## Config: ""
4 ## ServiceRestart: freeswitch
5 #
6 # if not empty: parameters for freeswitch
7 #
8 FREESWITCH_PARAMS="-nonat"

四、 自启动添加

systemctl enable freeswitch.service

五、 添加自启动的时候,日志提示如下

Created symlink /etc/systemd/system/multi-user.target.wants/freeswitch.service /lib/systemd/system/freeswitch.service

在etc/systemd/system/multi-user.target.wants/目录下生成一个freeswitch.service的链接。

posted @ 2017-10-31 21:25  一沙世界  阅读(8146)  评论(0编辑  收藏  举报