How to register an app as a service?

Red Squirrel

No Lifer
May 24, 2003
70,663
13,834
126
www.anyf.ca
How do I go about making an app act as a service? I want to be able to do "service myapp start" and it starts and runs in the background and "service myapp stop" and it stops, etc. When it stops, I want it to call some kind of procedure so my program can handle the stop and close properly, not just be killed. I'm not worried about making it work on debian based systems for now, but maybe later I'll tackle that service system as well.

Was not sure if I should put this in programming or here as I'm guessing it involves lot of programming to make the app work that way.
 

Colt45

Lifer
Apr 18, 2001
19,720
1
0
Look at the files in /etc/init.d/
Copy one of the scripts, modify to your app. I think there's usually a skeleton example in there...

then you can use like:

/etc/init.d/myappname (start | stop | restart | etc)
 

jvroig

Platinum Member
Nov 4, 2009
2,394
1
81
Right. Assuming you are running a RedHat-based Linux machine:

1.) Go to /etc/rc.d/init.d (or just /etc/init.d, same thing, it's a link to rc.d/init.d)
2.) Paste your script there. A sample script would be:

Code:
#!/bin/bash
#
# Run-level Startup script for XAMPP
#
# chkconfig: 0123456 91 19
# description: Startup/Shutdown service for XAMPP

case "$1" in
    start)
        echo "Starting XAMPP: "
        /opt/lampp/lampp start
        ;;
    stop)
        echo "Shutdown XAMPP: "
        /opt/lampp/lampp stop
        ;;
    restart)
        echo "Restarting XAMPP: "
        /opt/lampp/lampp restart
        ;;
    *)
        echo "XAMPP Startup/Shutdown Service"
        echo "Scripted by JV Roig"
        echo ""
        echo "XAMPP is an easy-to-use implementation of:"
        echo "   -Apache webserver with SSL support"
        echo "   -PHP (versions 4 and 5)"
        echo "   -MySQL Database Server"
        echo "   -ProFTPD and a lot of others"
        echo ""
        echo "Usage: "
        echo "service xampp start - starts the XAMPP service."
        echo "service xampp stop  - stops the XAMPP service."
        echo "service xampp restart - restarts the XAMPP service."
        echo ""
        exit 1
esac
exit 0
That's pretty basic right there. The app name in this case is "xampp" (it's a pretty redundant app when you think of it, since in modern linux distros you arleady get httpd, mysqld in the package manager, along with vsftpd... but this is ok as an example, and the only one I could get from my archive quickly, back in the days when I had to use xampp by ApacheFriends. Also, I should add that xampp is actually pretty convenient for devs who don't want to go to the hassle of configuring all the components by themselves, but as noted, modern linux distros make it almost painless already)

If you are familiar with bash scripting, then that's a wrap.

3. (Optional) If you also want your service to run automatically depending on the runlevel, you use chkconfig command after you paste your script. Assuming "xampp" is the name of your script:
Code:
chkconfig --add xampp
chkconfig --level 0123456 off
chkconfig --level 345 on
That makes it start automatically when you boot up in either in command line mode or the normal GUI mode, and the service is stopped properly when you restart or shut down. Technically, you don't really need to "chkconfig --level 0123456 off", because runlevels will be off by default, but that's just being thorough and it's just one line :D

Regards.