LinuxQuestions.org
Download your favorite Linux distribution at LQ ISO.
Go Back   LinuxQuestions.org > Forums > Other *NIX Forums > AIX
User Name
Password
AIX This forum is for the discussion of IBM AIX.
eserver and other IBM related questions are also on topic.

Notices

Reply
 
Search this Thread
Old 10-01-2013, 09:36 AM   #1
redssr
Member
 
Registered: Oct 2013
Location: Dist Nasik MH State Country India
Distribution: Linux
Posts: 32

Rep: Reputation: Disabled
How to define pidfile in sh script of AIX


Hello,

All,

This the first time i am using AIX. I had a script which runs java application. but i am unable to define the pidfile as a result the application is not getting started.

I am attaching the my sh script to this post. Kindly let me know if any modifications need to be done.

Thanks and Regards,

kindly find the script bellow,
#!/usr/bin/ksh
##################################################
# name: opensyncserver
# purpose: script that will start or stop the opensync daemon.
#################################################
# set PATH
PATH=/usr/bin/macros:/bin:/usr/bin:/usr/sbin:/etc:/usr/ucb:$HOME/bin:/usr/bin/X11:/usr/java6/bin/:.

# Set this to your Java installation
JAVA_HOME=/usr/java6

serviceNameLo="angel" # service name with the first letter in lowercase
serviceName="Angel" # service name
serviceUser="root" # OS user name for the service
serviceGroup="system" # OS group name for the service
applDir="/usr/cachesys/od_bk/Server_Platform" # home directory of the service application
serviceUserHome="$appldir/$serviceUser" # home directory of the service user
serviceLogFile="/$applDir/$serviceNameLo.log" # log file for StdOut/StdErr
maxShutdownTime=15 # maximum number of seconds to wait for the daemon to terminate normally
pidFile="/var/run/$serviceNameLo.pid" # name of PID file (PID = process ID number)
cd $applDir
javaCommand="java" # name of the Java launcher without the path
javaExe="$JAVA_HOME/bin/$javaCommand" # file name of the Java application launcher executable
javaArgs="-Dpidfile=$pidFile -jar -Xms512m -Xmx1024m ./Opensync-angel-services-1.3.0.jar angel.plugin.Main" # arguments for Java launcher
javaCommandLine="$javaExe $javaArgs" # command line to start the Java service application
#javaCommandLineKeyword="Opensync-angel-services-1.3.0.jar angel.plugin.Main" # a keyword that occurs on the commandline, used to detect an already running service process and to distinguish it from others

case "$1" in
start )
$javaCommandLine start
;;
stop )
$javaCommandLine stop
;;
* )
echo "Usage: $0 (start | stop)"
exit 1
esac


redssr
 
Old 10-01-2013, 09:45 AM   #2
Firerat
Senior Member
 
Registered: Oct 2008
Distribution: Debian Jessie / sid
Posts: 1,331

Rep: Reputation: 400Reputation: 400Reputation: 400Reputation: 400Reputation: 400
Please use Code tags

[code]
Like this
[/code]


Code:
#!/usr/bin/ksh
##################################################
# name: opensyncserver
# purpose: script that will start or stop the opensync daemon.
#################################################
# set PATH
PATH=/usr/bin/macros:/bin:/usr/bin:/usr/sbin:/etc:/usr/ucb:$HOME/bin:/usr/bin/X11:/usr/java6/bin/:.

# Set this to your Java installation
JAVA_HOME=/usr/java6

