-
-
Save X4/4369947 to your computer and use it in GitHub Desktop.
Linux service control script
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/env bash | |
| # Start/stop/restart TrustLeap G-WAN Application Server. | |
| # @date 2012-12-24 | |
| # @author Ferhat | |
| # @version 0.98 | |
| ########################################################## | |
| # This is the directory you installed gwan into | |
| DIR="/opt/gwan" | |
| # Default program name | |
| PROC="gwan" | |
| # Set how many CPUs you want to saturate (1 - n) | |
| CORES="" | |
| # Set to different User/Group to restrict privileges | |
| user="" | |
| group="" | |
| ########################################################## | |
| set -e | |
| # Set new privileges when USER & GROUP is set | |
| if [[ -n "$user" && -n "$group" ]]; then | |
| OWNER=":$group:$user" | |
| fi | |
| # Set gwan workers, when CORES is set | |
| if [[ -n "$CORES" ]]; then | |
| WORKER="-w $CORES" | |
| fi | |
| # Exit if gwan is not installed/executable: | |
| if [[ ! -e "${DIR}/${PROC}" ]]; then | |
| echo -e "Error: $1 Couldn't find ${PROC} in ${DIR}" | |
| exit 1 | |
| fi | |
| # Start gwan in daemon mode: | |
| gwan_start() { | |
| echo -n "starting G-WAN... " | |
| $DIR/$PROC -d$OWNER $WORKER | |
| echo -e "OK" | |
| } | |
| # Start gwan in development mode: | |
| gwan_test() { | |
| echo -n "starting G-WAN... " | |
| $DIR/$PROC $WORKER | |
| } | |
| # Gracefully kill all gwan processes: | |
| gwan_stop() { | |
| $DIR/$PROC -k | |
| } | |
| # Forcibly kill all gwan processes: | |
| gwan_force_stop() { | |
| killall -s TERM -g $PROC 2> /dev/null && echo "OK. Sent SIGTERM to $PROC" || echo "Process is not running." | |
| } | |
| # Restart gwan: | |
| gwan_restart() { | |
| gwan_stop && sleep 10 && gwan_start | |
| } | |
| # Forcibly restart all gwan processes: | |
| gwan_force_restart() { | |
| gwan_force_stop && sleep 10 && gwan_start | |
| } | |
| # Check if gwan is running | |
| gwan_status() { | |
| PID=$(killall -0 $PROC 2> /dev/null) | |
| if [[ "$?" -eq 0 ]]; then | |
| echo "G-WAN is running." | |
| exit 0 | |
| else | |
| echo "G-WAN is stopped." | |
| exit 1 | |
| fi | |
| } | |
| # Run general-purpose code: | |
| gwan_run() { | |
| echo -e "Running C script: $@\n" | |
| $DIR/$PROC -r $@ | |
| } | |
| case "$1" in | |
| 'start') | |
| gwan_start | |
| ;; | |
| 'stop') | |
| gwan_stop | |
| ;; | |
| 'restart') | |
| gwan_restart | |
| ;; | |
| 'status') | |
| gwan_status | |
| ;; | |
| 'run') | |
| gwan_run "${@:2}" | |
| ;; | |
| 'test') | |
| gwan_test | |
| ;; | |
| 'force-stop') | |
| gwan_force_stop | |
| ;; | |
| 'force-restart') | |
| gwan_force_restart | |
| ;; | |
| *) | |
| echo "usage $0 start|stop|restart|status|run|test|force-stop|force-restart" | |
| esac |
Author
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Fixed gwan_run() omitting additional parameters
Example: /etc/init.d/gwan run /opt/gwan/tasks/argv.c