Systemd

CentOS 7 將多程序初始化腳本轉換為 systemd 服務

  • November 18, 2016

我有一個初始化腳本,如下所示

#!/bin/bash
# description: connect start | stop | restart | install
# processname: tomcat
# chkconfig: 234 99 01
JAVA_HOME=/
export JAVA_HOME
PATH=$JAVA_HOME/bin:$PATH
export PATH
CATALINA_HOME=/
ulimit -v unlimited -m..
LOG_COPY_FOLDER=$(date +"%Y-%m-%d_%H%M")

case $1 in
start)
JAVA_OPTS='-Xms160g -Xmx160g -XX:+ResetSignal_MaskAtLaunch -Xloggc:/tom....'
   export JAVA_OPTS
   echo "Using JAVA_OPTS:       $JAVA_OPTS"
find $CATALINA_HOME/logs_prev/* -type d -ctime +180 | xargs rm -rf
mv $CATALINA_HOME/logs $CATALINA_HOME/logs_prev/$LOG_COPY_FOLDER
   mkdir $CATALINA_HOME/logs
   nohup sh $CATALINA_HOME/bin/startup.sh
 less $CATALINA_HOME/logs/catalina.out
;;
stop)
   sh $CATALINA_HOME/bin/shutdown.sh
;;
install)
MySQLConfirmation() {
   while :
   do
       read -p 'Have you run MySQL commands? (y/n): ' answer
           case "${answer}" in
                   y|Y|yes|Yes) exit 0 ;;
                   n|N|no|No) exit 1 ;;
           esac
   done
}

if $( MySQLConfirmation ); then
       JAVA_OPTS='-Xms160g -Xmx160g -XX:+ResetSignalMaskAtLaunch -Xloggc:/tom...
       export JAVA_OPTS
       echo "Using JAVA_OPTS:       $JAVA_OPTS"
   find $CATALINA_HOME/logs_prev/* -type d -ctime +30 | xargs rm -rf
   mv $CATALINA_HOME/logs $CATALINA_HOME/logs_prev/$LOG_COPY_FOLDER
       **rm -R $CATALINA_HOME/website.com/ROOT**
       mkdir $CATALINA_HOME/logs
       **mv -f /root/website.war $CATALINA_HOME/websitcom/ROOT.war**
       nohup sh $CATALINA_HOME/bin/startup.sh
       less $CATALINA_HOME/logs/catalina.out
else
           echo "See you when you're done with MySQL updates!"
   fi
;;
esac
exit 0

如您所見,我已經開始停止重新啟動並安裝程序。並且安裝過程需要刪除和 mkdir 命令,我不知道如何在 systemd 程序上實現這些命令,因為我看不到添加程序以便能夠像“systemctl qinstall service”一樣執行的方法有沒有辦法添加 Execinstall 和systemd 的 ExecinstallPre 參數?謝謝

systemd不支持自定義動作名稱install,它專注於流程管理。

引用自:https://serverfault.com/questions/815542