This shows you the differences between two versions of the page.
add_a_custom_service [2015/05/07 18:13] stephdl [Example] |
add_a_custom_service [2019/06/05 19:48] |
||
---|---|---|---|
Line 1: | Line 1: | ||
- | ===== SME Server ===== | ||
- | |||
- | |||
- | ===Start at the boot time a custom service=== | ||
- | |||
- | If you want to start a custom service on the SME Server, you will have to follow some steps. | ||
- | The init script below is just a wrapper, a launcher that will start the real script which will do the job for you ''' | ||
- | |||
- | ''' | ||
- | |||
- | add a script to / | ||
- | |||
- | nano / | ||
- | |||
- | #!/bin/sh | ||
- | ### BEGIN INIT INFO | ||
- | # Provides: < | ||
- | # Required-Start: | ||
- | # Required-Stop: | ||
- | # Default-Start: | ||
- | # Default-Stop: | ||
- | # Description: | ||
- | ### END INIT INFO | ||
- | | ||
- | ''' | ||
- | ''' | ||
- | ''' | ||
- | | ||
- | PIDFILE=/ | ||
- | LOGFILE=/ | ||
- | | ||
- | start() { | ||
- | if [ -f $PIDFILE ] && kill -0 $(cat $PIDFILE); then | ||
- | echo ' | ||
- | return 1 | ||
- | fi | ||
- | echo ' | ||
- | local CMD=" | ||
- | su -c " | ||
- | | ||
- | # Try with this command line instead of above if not workable | ||
- | # su -s /bin/sh $RUNAS -c " | ||
- | | ||
- | sleep 2 | ||
- | PID=$(cat $PIDFILE) | ||
- | if pgrep -u $RUNAS -f $NAME > /dev/null | ||
- | then | ||
- | echo "$NAME is now running, the PID is $PID" | ||
- | else | ||
- | echo " | ||
- | fi | ||
- | | ||
- | } | ||
- | | ||
- | stop() { | ||
- | if [ ! -f " | ||
- | echo ' | ||
- | return 1 | ||
- | fi | ||
- | echo ' | ||
- | kill -15 $(cat " | ||
- | echo ' | ||
- | } | ||
- | | ||
- | uninstall() { | ||
- | echo -n "Are you really sure you want to uninstall this service? That cannot be undone. [yes|No] " | ||
- | local SURE | ||
- | read SURE | ||
- | if [ " | ||
- | stop | ||
- | rm -f " | ||
- | echo " | ||
- | update-rc.d -f < | ||
- | rm -fv " | ||
- | fi | ||
- | } | ||
- | | ||
- | status() { | ||
- | printf " | ||
- | if [ -f $PIDFILE ]; then | ||
- | PID=$(cat $PIDFILE) | ||
- | if [ -z "$(ps axf | grep ${PID} | grep -v grep)" ]; then | ||
- | printf " | ||
- | else | ||
- | echo " | ||
- | fi | ||
- | else | ||
- | printf " | ||
- | fi | ||
- | } | ||
- | | ||
- | | ||
- | case " | ||
- | start) | ||
- | start | ||
- | ;; | ||
- | stop) | ||
- | stop | ||
- | ;; | ||
- | status) | ||
- | status | ||
- | ;; | ||
- | uninstall) | ||
- | uninstall | ||
- | ;; | ||
- | restart) | ||
- | stop | ||
- | start | ||
- | ;; | ||
- | *) | ||
- | echo " | ||
- | esac | ||
- | |||
- | you have to fill these fields with the relevant values | ||
- | |||
- | < | ||
- | .. | ||
- | < | ||
- | .. | ||
- | SCRIPT=<''' | ||
- | RUNAS=<''' | ||
- | NAME=<''' | ||
- | |||
- | make that script executable | ||
- | |||
- | chmod u+x / | ||
- | |||
- | You need to say to SME Server to add the script to each run level you have specified at the top of your init script( Default-Start: | ||
- | |||
- | chkconfig YOUR_SERVICE_NAME --add | ||
- | |||
- | You can set the levels where the initscript has to start | ||
- | chkconfig YOUR_SERVICE_NAME --level 2345 on | ||
- | |||
- | If you want to see which runlevel your script will run in | ||
- | |||
- | chkconfig YOUR_SERVICE_NAME --list | ||
- | |||
- | example : | ||
- | # chkconfig dhcp-dns --list | ||
- | dhcp-dns | ||
- | |||
- | |||
- | <WRAP center round tip 60%> | ||
- | However it is not enough for SME server since we have to add service to rc7.d to say to our distro to start the service at boot, therefore let's go to the section below | ||
- | </ | ||
- | |||
- | |||
- | ===allow a service to start for a particular time=== | ||
- | If your package implements a server or daemon, you will probably want it to be started automatically when the system boots. The SME Server boots in runlevel 7, so you can get an idea of the startup processes by listing the contents of / | ||
- | |||
- | These are similar to the init scripts you may be familiar with from other Linux systems, with one important difference. Instead of pointing to scripts within / | ||
- | |||
- | So for example, you might have: | ||
- | |||
- | S90squid -> / | ||
- | |||
- | The e-smith-service script looks up the name it was invoked with (S90squid), drops the prefix (leaving squid), checks the configuration database for the " | ||
- | |||
- | / | ||
- | |||
- | * with this way SME's knows how to/if start the service at startup | ||
- | |||
- | | ||
- | |||
- | :* IF your init script is not ALREADY in etc/ | ||
- | cd / | ||
- | ln -s / | ||
- | |||
- | ''' | ||
- | |||
- | * In /etc/rc7.d we do a link to the wrapper e-smith-service | ||
- | |||
- | cd /etc/rc7.d | ||
- | ln -s / | ||
- | |||
- | we create a symlink to e-smith-service startup script with a name where: S tells SME to start XX are numbers | ||
- | |||
- | You can decide when to start the service ''' | ||
- | |||
- | signal-event remoteaccess-update | ||
- | service ''' | ||
- | ====Creating or deleting a service==== | ||
- | Some examples | ||
- | * Creating and starting service | ||
- | |||
- | ln -f -s / | ||
- | / | ||
- | / | ||
- | service popfile start | ||
- | |||
- | * Deleting and unregistering service | ||
- | |||
- | service popfile stop | ||
- | sleep 3 | ||
- | rm -f / | ||
- | rm -f / | ||
- | / | ||
- | / | ||
- | |||
- | ====Create a service with db command and set network access==== | ||
- | http:// | ||
- | |||
- | Create a custom-named service definition in the configuration database. | ||
- | |||
- | db configuration set < | ||
- | |||
- | Apply your desired firewall restrictions to any existing SME ' | ||
- | |||
- | db configuration setprop < | ||
- | db configuration setprop < | ||
- | db configuration setprop < | ||
- | db configuration setprop < | ||
- | db configuration setprop < | ||
- | db configuration setprop < | ||
- | db configuration setprop < | ||
- | db configuration setprop < | ||
- | |||
- | |||
- | Effectuate the changes you have made | ||
- | signal-event remoteaccess-update | ||
- | |||
- | === General Service Handling === | ||
- | SME Server uses [http:// | ||
- | |||
- | All other linux common way to start or stop services are also valuable | ||
- | |||
- | / | ||
- | service servicename start/ | ||
- | |||
- | *start | ||
- | sv u / | ||
- | *stop | ||
- | sv d / | ||
- | *restart | ||
- | sv t / | ||
- | * status | ||
- | sv s / | ||
- | |||
- | <WRAP center round tip 60%> | ||
- | you may use TAB to auto-complete your command line | ||
- | </ | ||
- | |||
- | |||
- | |||
- | |||
- | ====Example==== | ||
- | |||
- | Restarting: | ||
- | |||
- | sv t / | ||
- | {{tag> development }} | ||