用service來管理服務的時候,是在/etc/init.d/目錄中建立一個腳本文件,來管理服務的啓動和中止,在systemctl中,也相似,文件目錄有所不一樣,在/lib/systemd/system目錄下建立一個腳本文件tomcat,裏面的內容以下:tomcat
[Unit] Description=Tomcat After=network.target [Service] Type=forking PIDFile=/usr/local/tomcat/pid ExecStart=/usr/local/tomcat/bin/catalina.sh start ExecReload=/usr/local/tomcat/bin/catalina.sh restart ExecStop=/usr/local/tomcat/bin/catalina.sh stop [Install] WantedBy=multi-user.target
tomcat的bin/catalina.sh中加入CATALINA_PID參數時,須要在# OS specific support.上加入 CATALINA_PID=/usr/local/tomcat/pid # OS specific support. $var _must_ be set to either true or false. cygwin=false ....略..
建立軟連接是爲了下一步系統初始化時自動啓動服務bash
ln -s /lib/systemd/system/tomcat.service /etc/systemd/system/multi-user.target.wants/tomcat.service
建立軟連接就比如Windows下的快捷方式 網絡
若是建立軟鏈接的時候出現異常,不要擔憂,看看/etc/systemd/system/multi-user.target.wants/ 目錄是否正常建立軟連接爲準,有時候報錯只是提示一下,其實成功了。ide
$ ll /etc/systemd/system/multi-user.target.wants/ total 8 drwxr-xr-x 2 root root 4096 Mar 30 15:46 ./ drwxr-xr-x 13 root root 4096 Mar 13 14:18 ../ lrwxrwxrwx 1 root root 31 Nov 23 14:43 tomcat.service -> /lib/systemd/system/tomcat.service ...略...
剛剛配置的服務須要讓systemctl能識別,就必須刷新配置rest
$ systemctl daemon-reload
若是沒有權限能夠使用sudocode
$ sudo systemctl daemon-reload
啓動tomcatip
$ systemctl start tomcat
重啓tomcatci
$ systemctl restart tomcat
中止tomcatget
$ systemctl stop tomcat
tomcat服務加入開機啓動it
$ systemctl enable tomcat
禁止開機啓動
$ systemctl disable tomcat
查看狀態
$ systemctl status tomcat