serviceNameLo="angel" # service name with the first letter in lowercase
serviceName="Angel" # service name
serviceUser="root" # OS user name for the service
serviceGroup="system" # OS group name for the service
applDir="/usr/cachesys/od_bk/Server_Platform" # home directory of the service application
serviceUserHome="$appldir/$serviceUser" # home directory of the service user
serviceLogFile="/$applDir/${serviceNameLo}.log" # log file for StdOut/StdErr
maxShutdownTime=15 # maximum number of seconds to wait for the daemon to terminate normally
pidFile="/var/run/${serviceNameLo}.pid" # name of PID file (PID = process ID number)
cd $applDir
javaCommand="java" # name of the Java launcher without the path
javaExe="$JAVA_HOME/bin/$javaCommand" # file name of the Java application launcher executable
javaArgs="-Dpidfile=$pidFile -jar -Xms512m -Xmx1024m ./Opensync-angel-services-1.3.0.jar angel.plugin.Main" # arguments for Java launcher
javaCommandLine="$javaExe $javaArgs" # command line to start the Java service application
#javaCommandLineKeyword="Opensync-angel-services-1.3.0.jar angel.plugin.Main" # a keyword that occurs on the commandline, used to detect an already running service process and to distinguish it from others

case "$1" in
    start )
       $javaCommandLine start
    ;;
    stop )
       $javaCommandLine stop
    ;;
    * )
       echo "Usage: $0 (start | stop)"
       exit 1
    ;;
esac
untested, and I only looked for the obvious
 
Old 10-02-2013, 04:30 AM   #3
cliffordw
Member
 
Registered: Jan 2012
Location: South Africa
Posts: 250

Rep: Reputation: 107Reputation: 107
Hi,

I'm not a Java programmer, but can help with the AIX / script part of it.

Why do you say the problem is with the pidfile? How is your application trying to use this? What errors do you get when you try and start it?

As an aside, it's generally not a good idea to run things as root if not absolutely necessary (i.e. it needs to do things that only root can do).

Regards,

Clifford

---------- Post added 2013-10-02 at 11:30 ----------

You might also find some ideas at http://stackoverflow.com/questions/5...a-java-program.
 
1 members found this post helpful.
Old 10-02-2013, 07:15 AM   #4
Firerat
Senior Member
 
Registered: Oct 2008
Distribution: Debian Jessie / sid
Posts: 1,331

Rep: Reputation: 400Reputation: 400Reputation: 400Reputation: 400Reputation: 400
Code:
serviceLogFile="/$applDir/${serviceNameLo}.log" # log file for StdOut/StdErr
maxShutdownTime=15 # maximum number of seconds to wait for the daemon to terminate normally
pidFile="/var/run/${serviceNameLo}.pid"
the color wasn't clear
 
Old 10-03-2013, 03:46 AM   #5
redssr
Member
 
Registered: Oct 2013
Location: Dist Nasik MH State Country India
Distribution: Linux
Posts: 32

Original Poster
Rep: Reputation: Disabled
Hi,

Friends,

Thanks for the prompt reply.

Actually it was my mistake in clarification..

My problem is when i run the script the application gets started, but not as per my expectations. Means the application gets started but there is no pid file generated, as result when i try to stop the application, instead of stoping the script tries to start it again even i am passing the cmd line option as ,

./opensyncserver stop

Here is the output of the cmd.
./opensyncserver start
Server is listening on port 3636
START Server started
***Invoke Login service
URL :http://isync.nextdayappointments.com...bservice/login
Connected to Application Database
Connected to Application Database
Connected to Client Database


./opensyncserver stop
openSync is already running. No need to duplicate.

There is no PID file also in the path mentioned accroding to script.
pidFile="/var/run/$serviceNameLo.pid"

ls -lrt /var/run

total 0

I tried your options

serviceLogFile="/$applDir/${serviceNameLo}.log"
pidFile="/var/run/${serviceNameLo}.pid"

but no difference.

I had one more script which runs perfectly in Linux but gives syntax error in AIX

Here is the other script which i use in Linux. and changed the shell in AiX from /bin/bash to /usr/bin/ksh before running it in AIX
#!/usr/bin/ksh
#
# chkconfig: 345 99 05
# description: Java deamon script
#
# A non-SUSE Linux start/stop script for Java daemons.
#
# Derived from -
# Home page: http://www.source-code.biz
# License: GNU/LGPL (http://www.gnu.org/licenses/lgpl.html)
# Copyright 2006 Christian d'Heureuse, Inventec Informatik AG, Switzerland.
#
# History:
# 2010-09-21 Josh Davis: Changed 'sudo' to 'su', fix some typos, removed unused variables
# 2009-03-04 Josh Davis: Ubuntu/Redhat version.
# 2006-06-27 Christian d'Heureuse: Script created.
# 2006-07-02 chdh: Minor improvements.
# 2006-07-10 chdh: Changes for SUSE 10.0.


# Set this to your Java installation
#JAVA_HOME=/usr/lib/jvm/java-1.6.0-openjdk.x86_64 #FOR LINUX
JAVA_HOME=/usr/java6 #FOR AIX

serviceNameLo="opensyncserver" # service name with the first letter in lowercase
serviceName="opensync" # service name
serviceUser="root" # OS user name for the service
serviceGroup="root" # OS group name for the service
applDir="/root/Server_Platform" # home directory of the service application
#serviceUserHome="/home/$serviceUser" # home directory of the service user
serviceLogFile="/var/log/$serviceNameLo.log" # log file for StdOut/StdErr
maxShutdownTime=15 # maximum number of seconds to wait for the daemon to terminate normally
pidFile="/var/run/$serviceNameLo.pid" # name of PID file (PID = process ID number)
javaCommand="java" # name of the Java launcher without the path
javaExe="$JAVA_HOME/bin/$javaCommand" # file name of the Java application launcher executable
javaArgs="-jar $applDir/" # arguments for Java launcher
javaCommandLine="$javaExe $javaArgs" # command line to start the Java service application

# Makes the file $1 writable by the group $serviceGroup.
function makeFileWritable {
local filename="$1"
touch $filename || return 1
chgrp $serviceGroup $filename || return 1
chmod g+w $filename || return 1
return 0; }

# Returns 0 if the process with PID $1 is running.
function checkProcessIsRunning {
local pid="$1"
if [ -z "$pid" -o "$pid" == " " ]; then return 1; fi
if [ ! -e /proc/$pid ]; then return 1; fi
return 0; }

# Returns 0 if the process with PID $1 is our Java service process.
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; }

# Returns 0 when the service is running and sets the variable $pid to the PID.
function getServicePID {
if [ ! -f $pidFile ]; then return 1; fi
pid="$(<$pidFile)"
checkProcessIsRunning $pid || return 1
checkProcessIsOurService $pid || return 1
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"
su -m $serviceUser -s $SHELL -c "$cmd" || return 1
sleep 0.1
pid="$(<$pidFile)"
if checkProcessIsRunning $pid; then :; else
echo -ne "\n$serviceName start failed, see logfile."
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 -e "\n$serviceName did not terminate within $maxShutdownTime seconds, sending SIGKILL..."
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 "Error: $serviceName could not be stopped within $maxShutdownTime+$killWaitTime seconds!"
return 1; }

function startService {
getServicePID
if [ $? -eq 0 ]; then echo -n "$serviceName is already running"; RETVAL=0; return 0; fi
echo -n "Starting $serviceName "
startServiceProcess
if [ $? -ne 0 ]; then RETVAL=1; echo "failed"; return 1; fi
echo "started PID=$pid"
RETVAL=0
return 0; }

function stopService {
getServicePID
if [ $? -ne 0 ]; then echo -n "$serviceName is not running"; RETVAL=0; echo ""; return 0; fi
echo -n "Stopping $serviceName "
stopServiceProcess
if [ $? -ne 0 ]; then RETVAL=1; echo "failed"; return 1; fi
echo "stopped PID=$pid"
RETVAL=0
return 0; }

function checkServiceStatus {
echo -n "Checking for $serviceName: "
if getServicePID; then
echo "running PID=$pid"
RETVAL=0
else
echo "stopped"
RETVAL=3
fi
return 0; }

