Created
April 6, 2017 09:30
-
-
Save Galigator/2d82e101acf5c80b8022bc45a46984e4 to your computer and use it in GitHub Desktop.
Transform a script a linux demon
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| #!/usr/bin/perl | |
| # ./demon_generator.pl graphdb /home/jarvis/graphdb.sh 'graphdb demon' 'graphdb demon' > mydemon | |
| # sudo mv mydemon /etc/init.d | |
| # sudo chmod u+x mydemon | |
| # sudo update-rc.d mydemon defaults | |
| # sudo service mydemon start | |
| use strict; | |
| use warnings; | |
| my ($name, $path, $shortDescription, $description, @params) = @ARGV or die "usage ./demon_generator.pl name path shortDescription description parameters"; | |
| my $userVar = "root"; | |
| my $ulimitVar = $name . "_ULIMIT_NOFILE"; | |
| print "\ | |
| #!/bin/sh | |
| ### BEGIN INIT INFO | |
| # Provides: $name | |
| # Required-Start: \$remote_fs \$syslog \$network | |
| # Required-Stop: \$remote_fs \$syslog | |
| # Default-Start: 2 3 4 5 | |
| # Default-Stop: 0 1 6 | |
| # Short-Description: $shortDescription | |
| # Description: $description | |
| ### END INIT INFO | |
| PATH=/sbin:/usr/sbin:/bin:/usr/bin | |
| NAME=$name | |
| DAEMON=$path | |
| PIDDIR=/var/run/\${NAME} | |
| PIDFILE=\${PIDDIR}/$name.pid | |
| SCRIPTNAME=/etc/init.d/\$NAME | |
| [ -x \"\$DAEMON\" ] || exit 0 | |
| [ -r /etc/default/\$NAME ] && . /etc/default/\$NAME | |
| [ -n $userVar ] || $userVar=\$NAME | |
| . /lib/lsb/init-functions | |
| do_start() | |
| { | |
| do_ulimit | |
| [ -d \"\${PIDDIR}\" ] || mkdir -p \"\${PIDDIR}\" | |
| chown \"\$userVar:\" \"\${PIDDIR}\" | |
| start-stop-daemon --chuid $userVar --start --quiet --pidfile \$PIDFILE --exec \$DAEMON -- @params & | |
| } | |
| do_stop() | |
| { | |
| \$DAEMON stop | |
| } | |
| do_ulimit() | |
| { | |
| if [ -n \"\${$ulimitVar}\" ]; then | |
| ulimit -n \"\${$ulimitVar}\" | |
| fi | |
| } | |
| case \"\$1\" in | |
| start) | |
| do_start | |
| ;; | |
| stop) | |
| do_stop | |
| ;; | |
| status) | |
| status_of_proc -p \$PIDFILE \"\$DAEMON\" \"\$NAME\" | |
| ;; | |
| restart|force-reload) | |
| do_stop && do_start | |
| ;; | |
| *) | |
| echo \"Usage: \$SCRIPTNAME {start|stop|status|restart|force-reload}\" >&2 | |
| exit 3 | |
| ;; | |
| esac | |
| "; |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment