# #Services - starting stopping and logs. # # # #STARTING AND STOPING # #To demonstrate how to either keep a service going up or to kill it permanently from either the run script or the finish script, do this: mkdir -vp /tmp/testservice cat > /tmp/testservice/run << "EOF" #!/bin/sh #uncomment the next line to redirect stderr to stdout #exec 2>&1 echo "started run ..." if false;then echo "do something here with exec that doesn't finish" exec sleep 10d else echo "condition failed catastrophically" echo "Don't restart the service" #comment out the next line to make the service keep restarting echo "d" > supervise/control fi EOF cat > /tmp/testservice/finish << "EOF" #!/bin/sh echo "started finish ... " echo "run exited with code $1" #echo "d" > supervise/control EOF #Start the service cd /etc/runit/runsvdir/current sudo ln -svfn /tmp/testservice testservice #As it is the test at line 5 fails ( false always fails ) the service is then instructed to stop by line 12, the finish script will #run and the service will not restart, if you comment out line 12 and restart the service like so cd /etc/runit/runsvdir/current sudo rm /etc/runit/runsvdir/current/testservice sudo ln -svfn /tmp/testservice testservice #As runit constantly checks the service folder every few seconds services can be started/stopped just by adding removing links, #this also means you can add a service at any time just by adding a link #LOGS #A number of apps like cups etc handle their own logging and so a log folder is not needed, however it is easy enough #to add a logger either for admin or debugging, like so: mkdir -vp /tmp/testservice/log cat > /tmp/testservice/log/run << "EOF" #!/bin/bash #stdin is connected to ../run stdout while read do #echo to console AND to logfile echo -n "message from log runscript ... "|tee -a /tmp/testservice.log echo "$REPLY"|tee -a /tmp/testservice.log done EOF #Basically the input to log/run is connected to the output of run AND finish so anything either of these two scripts send to stdout #will be sent to log/run's stdin but ONLY if the log folder/files are present, if you want to capture stderr from the run/finish #scripts you should redirect stderr to stdout uncomment the line at the start of the run file 'exec ...'. # #If you have set the service to keep restarting you will find that the log file and the console will pretty soon #clog up with repeat entry's, so be aware , the simple way to prevent unwanted duplicates in the log file is just to pipe it #to sort -u. # #And that is simple logging with runit.