function main {
RETVAL=0
case "$1" in
start) # starts the Java program as a Linux service
startService
;;
stop) # stops the Java program service
stopService
;;
restart) # stops and restarts the service
stopService && startService
;;
status) # displays the service status
checkServiceStatus
;;
*)
echo "Usage: $0 {start|stop|restart|status}"
exit 1
;;
esac
exit $RETVAL
}

main $1

Here is the Error which i gets in AIX when I run this second script

0403-057 Syntax error at line 86 : `(' is not expected.

Kindly suggest.
Or it will be greatful if yo can provide me a script accroding to your experties

Thanks and Regards,

redssr

Last edited by redssr; 10-03-2013 at 04:02 AM.
 
Old 10-03-2013, 04:15 AM   #6
colucix
Moderator
 
Registered: Sep 2003
Location: Bologna
Distribution: CentOS 6.5 OpenSuSE 12.3
Posts: 10,458

Rep: Reputation: 1941Reputation: 1941Reputation: 1941Reputation: 1941Reputation: 1941Reputation: 1941Reputation: 1941Reputation: 1941Reputation: 1941Reputation: 1941Reputation: 1941
Quote:
Originally Posted by redssr View Post
Here is the Error which i gets in AIX when I run this second script

0403-057 Syntax error at line 86 : `(' is not expected.
Old versions of the Korn Shell don't support the arithemtic syntax of the for loop:
Code:
for ((i=0; i<maxShutdownTime*10; i++))
do
  :
done
You have to change it into:
Code:
for i in $(seq 0 $((maxShutdownTime*10)))
do
  :
done
 
Old 10-03-2013, 05:11 AM   #7
redssr
Member
 
Registered: Oct 2013
Location: Dist Nasik MH State Country India
Distribution: Linux
Posts: 32

Original Poster
Rep: Reputation: Disabled
Hi,

Again,

Thanks for reply, I made the changes according to ur suggession. Now i got the new error as bellow,

./opensync_linux start
-n Starting opensync
sh: root: not found.
failed

The log file is empty.

Regards,

redssr
 
Old 10-03-2013, 05:35 AM   #8
Firerat
Senior Member
 
Registered: Oct 2008
Distribution: Debian Jessie / sid
Posts: 1,331

Rep: Reputation: 400Reputation: 400Reputation: 400Reputation: 400Reputation: 400
Code:
ksh -x opensync_linux start
post output

and please use [code]
Your Code here
[/code]
 
Old 10-03-2013, 06:01 AM   #9
redssr
Member
 
Registered: Oct 2013
Location: Dist Nasik MH State Country India
Distribution: Linux
Posts: 32

Original Poster
Rep: Reputation: Disabled
Hi,

Here is the output of the cmd.

ksh -x ./opensync_linux start
+ JAVA_HOME=/usr/java6
+ serviceNameLo=opensyncserver
+ serviceName=opensync
+ serviceUser=root
+ serviceGroup=system
+ applDir=/usr/cachesys/od_bk/Server_Platform
+ serviceLogFile=/usr/cachesys/od_bk/Server_Platform/opensyncserver.log
+ maxShutdownTime=15
+ pidFile=/var/run/opensyncserver.pid
+ javaCommand=java
+ javaExe=/usr/java6/bin/java
+ javaArgs=-Dpidfile=/var/run/opensyncserver.pid -jar ./Opensync-server-platform-1.3.0.jar com.qarea.opendoc.serveR.Application
+ main start
-n Starting opensync
sh: root: not found.
failed

Thanks and Regards,

redssr
 
Old 10-03-2013, 06:18 AM   #10
Firerat
Senior Member
 
Registered: Oct 2008
Distribution: Debian Jessie / sid
Posts: 1,331

Rep: Reputation: 400Reputation: 400Reputation: 400Reputation: 400Reputation: 400
ok, again but

change
Code:
su -m $serviceUser -s $SHELL -c "$cmd" || return 1
to
Code:
su -c "$cmd" || return 1
AND do not ignore my request for code tags

[code]
Your Code here
[/code]
 
Old 10-03-2013, 07:08 AM   #11
redssr
Member
 
Registered: Oct 2013
Location: Dist Nasik MH State Country India
Distribution: Linux
Posts: 32

Original Poster
Rep: Reputation: Disabled
Hi,

I worked but not as expected. Kindly find the output.

ksh -x opensync_linux start
+ JAVA_HOME=/usr/java6
+ serviceNameLo=opensyncserver
+ serviceName=opensync
+ serviceUser=root
+ serviceGroup=system
+ applDir=/usr/cachesys/od_bk/Server_Platform
+ serviceLogFile=/usr/cachesys/od_bk/Server_Platform/opensyncserver.log
+ maxShutdownTime=15
+ pidFile=/var/run/opensyncserver.pid
+ javaCommand=java
+ javaExe=/usr/java6/bin/java
+ javaArgs=-Dpidfile=/var/run/opensyncserver.pid -jar ./Opensync-server-platform-1.3.0.jar com.qarea.opendoc.server.Application
+ main start
-n Starting opensync
sleep: 0509-020 Specify time as a positive integer.
started PID=9044116
(ADS-BACKUP SERV) /usr/cachesys/od_bk/Server_Platform # ls -lrt /var/run/
total 8
-rw-rw-r-- 1 root system 8 Oct 03 08:02 opensyncserver.pid

ksh -x opensync_linux stop
+ JAVA_HOME=/usr/java6
+ serviceNameLo=opensyncserver
+ serviceName=opensync
+ serviceUser=root
+ serviceGroup=system
+ applDir=/usr/cachesys/od_bk/Server_Platform
+ serviceLogFile=/usr/cachesys/od_bk/Server_Platform/opensyncserver.log
+ maxShutdownTime=15
+ pidFile=/var/run/opensyncserver.pid
+ javaCommand=java
+ javaExe=/usr/java6/bin/java
+ javaArgs=-Dpidfile=/var/run/opensyncserver.pid -jar ./Opensync-server-platform-1.3.0.jar com.qarea.opendoc.server.Application
+ main stop
-n opensync is not running

ls -lrt /var/run
total 8
-rw-rw-r-- 1 root system 8 Oct 03 08:02 opensyncserver.pid

cat opensyncserver.log
Usage: nohup { -p pid | Command [ Argument... ] }

Not able to understand this strange output

Regards,

redssr
 
Old 10-03-2013, 07:24 AM   #12
Firerat
Senior Member
 
Registered: Oct 2008
Distribution: Debian Jessie / sid
Posts: 1,331

Rep: Reputation: 400Reputation: 400Reputation: 400Reputation: 400Reputation: 400
seriously?

you again ignore my request for code tags.....

change
Code:
cmd="nohup $javaCommandLine >>$serviceLogFile 2>&1 & echo \$! >$pidFile"
Code:
cmd="$javaCommandLine >>$serviceLogFile 2>&1 |& echo \$! >$pidFile"

and why was the script switched from sh/bash to ksh?

it is written for sh,
and bash hasn't needed nohup since version 4 ( see builtin 'coproc' )
ksh has had co-processes for a long time ( the |& starts a coprocess )

Last edited by Firerat; 10-03-2013 at 07:30 AM.
 
Old 10-03-2013, 07:27 AM   #13
redssr
Member
 
Registered: Oct 2013
Location: Dist Nasik MH State Country India
Distribution: Linux
Posts: 32

Original Poster
Rep: Reputation: Disabled
Sorry,

Friends,

Its my mistake.

Its started but not getting stoped.

Kindly find the output

ksh -x opensync_linux stop
+ JAVA_HOME=/usr/java6
+ serviceNameLo=opensyncserver
+ serviceName=opensync
+ serviceUser=root
+ serviceGroup=system
+ applDir=/usr/cachesys/od_bk/Server_Platform
+ serviceLogFile=/usr/cachesys/od_bk/Server_Platform/opensyncserver.log
+ maxShutdownTime=15
+ pidFile=/var/run/opensyncserver.pid
+ javaCommand=java
+ javaExe=/usr/java6/bin/java
+ javaArgs= -jar /usr/cachesys/od_bk/Server_Platform/Opensync-server-platform-1.3.0.jar com.qarea.opendoc.server.Application
+ javaCommandLine=/usr/java6/bin/java -jar /usr/cachesys/od_bk/Server_Platform/Opensync-server-platform-1.3.0.jar com.qarea.opendoc.server.Application
+ main stop
ps: Not a recognized flag: -
Usage: ps [-AMNZaedfklm] [-n namelist] [-F Format] [-o specifier[=header],...]
[-p proclist][-G|-g grouplist] [-t termlist] [-U|-u userlist] [-c classlist] [ -T pid] [ -L pidlist ]
[-@ [wparname] ]
Usage: ps [aceglnsuvwxX] [t tty] [processnumber]
-n opensync is not running

Regards,

redssr
 
Old 10-03-2013, 07:32 AM   #14
Firerat
Senior Member
 
Registered: Oct 2008
Distribution: Debian Jessie / sid
Posts: 1,331

Rep: Reputation: 400Reputation: 400Reputation: 400Reputation: 400Reputation: 400
until you use code tags I will not help.
 
Old 10-03-2013, 07:43 AM   #15
redssr
Member
 
Registered: Oct 2013
Location: Dist Nasik MH State Country India
Distribution: Linux
Posts: 32

Original Poster
Rep: Reputation: Disabled
Hi,

Friend,

Sorry for misunderstanding. I am editing the script at every moment as per your suggestion, If you want i can paste the script here.

Kindly find the script.

cat opensync_linux
#!/bin/ksh
#
# chkconfig: 345 99 05
# description: Java deamon script
#
# A non-SUSE Linux start/stop script for Java daemons.
#
# Derived from -
# Home page: http://www.source-code.biz
# License: GNU/LGPL (http://www.gnu.org/licenses/lgpl.html)
# Copyright 2006 Christian d'Heureuse, Inventec Informatik AG, Switzerland.
#
# History:
# 2010-09-21 Josh Davis: Changed 'sudo' to 'su', fix some typos, removed unused variables
# 2009-03-04 Josh Davis: Ubuntu/Redhat version.
# 2006-06-27 Christian d'Heureuse: Script created.
# 2006-07-02 chdh: Minor improvements.
# 2006-07-10 chdh: Changes for SUSE 10.0.


# Set this to your Java installation
JAVA_HOME=/usr/java6

serviceNameLo="opensyncserver" # service name with the first letter in lowercase
serviceName="opensync" # service name
serviceUser="root" # OS user name for the service
serviceGroup="system" # OS group name for the service
applDir="/usr/cachesys/od_bk/Server_Platform" # home directory of the service application
#serviceUserHome="/home/$serviceUser" # home directory of the service user
serviceLogFile="$applDir/$serviceNameLo.log" # log file for StdOut/StdErr
maxShutdownTime=15 # maximum number of seconds to wait for the daemon to terminate normally
pidFile="/var/run/$serviceNameLo.pid" # name of PID file (PID = process ID number)
javaCommand="java" # name of the Java launcher without the path
javaExe="$JAVA_HOME/bin/$javaCommand" # file name of the Java application launcher executable
javaArgs=" -jar $applDir/Opensync-server-platform-1.3.0.jar com.qarea.opendoc.server.Application"
javaCommandLine="$javaExe $javaArgs" # command line to start the Java service application

# Makes the file $1 writable by the group $serviceGroup.
function makeFileWritable {
local filename="$1"
touch $filename || return 1
chgrp $serviceGroup $filename || return 1
chmod g+w $filename || return 1
return 0; }

# Returns 0 if the process with PID $1 is running.
function checkProcessIsRunning {
local pid="$1"
if [ -z "$pid" -o "$pid" == " " ]; then return 1; fi
if [ ! -e /proc/$pid ]; then return 1; fi
return 0; }

# Returns 0 if the process with PID $1 is our Java service process.
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; }

