Firstly, Saving the PID on *nix:
$ ./yourprogram &
$ echo $! > /var/run/yourpid
yourpid will now contain yourpgram's PID, and /var/run is the proper place to put it.
The above can be put in your "start" script.
The "stop" script can look at yourpid to know what to kill.
If you want to be more elegant and stop your app properly, you can look at the source code for Tomcat's org.apache.catalina.startup.Catalina.java on how to implement proper shutdown hooks.
Secondly, above "stop" and "start" scripts can then be put in /etc/init.d/mystopstartscript:
#!/bin/bash
# processname: yourprogram
# pidfile: /var/run/yourpid
case $1 in
start)
sh /some/where/start.sh
;;
stop)
sh /some/where/stop.sh
;;
restart)
sh /some/where/stop.sh
sh /some/where/start.sh
;;
esac
exit 0
This is a fairly home-grown solution, with ideas mostly taken from good 'ol Tomcat, but I hope it helps :)