讓 Java 程式成為 Linux service
訊息內容為土耳其文from [url]http://www.kadirsert.com/2009/running-java-program-as-a-linux-service/[/url][code]#!/bin/sh
# Running java program as a linux service
# description: LinuxAgent
scriptFile=$(readlink -fn $(type -p $0))
scriptDir=$(dirname $scriptFile)
applDir="$scriptDir"
serviceName="LinuxAgent"
serviceNameLo="LinuxAgent"
serviceGroup="root"
pidFile="/var/run/$serviceNameLo.pid"
serviceLogFile="/var/log/$serviceNameLo.log"
javaCommand="java"
javaArgs="-jar /LinuxAgent/LinuxAgent.jar"
javaCommandLine="$javaCommand $javaArgs"
javaCommandLineKeyword="LinuxAgent"
maxShutdownTime=15
function makeFileWritable {
local filename="$1"
touch $filename || return 1
chgrp $serviceGroup $filename || return 1
chmod g+w $filename || return 1
return 0;
}
function checkProcessIsRunning {
local pid="$1"
if [ -z "$pid" -o "$pid" == " " ];
then return 1;
fi
if [ ! -e /proc/$pid ]; then
return 1;
fi
return 0;
}
function checkProcessIsOurService {
local pid="$1"
if [ "$(ps -p $pid --no-headers -o comm)" != "$javaCommand" ]; then
return 1;
fi
grep -q –binary -F “$javaCommandLineKeyword" /proc/$pid/cmdline
if [ $? -ne 0 ]; then
return 1;
fi
return 0; }
function startServiceProcess {
cd $applDir || return 1
rm -f $pidFile
makeFileWritable $pidFile || return 1
makeFileWritable $serviceLogFile || return 1
cmd="nohup $javaCommandLine >>$serviceLogFile 2>&1 & echo \$! >$pidFile"
#sudo -u $serviceUser $SHELL -c “$cmd" || return 1
$SHELL -c “$cmd" || return 1
sleep 0.1
pid="$(<$pidFile)"
if checkProcessIsRunning $pid; then :; else
echo "$serviceName servisi calistirilamadi, log dosyasi $serviceLogFile."
return 1
fi
return 0;
}
function stopServiceProcess {
kill $pid || return 1
for ((i=0; i<maxShutdownTime*10; i++)); do
checkProcessIsRunning $pid
if [ $? -ne 0 ]; then
rm -f $pidFile
return 0
fi
sleep 0.1
done
echo "$serviceName $maxShutdownTime surede kapatilamadi, SIGKILL yollaniyor"
kill -s KILL $pid || return 1
local killWaitTime=15
for ((i=0; i<killWaitTime*10; i++)); do
checkProcessIsRunning $pid
if [ $? -ne 0 ]; then
rm -f $pidFile
return 0
fi
sleep 0.1
done
echo "Hata: $serviceName $maxShutdownTime+$killWaitTime surede kapatilamadi"
return 1;
}
function getServicePID {
if [ ! -f $pidFile ]; then
return 1;
fi
pid="$(<$pidFile)"
checkProcessIsRunning $pid || return 1
checkProcessIsOurService $pid || return 1
return 0;
}
function startService {
getServicePID
if [ $? -eq 0 ]; then
echo "$serviceName zaten calisiyor";
return 0;
fi
echo "$serviceName Basliyor"
startServiceProcess
if [ $? -ne 0 ]; then
return 1;
fi
return 0;
}
function stopService {
getServicePID
if [ $? -ne 0 ]; then
echo "$serviceName calismiyor";
return 0;
fi
echo "$serviceName Duruyor"
stopServiceProcess
if [ $? -ne 0 ]; then
return 1;
fi
return 0;
}
function checkServiceStatus {
echo -n “$serviceName kontrol ediliyor: "
if getServicePID; then
echo "LinuxAgent calisiyor"
else
echo "LinuxAgent calismiyor"
fi
return 0;
}
function main {
case “$1" in
start)
startService
;;
stop)
stopService
;;
restart)
stopService && startService
;;
status)
checkServiceStatus
;;
*)
echo "Kullanim: $0 {start|stop|restart|status}"
exit 1
;;
esac
}
main $1
[/code]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
頁:
[1]