[lug] How to tell if an app is running/port is in use?
D. Stimits
stimits at idcomm.com
Mon Mar 11 14:59:21 MST 2002
Alan Robertson wrote:
>
> Hi,
>
> I'm writing an System V init script for an app which doesn't have one.
>
> For the "status" case, I want to tell if the application is running.
>
> This is a little hard since the application doesn't appear to make any lock
> files, and is all in Java, so the ps always shows "java" as the process name.
>
> Another option would be to look and see if the app had it's ports open (it's
> a server which waits for clients to connect).
The following finds out what is using a particular port for either tcp
or udp:
fuser -v -u -n tcp "somePortNumber" -n udp "somePortNumber"
You can modify that to just show tcp or udp. It might or might not give
all you need.
>
> What's the best way to do that?
>
> Thanks!
>
> -- Alan Robertson
> alanr at unix.sh
>
> _______________________________________________
> Web Page: http://lug.boulder.co.us
> Mailing List: http://lists.lug.boulder.co.us/mailman/listinfo/lug
I'm currently playing around with Redhat init scripts (I'm using 7.1),
here is a snippet to allow creation of such a script, but it is only an
excerpt, you'll maybe find some of it useful (it was originally from
other scripts I've hacked around with). It's purpose is for a network
game server, so it might have a lot in common. It works with lock files
even though the program itself does not have a concept of the lock
files. Adapt to your favorite distro. Forgive the line wrap, email is
notorious for that):
. /etc/init.d/functions
. /etc/sysconfig/network
[ ${NETWORKING} = "no" ] && exit 0
application="your_app"
the_path="/somewhere"
user="who_to_run_as"
lock_file="/var/lock/subsys/${application}
start()
{
echo -n $"Starting $prog: "
ulimit -S -c 0 >/dev/null 2>&1
local proc_pid=$(pidof ${application})
# case already started.
if [ ! -z "${proc_pid}" ] ; then
success $"(${application} already started, pid ${proc_pid}) startup"
touch "${lock_file}"
exit 0
# case not started but lock file exists.
elif [ -f "${lock_file}" ] ; then
success "rm -f ${lock_file}" && passed "${lock_file} locked but not
alive, removing lock"
fi
# At this point, the server is neither locked nor started.
daemon --user ${user_name} ${the_path}/${application} \
&& success $"${application} startup (pid $(pidof ${application}))"
proc_pid=$(pidof ${binary_ex})
# Case that startup attempt failed.
if [ -z "${proc_pid}" ] ; then
failure "${application} startup"
rm -f "${lock_file}"
echo -n '' > "${pid_file}"
exit 1
# Case of startup success.
else
touch "${lock_file}"
echo "${proc_pid}" > "${pid_file}"
fi
exit 0
}
stop()
{
local proc_pid=$(pidof ${application})
if [ -n "${proc_pid}" ] ; then
killproc ${application}
echo -n '' > "${pid_file}"
rm -f "${lock_file}"
fi
}
restart()
{
local proc_pid=$(pidof ${application})
if [ -n "${proc_pid}" ] ; then
killproc ${application} -HUP
touch "${lock_file}"
echo "${proc_pid}" > "${pid_file}"
else
start
fi
}
case "$1" in
start)
start
;;
stop)
stop
;;
restart)
restart
;;
status)
status ${binary_ex}
;;
describe)
describe
;;
*)
echo "Usage: ${binary_ex} <start|stop|status|restart|describe>"
esac
More information about the LUG
mailing list