# Returns 0 when the service is running and sets the variable $pid to the PID.
function getServicePID {
if [ ! -f $pidFile ]; then return 1; fi
pid="$(<$pidFile)"
checkProcessIsRunning $pid || return 1
checkProcessIsOurService $pid || return 1
return 0; }

function startServiceProcess {
cd $applDir || return 1
rm -f $pidFile
makeFileWritable $pidFile || return 1
makeFileWritable $serviceLogFile || return 1
cmd="$javaCommandLine >>$serviceLogFile 2>&1 |& echo \$! >$pidFile"
# cmd="nohup $javaCommandLine >>$serviceLogFile 2>&1 & echo \$! >$pidFile"
su -c "$cmd" || return 1
# su -m $serviceUser -s $SHELL -c "$cmd" || return 1
sleep 0.1
pid="$(<$pidFile)"
if checkProcessIsRunning $pid; then :; else
echo -ne "\n$serviceName start failed, see logfile."
return 1
fi
return 0; }

function stopServiceProcess {
kill $pid || return 1
for i in $(seq 0 $((maxShutdownTime*10)))
do
checkProcessIsRunning $pid
if [ $? -ne 0 ]; then
rm -f $pidFile
return 0
fi
sleep 0.1
done
echo -e "\n$serviceName did not terminate within $maxShutdownTime seconds, sending SIGKILL..."
kill -s KILL $pid || return 1
local killWaitTime=15
for i in $(seq 0 $((maxShutdownTime*10)))
do
checkProcessIsRunning $pid
if [ $? -ne 0 ]; then
rm -f $pidFile
return 0
fi
sleep 0.1
done
echo "Error: $serviceName could not be stopped within $maxShutdownTime+$killWaitTime seconds!"
return 1; }

