Poor Man's Service: Difference between revisions

From Nottinghack Wiki
Jump to navigation Jump to search
No edit summary
Elsen (talk | contribs)
No edit summary
Line 40: Line 40:


</syntaxhighlight>
</syntaxhighlight>
[[Category:Network]]

Revision as of 13:54, 12 December 2014

Ever want to run a permanent restarting service on a Unix machine as a non-root user?

I often use a screen + cron recipe to run long-lived processes where cron checks to see if it's running, restarting it if it isn't, and screen provides a handy detached interface to stdin/stdout etc.

So, let's call this service "service1" and make cron run a script every five minutes to check that it's OK...

 0,5,10,15,20,25,30,35,40,45,50,55 * * * *       /home/michael/service1/check

The "check" script looks for the associated named screen process that holds the service, in this case it's an Edgewall Trac server. If not found it starts it. Everything gets logged along with timestamps so we have a record if it ever goes down.

#!/bin/bash
# Make script crontab friendly:
# ==> Change to directory where this script lives.
cd $(dirname $0)

LOGFILE=service1.log
# want to redirect own stdout and stderr to logfile

exec >> $LOGFILE

echo `date`
echo "search for service1 server process..."
echo `ps auxww | grep "SCREEN -dmS service1" | grep -v grep`

if ! ps auxww | grep 'SCREEN -dmS service1' | grep -v grep > /dev/null
then
    echo "*** WARNING *** seems not to be running -- starting service"
    # do something smart here - launch it in detached screen
    /usr/bin/screen -dmS service1 /usr/bin/tracd --port 8000 \
        --basic-auth=tractest,/home/michael/tractest/.htpasswd,/home/michael/tractest \
        --basic-auth=swimtracproj,/home/michael/swimtrac/.htpasswd,/home/michael/swimtrac/swimtracproj \
        --basic-auth=rcvsim-proj,/home/michael/trac-rcvsim/.htpasswd,/home/michael/trac-rcvsim/rcvsim-proj \
	/home/michael/tractest \
	/home/michael/swimtrac/swimtracproj \
	/home/michael/trac-rcvsim/rcvsim-proj
else
    echo "seems to be running"
fi