bestlong 怕失憶論壇

 

 

搜索
bestlong 怕失憶論壇 論壇 Java 讓 Java 程式成為 Linux service
查看: 3320|回復: 0
go

讓 Java 程式成為 Linux service [複製鏈接]

Rank: 9Rank: 9Rank: 9

1#
發表於 2011-1-28 08:31 |只看該作者 |倒序瀏覽 |打印
訊息內容為土耳其文
from http://www.kadirsert.com/2009/ru ... as-a-linux-service/
  1. #!/bin/sh
  2. # Running java program as a linux service
  3. # description: LinuxAgent

  4. scriptFile=$(readlink -fn $(type -p $0))
  5. scriptDir=$(dirname $scriptFile)
  6. applDir="$scriptDir"
  7. serviceName="LinuxAgent"
  8. serviceNameLo="LinuxAgent"
  9. serviceGroup="root"
  10. pidFile="/var/run/$serviceNameLo.pid"
  11. serviceLogFile="/var/log/$serviceNameLo.log"
  12. javaCommand="java"
  13. javaArgs="-jar /LinuxAgent/LinuxAgent.jar"
  14. javaCommandLine="$javaCommand $javaArgs"
  15. javaCommandLineKeyword="LinuxAgent"
  16. maxShutdownTime=15

  17. function makeFileWritable {
  18.     local filename="$1"
  19.     touch $filename || return 1
  20.     chgrp $serviceGroup $filename || return 1
  21.     chmod g+w $filename || return 1
  22.     return 0;
  23. }

  24. function checkProcessIsRunning {
  25.     local pid="$1"
  26.     if [ -z "$pid" -o "$pid" == " " ];
  27.         then return 1;
  28.     fi
  29.     if [ ! -e /proc/$pid ]; then
  30.         return 1;
  31.     fi
  32.     return 0;
  33. }

  34. function checkProcessIsOurService {
  35.     local pid="$1"
  36.     if [ "$(ps -p $pid --no-headers -o comm)" != "$javaCommand" ]; then
  37.         return 1;
  38.     fi
  39.     grep -q –binary -F “$javaCommandLineKeyword" /proc/$pid/cmdline
  40.     if [ $? -ne 0 ]; then
  41.         return 1;
  42.     fi
  43.     return 0; }

  44. function startServiceProcess {
  45.     cd $applDir || return 1
  46.     rm -f $pidFile
  47.     makeFileWritable $pidFile || return 1
  48.     makeFileWritable $serviceLogFile || return 1
  49.     cmd="nohup $javaCommandLine >>$serviceLogFile 2>&1 & echo \$! >$pidFile"
  50.     #sudo -u $serviceUser $SHELL -c “$cmd" || return 1
  51.     $SHELL -c “$cmd" || return 1
  52.     sleep 0.1
  53.     pid="$(<$pidFile)"
  54.     if checkProcessIsRunning $pid; then :; else
  55.         echo "$serviceName servisi calistirilamadi, log dosyasi $serviceLogFile."
  56.         return 1
  57.     fi
  58.     return 0;
  59. }

  60. function stopServiceProcess {
  61.     kill $pid || return 1
  62.     for ((i=0; i<maxShutdownTime*10; i++)); do
  63.         checkProcessIsRunning $pid
  64.         if [ $? -ne 0 ]; then
  65.             rm -f $pidFile
  66.             return 0
  67.         fi
  68.         sleep 0.1
  69.     done
  70.     echo "$serviceName $maxShutdownTime surede kapatilamadi, SIGKILL yollaniyor"
  71.     kill -s KILL $pid || return 1
  72.     local killWaitTime=15
  73.     for ((i=0; i<killWaitTime*10; i++)); do
  74.         checkProcessIsRunning $pid
  75.         if [ $? -ne 0 ]; then
  76.             rm -f $pidFile
  77.         return 0
  78.         fi
  79.         sleep 0.1
  80.     done
  81.     echo "Hata: $serviceName $maxShutdownTime+$killWaitTime surede kapatilamadi"
  82.     return 1;
  83. }

  84. function getServicePID {
  85.     if [ ! -f $pidFile ]; then
  86.         return 1;
  87.     fi
  88.     pid="$(<$pidFile)"
  89.     checkProcessIsRunning $pid || return 1
  90.     checkProcessIsOurService $pid || return 1
  91.     return 0;
  92. }

  93. function startService {
  94.     getServicePID
  95.     if [ $? -eq 0 ]; then
  96.         echo "$serviceName zaten calisiyor";
  97.         return 0;
  98.     fi
  99.     echo "$serviceName Basliyor"
  100.     startServiceProcess
  101.     if [ $? -ne 0 ]; then
  102.         return 1;
  103.     fi
  104.     return 0;
  105. }

  106. function stopService {
  107.     getServicePID
  108.     if [ $? -ne 0 ]; then
  109.         echo "$serviceName calismiyor";
  110.         return 0;
  111.     fi
  112.     echo "$serviceName Duruyor"
  113.     stopServiceProcess
  114.     if [ $? -ne 0 ]; then
  115.         return 1;
  116.     fi
  117.     return 0;
  118. }

  119. function checkServiceStatus {
  120.     echo -n “$serviceName kontrol ediliyor: "
  121.     if getServicePID; then
  122.         echo "LinuxAgent calisiyor"
  123.     else
  124.         echo "LinuxAgent calismiyor"
  125.     fi
  126.     return 0;
  127. }

  128. function main {
  129.     case “$1" in
  130.     start)
  131.         startService
  132.         ;;
  133.     stop)
  134.         stopService
  135.         ;;
  136.     restart)
  137.         stopService && startService
  138.         ;;
  139.     status)
  140.         checkServiceStatus
  141.         ;;
  142.     *)
  143.         echo "Kullanim: $0 {start|stop|restart|status}"
  144.         exit 1
  145.         ;;
  146.     esac
  147. }
  148. main $1
複製代碼
Script must be saved under the /etc/inid.d directory and execute permission must be given;
$ chmod 755 /etc/init.d/LinuxAgent

Chkconfig utility registers the service;
$ chkconfig –add LinuxAgent
$ chkconfig LinuxAgent –level 12345 on

Now service is created and we start and stop service with these commands;
$ service LinuxAgent start
$ service LinuxAgent stop

To learn the status of the service;
$ service LinuxAgent status
我是雪龍
http://blog.bestlong.idv.tw
http://www.bestlong.idv.tw
‹ 上一主題|下一主題

Archiver|怕失憶論壇

GMT+8, 2024-4-26 20:22 , Processed in 0.010456 second(s), 10 queries .

Powered by Discuz! X1.5

© 2001-2010 Comsenz Inc.