function startService {
getServicePID
if [ $? -eq 0 ]; then echo -n "$serviceName is already running"; RETVAL=0; return 0; fi
echo -n "Starting $serviceName "
startServiceProcess
if [ $? -ne 0 ]; then RETVAL=1; echo "failed"; return 1; fi
echo "started PID=$pid"
RETVAL=0
return 0; }

function stopService {
getServicePID
if [ $? -ne 0 ]; then echo -n "$serviceName is not running"; RETVAL=0; echo ""; return 0; fi
echo -n "Stopping $serviceName "
stopServiceProcess
if [ $? -ne 0 ]; then RETVAL=1; echo "failed"; return 1; fi
echo "stopped PID=$pid"
RETVAL=0
return 0; }

function checkServiceStatus {
echo -n "Checking for $serviceName: "
if getServicePID; then
echo "running PID=$pid"
RETVAL=0
else
echo "stopped"
RETVAL=3
fi
return 0; }

function main {
RETVAL=0
case "$1" in
start) # starts the Java program as a Linux service
startService
;;
stop) # stops the Java program service
stopService
;;
restart) # stops and restarts the service
stopService && startService
;;
status) # displays the service status
checkServiceStatus
;;
*)
echo "Usage: $0 {start|stop|restart|status}"
exit 1
;;
esac
exit $RETVAL
}

main $1

Regards,

redssr
 
  


Reply


Thread Tools Search this Thread
Search this Thread:

Advanced Search

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
script to delete the user define input Hariharan Linux - Software 5 10-29-2010 02:05 AM
define a variable outside of a *.sh script... Mike_V Programming 11 09-10-2009 07:24 AM
iptables script - How can I define a dynamic IPaddress theinfidel Linux - Networking 2 12-26-2008 10:47 AM
PidFile JJX Linux - Newbie 4 04-30-2004 04:18 PM
mldonkey pidfile? White R4bbit Debian 0 01-14-2004 06:05 PM


All times are GMT -5. The time now is 06:20 PM.

Main Menu
My LQ
Write for LQ
LinuxQuestions.org is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
Syndicate
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
identi.ca: @linuxquestions
Facebook: linuxquestions Google+: linuxquestions
Open Source Consulting | Domain Registration