Kanwar Ranbir Sandhu wrote:
On Mon, 2005-21-11 at 13:19 +0000, Paul Howarth wrote:
I had the same problem when creating initscripts for the bittorrent
package in Fedora Extras.
See:
http://cvs.fedora.redhat.com/viewcvs/devel/bittorrent/btseed.init?root=extras&view=markup
Thanks for the link, Paul: it was exactly what I needed.
I incorporated what you did into the script I already had, and now it's
working great. I'm able to start, stop, restart, and check the status
of the service. I also added some tests for running/stopped processes.
I'm not a bash expert, which was adding to my frustration. However,
I'll hopefully have that remedied soon as I'm currently learning the ins
and outs of it.
Just one question: what exactly is happening? As far as I can tell, a
sub-shell is started with the application running under that shell. The
entire thing is then put into the background.
The "start" procedure is (after variable substitution):
echo -n $"Starting BitTorrent seed client: "
runuser -s /bin/sh -c "btseed /srv/bittorrent/data" torrent >>
/var/log/btseed.log 2>&1 &
disown -ar
usleep 500000
status btseed &> /dev/null && echo_success || echo_failure
RETVAL=$?
[ $RETVAL -eq 0 ] && touch /var/lock/subsys/btseed
echo
So, the gist of this is:
* display a message showing which process is starting
* use "runuser" to start a subshell running as user "torrent", and have
that shell run the application "btseed" with parameter
"/srv/bittorrent/data"
* the "disown -a" causes the current shell to "forget" about all of its
background jobs (such as the one just started). This is done so that if
someone runs this initscript from inside say an ssh session, the ssh
session doesn't hang if it's logged out.
* the "usleep" waits half a second, which gives time for the background
processes to start up before we check to see if "btseed" is running properly
* the "status" function checks to see if the process "btseed" is running
properly and returns an appropriate exit status; this is then used to
display "[ OK ]" or "[FAILED]" on the console.
* if the program started up properly, we create the lockfile
/var/lock/subsys/btseed to indicate that the process should be in a
running state. This is used by the "status" function of the initscript.
> I listed the running
> processes, and had two PIDs reported, one for the shell itself and
> another for the actual application. I'm assuming that the shell exits
> when the application is killed with 'killproc $prog'. Is that about
> right?
Yes, shells terminate when the command they are running exits/is killed.
Paul.