以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的鏈接。