Skip to content

Instantly share code, notes, and snippets.

@naholyr
Created December 13, 2012 09:39

Revisions

  1. naholyr revised this gist Dec 13, 2012. 1 changed file with 15 additions and 1 deletion.
    16 changes: 15 additions & 1 deletion _service.md
    Original file line number Diff line number Diff line change
    @@ -61,4 +61,18 @@ If you feel confident enough with my script, you can `sudo` the script directly:
    wget 'https://raw.github.com/gist/4275302/new-service.sh' && sudo bash new-service.sh
    ```

    Note: the cool hipsterish `curl $URL | bash` won't work here, I don't really want to check why.
    Note: the cool hipsterish `curl $URL | bash` won't work here, I don't really want to check why.

    ### Demo

    Creating the service:

    ![service-create](http://dl.dropbox.com/u/6414656/gist-4275302/service-create.png)

    Looking at service files (logs, pid):

    ![service-files](http://dl.dropbox.com/u/6414656/gist-4275302/service-files.png)

    Uninstalling service:

    ![service-uninstall](http://dl.dropbox.com/u/6414656/gist-4275302/service-uninstall.png)
  2. naholyr revised this gist Dec 13, 2012. 2 changed files with 3 additions and 1 deletion.
    2 changes: 1 addition & 1 deletion new-service.sh
    Original file line number Diff line number Diff line change
    @@ -28,7 +28,7 @@ prompt_token() {
    }

    prompt_token 'NAME' 'Service name'
    if [ -f "/etc/inid.d/$NAME" ]; then
    if [ -f "/etc/init.d/$NAME" ]; then
    echo "Error: service '$NAME' already exists"
    exit 1
    fi
    2 changes: 2 additions & 0 deletions service.sh
    Original file line number Diff line number Diff line change
    @@ -41,6 +41,8 @@ uninstall() {
    read SURE
    if [ "$SURE" = "yes" ]; then
    stop
    rm -f "$PIDFILE"
    echo "Notice: log file is not be removed: '$LOGFILE'" >&2
    update-rc.d -f <NAME> remove
    rm -fv "$0"
    fi
  3. naholyr revised this gist Dec 13, 2012. 2 changed files with 7 additions and 7 deletions.
    12 changes: 6 additions & 6 deletions new-service.sh
    Original file line number Diff line number Diff line change
    @@ -35,9 +35,9 @@ fi

    prompt_token 'DESCRIPTION' ' Description'
    prompt_token 'COMMAND' ' Command'
    prompt_token 'RUNAS' ' User'
    if ! id -u "$RUNAS" &> /dev/null; then
    echo "Error: user '$RUNAS' not found"
    prompt_token 'USERNAME' ' User'
    if ! id -u "$USERNAME" &> /dev/null; then
    echo "Error: user '$USERNAME' not found"
    exit 1
    fi

    @@ -50,14 +50,14 @@ if [ ! -w /etc/init.d ]; then
    echo "You should now type those commands as superuser to install and run your service:"
    echo ""
    echo " mv \"$SERVICE_FILE\" \"/etc/init.d/$NAME\""
    echo " touch \"/var/log/$NAME.log\" && chown \"$RUNAS\" \"/var/log/$NAME.log\""
    echo " touch \"/var/log/$NAME.log\" && chown \"$USERNAME\" \"/var/log/$NAME.log\""
    echo " update-rc.d \"$NAME\" defaults"
    echo " service \"$NAME\" start"
    else
    echo "1. mv \"$SERVICE_FILE\" \"/etc/init.d/$NAME\""
    mv -v "$SERVICE_FILE" "/etc/init.d/$NAME"
    echo "2. touch \"/var/log/$NAME.log\" && chown \"$RUNAS\" \"/var/log/$NAME.log\""
    touch "/var/log/$NAME.log" && chown "$RUNAS" "/var/log/$NAME.log"
    echo "2. touch \"/var/log/$NAME.log\" && chown \"$USERNAME\" \"/var/log/$NAME.log\""
    touch "/var/log/$NAME.log" && chown "$USERNAME" "/var/log/$NAME.log"
    echo "3. update-rc.d \"$NAME\" defaults"
    update-rc.d "$NAME" defaults
    echo "4. service \"$NAME\" start"
    2 changes: 1 addition & 1 deletion service.sh
    Original file line number Diff line number Diff line change
    @@ -9,7 +9,7 @@
    ### END INIT INFO

    SCRIPT=<COMMAND>
    RUNAS=<USER>
    RUNAS=<USERNAME>

    PIDFILE=/var/run/<NAME>.pid
    LOGFILE=/var/log/<NAME>.log
  4. naholyr revised this gist Dec 13, 2012. 1 changed file with 1 addition and 1 deletion.
    2 changes: 1 addition & 1 deletion new-service.sh
    Original file line number Diff line number Diff line change
    @@ -36,7 +36,7 @@ fi
    prompt_token 'DESCRIPTION' ' Description'
    prompt_token 'COMMAND' ' Command'
    prompt_token 'RUNAS' ' User'
    if ! id -u "RUNAS"; then
    if ! id -u "$RUNAS" &> /dev/null; then
    echo "Error: user '$RUNAS' not found"
    exit 1
    fi
  5. naholyr revised this gist Dec 13, 2012. 2 changed files with 10 additions and 7 deletions.
    13 changes: 8 additions & 5 deletions new-service.sh
    Original file line number Diff line number Diff line change
    @@ -35,9 +35,9 @@ fi

    prompt_token 'DESCRIPTION' ' Description'
    prompt_token 'COMMAND' ' Command'
    prompt_token 'USER' ' User'
    if ! id -u "$USER"; then
    echo "Error: user '$USER' not found"
    prompt_token 'RUNAS' ' User'
    if ! id -u "RUNAS"; then
    echo "Error: user '$RUNAS' not found"
    exit 1
    fi

    @@ -50,14 +50,17 @@ if [ ! -w /etc/init.d ]; then
    echo "You should now type those commands as superuser to install and run your service:"
    echo ""
    echo " mv \"$SERVICE_FILE\" \"/etc/init.d/$NAME\""
    echo " touch \"/var/log/$NAME.log\" && chown \"$RUNAS\" \"/var/log/$NAME.log\""
    echo " update-rc.d \"$NAME\" defaults"
    echo " service \"$NAME\" start"
    else
    echo "1. mv \"$SERVICE_FILE\" \"/etc/init.d/$NAME\""
    mv -v "$SERVICE_FILE" "/etc/init.d/$NAME"
    echo "2. update-rc.d \"$NAME\" defaults"
    echo "2. touch \"/var/log/$NAME.log\" && chown \"$RUNAS\" \"/var/log/$NAME.log\""
    touch "/var/log/$NAME.log" && chown "$RUNAS" "/var/log/$NAME.log"
    echo "3. update-rc.d \"$NAME\" defaults"
    update-rc.d "$NAME" defaults
    echo "3. service \"$NAME\" start"
    echo "4. service \"$NAME\" start"
    service "$NAME" start
    fi

    4 changes: 2 additions & 2 deletions service.sh
    Original file line number Diff line number Diff line change
    @@ -11,8 +11,8 @@
    SCRIPT=<COMMAND>
    RUNAS=<USER>

    PIDFILE=/var/run/<NAME>
    LOGFILE=/var/log/<NAME>
    PIDFILE=/var/run/<NAME>.pid
    LOGFILE=/var/log/<NAME>.log

    start() {
    if [ -f /var/run/$PIDNAME ] && kill -0 $(cat /var/run/$PIDNAME); then
  6. naholyr revised this gist Dec 13, 2012. 1 changed file with 10 additions and 0 deletions.
    10 changes: 10 additions & 0 deletions new-service.sh
    Original file line number Diff line number Diff line change
    @@ -28,9 +28,19 @@ prompt_token() {
    }

    prompt_token 'NAME' 'Service name'
    if [ -f "/etc/inid.d/$NAME" ]; then
    echo "Error: service '$NAME' already exists"
    exit 1
    fi

    prompt_token 'DESCRIPTION' ' Description'
    prompt_token 'COMMAND' ' Command'
    prompt_token 'USER' ' User'
    if ! id -u "$USER"; then
    echo "Error: user '$USER' not found"
    exit 1
    fi

    echo ""

    echo "--- Installation ---"
  7. naholyr revised this gist Dec 13, 2012. 2 changed files with 10 additions and 6 deletions.
    4 changes: 4 additions & 0 deletions _service.md
    Original file line number Diff line number Diff line change
    @@ -41,6 +41,10 @@ The service can uninstall itself with `service $NAME uninstall`. Yes, that's ver

    Don't want it? Remove lines 56-58 of the service's script.

    ## Logs?

    Your service will log its output to `/var/log/$NAME.log`. Don't forget to setup a logrotate :)

    ## I'm noob and/or lazy

    Yep, I'm lazy too. But still, I've written a script to automate this :)
    12 changes: 6 additions & 6 deletions service.sh
    Original file line number Diff line number Diff line change
    @@ -11,27 +11,27 @@
    SCRIPT=<COMMAND>
    RUNAS=<USER>

    PIDNAME=<NAME>

    PIDFILE=/var/run/<NAME>
    LOGFILE=/var/log/<NAME>

    start() {
    if [ -f /var/run/$PIDNAME ] && kill -0 $(cat /var/run/$PIDNAME); then
    echo 'Service already running' >&2
    return 1
    fi
    echo 'Starting service…' >&2
    local CMD="$SCRIPT &> /dev/stderr & echo \$!"
    su -c "$CMD" $RUNAS > /var/run/$PIDNAME
    local CMD="$SCRIPT &> \"$LOGFILE\" & echo \$!"
    su -c "$CMD" $RUNAS > "$PIDFILE"
    echo 'Service started' >&2
    }

    stop() {
    if [ ! -f /var/run/$PIDNAME ] || ! kill -0 $(cat /var/run/$PIDNAME); then
    if [ ! -f "$PIDFILE" ] || ! kill -0 $(cat "$PIDFILE"); then
    echo 'Service not running' >&2
    return 1
    fi
    echo 'Stopping service…' >&2
    kill -15 $(cat /var/run/$PIDNAME) && rm -f /var/run/$PIDNAME
    kill -15 $(cat "$PIDFILE") && rm -f "$PIDFILE"
    echo 'Service stopped' >&2
    }

  8. naholyr revised this gist Dec 13, 2012. 2 changed files with 31 additions and 20 deletions.
    6 changes: 6 additions & 0 deletions _service.md
    Original file line number Diff line number Diff line change
    @@ -35,6 +35,12 @@ update-rc.d $YOUR_SERVICE_NAME defaults

    Enjoy

    ## Uninstall

    The service can uninstall itself with `service $NAME uninstall`. Yes, that's very easy, therefore a bit dangerous. But as it's an auto-generated script, you can bring it back very easily. I use it for tests and often install/uninstall, that's why I've put that here.

    Don't want it? Remove lines 56-58 of the service's script.

    ## I'm noob and/or lazy

    Yep, I'm lazy too. But still, I've written a script to automate this :)
    45 changes: 25 additions & 20 deletions service.sh
    Original file line number Diff line number Diff line change
    @@ -17,7 +17,7 @@ PIDNAME=<NAME>
    start() {
    if [ -f /var/run/$PIDNAME ] && kill -0 $(cat /var/run/$PIDNAME); then
    echo 'Service already running' >&2
    exit 1
    return 1
    fi
    echo 'Starting service…' >&2
    local CMD="$SCRIPT &> /dev/stderr & echo \$!"
    @@ -28,33 +28,38 @@ start() {
    stop() {
    if [ ! -f /var/run/$PIDNAME ] || ! kill -0 $(cat /var/run/$PIDNAME); then
    echo 'Service not running' >&2
    exit 1
    return 1
    fi
    echo 'Stopping service…' >&2
    kill -15 $(cat /var/run/$PIDNAME) && rm -f /var/run/$PIDNAME
    echo 'Service stopped' >&2
    }

    uninstall() {
    update-rc.d -f <NAME> remove
    rm -f "$0"
    echo -n "Are you really sure you want to uninstall this service? That cannot be undone. [yes|No] "
    local SURE
    read SURE
    if [ "$SURE" = "yes" ]; then
    stop
    update-rc.d -f <NAME> remove
    rm -fv "$0"
    fi
    }

    case "$1" in
    start)
    start
    ;;
    stop)
    stop
    ;;
    uninstall)
    stop
    uninstall
    ;;
    retart)
    stop
    start
    ;;
    *)
    echo "Usage: $0 {start|stop|restart}"
    start)
    start
    ;;
    stop)
    stop
    ;;
    uninstall)
    uninstall
    ;;
    retart)
    stop
    start
    ;;
    *)
    echo "Usage: $0 {start|stop|restart|uninstall}"
    esac
  9. naholyr revised this gist Dec 13, 2012. 2 changed files with 15 additions and 1 deletion.
    5 changes: 5 additions & 0 deletions new-service.sh
    Original file line number Diff line number Diff line change
    @@ -51,5 +51,10 @@ else
    service "$NAME" start
    fi

    echo ""
    echo "---Uninstall instructions ---"
    echo "The service can uninstall itself:"
    echo " service \"$NAME\" uninstall"
    echo "It will simply run update-rc.d -f \"$NAME\" remove && rm -f \"/etc/init.d/$NAME\""
    echo ""
    echo "--- Terminated ---"
    11 changes: 10 additions & 1 deletion service.sh
    Original file line number Diff line number Diff line change
    @@ -11,7 +11,7 @@
    SCRIPT=<COMMAND>
    RUNAS=<USER>

    PIDNAME=$(basename "$0")
    PIDNAME=<NAME>


    start() {
    @@ -35,13 +35,22 @@ stop() {
    echo 'Service stopped' >&2
    }

    uninstall() {
    update-rc.d -f <NAME> remove
    rm -f "$0"
    }

    case "$1" in
    start)
    start
    ;;
    stop)
    stop
    ;;
    uninstall)
    stop
    uninstall
    ;;
    retart)
    stop
    start
  10. naholyr revised this gist Dec 13, 2012. 1 changed file with 5 additions and 3 deletions.
    8 changes: 5 additions & 3 deletions _service.md
    Original file line number Diff line number Diff line change
    @@ -40,13 +40,15 @@ Enjoy
    Yep, I'm lazy too. But still, I've written a script to automate this :)

    ```sh
    curl 'https://raw.github.com/gist/4275302/new-service.sh' | bash
    wget 'https://raw.github.com/gist/4275302/new-service.sh' && bash new-service.sh
    ```

    In this script I will download `service.sh` into a `tempfile`, replace some tokens, and then show you commands you should run as superuser.

    If you feel confident enough with my script, you can `sudo` the script directly:

    ```sh
    curl 'https://raw.github.com/gist/4275302/new-service.sh' | sudo bash
    ```
    wget 'https://raw.github.com/gist/4275302/new-service.sh' && sudo bash new-service.sh
    ```

    Note: the cool hipsterish `curl $URL | bash` won't work here, I don't really want to check why.
  11. naholyr revised this gist Dec 13, 2012. 3 changed files with 69 additions and 4 deletions.
    14 changes: 12 additions & 2 deletions service.md → _service.md
    Original file line number Diff line number Diff line change
    @@ -37,6 +37,16 @@ Enjoy

    ## I'm noob and/or lazy

    Yep, I'm lazy too.
    Yep, I'm lazy too. But still, I've written a script to automate this :)

    I may edit the gist and provide an automated way to install new services.
    ```sh
    curl 'https://raw.github.com/gist/4275302/new-service.sh' | bash
    ```

    In this script I will download `service.sh` into a `tempfile`, replace some tokens, and then show you commands you should run as superuser.

    If you feel confident enough with my script, you can `sudo` the script directly:

    ```sh
    curl 'https://raw.github.com/gist/4275302/new-service.sh' | sudo bash
    ```
    55 changes: 55 additions & 0 deletions new-service.sh
    Original file line number Diff line number Diff line change
    @@ -0,0 +1,55 @@
    #!/bin/bash

    SERVICE_FILE=$(tempfile)

    echo "--- Download template ---"
    wget -q -O "$SERVICE_FILE" 'https://raw.github.com/gist/4275302/service.sh'
    chmod +x "$SERVICE_FILE"
    echo ""

    echo "--- Customize ---"
    echo "I'll now ask you some information to customize script"
    echo "Press Ctrl+C anytime to abort."
    echo "Empty values are not accepted."
    echo ""

    prompt_token() {
    local VAL=""
    while [ "$VAL" = "" ]; do
    echo -n "${2:-$1} : "
    read VAL
    if [ "$VAL" = "" ]; then
    echo "Please provide a value"
    fi
    done
    VAL=$(printf '%q' "$VAL")
    eval $1=$VAL
    sed -i "s/<$1>/$(printf '%q' "$VAL")/g" $SERVICE_FILE
    }

    prompt_token 'NAME' 'Service name'
    prompt_token 'DESCRIPTION' ' Description'
    prompt_token 'COMMAND' ' Command'
    prompt_token 'USER' ' User'
    echo ""

    echo "--- Installation ---"
    if [ ! -w /etc/init.d ]; then
    echo "You don't gave me enough permissions to install service myself."
    echo "That's smart, always be really cautious with third-party shell scripts!"
    echo "You should now type those commands as superuser to install and run your service:"
    echo ""
    echo " mv \"$SERVICE_FILE\" \"/etc/init.d/$NAME\""
    echo " update-rc.d \"$NAME\" defaults"
    echo " service \"$NAME\" start"
    else
    echo "1. mv \"$SERVICE_FILE\" \"/etc/init.d/$NAME\""
    mv -v "$SERVICE_FILE" "/etc/init.d/$NAME"
    echo "2. update-rc.d \"$NAME\" defaults"
    update-rc.d "$NAME" defaults
    echo "3. service \"$NAME\" start"
    service "$NAME" start
    fi

    echo ""
    echo "--- Terminated ---"
    4 changes: 2 additions & 2 deletions service.sh
    Original file line number Diff line number Diff line change
    @@ -8,8 +8,8 @@
    # Description: <DESCRIPTION>
    ### END INIT INFO

    SCRIPT='<COMMAND>'
    RUNAS='<USER>'
    SCRIPT=<COMMAND>
    RUNAS=<USER>

    PIDNAME=$(basename "$0")

  12. naholyr revised this gist Dec 13, 2012. 1 changed file with 2 additions and 2 deletions.
    4 changes: 2 additions & 2 deletions service.sh
    Original file line number Diff line number Diff line change
    @@ -8,8 +8,8 @@
    # Description: <DESCRIPTION>
    ### END INIT INFO

    SCRIPT=<COMMAND>
    RUNAS=<USER>
    SCRIPT='<COMMAND>'
    RUNAS='<USER>'

    PIDNAME=$(basename "$0")

  13. naholyr renamed this gist Dec 13, 2012. 1 changed file with 0 additions and 0 deletions.
    File renamed without changes.
  14. naholyr created this gist Dec 13, 2012.
    42 changes: 42 additions & 0 deletions README.md
    Original file line number Diff line number Diff line change
    @@ -0,0 +1,42 @@
    # Sample service script for debianoids

    Look at [LSB init scripts](http://wiki.debian.org/LSBInitScripts) for more information.

    ## Usage

    Copy to `/etc/init.d`:

    ```sh
    # replace "$YOUR_SERVICE_NAME" with your service's name (whenever it's not enough obvious)
    cp "service.sh" "/etc/init.d/$YOUR_SERVICE_NAME"
    chmod +x /etc/init.d/$YOUR_SERVICE_NAME
    ```

    Edit the script and replace following tokens:

    * `<NAME>` = `$YOUR_SERVICE_NAME`
    * `<DESCRIPTION>` = Describe your service here (be concise)
    * Feel free to modify the LSB header, I've made default choices you may not agree with
    * `<COMMAND>` = Command to start your server (for example `/home/myuser/.dropbox-dist/dropboxd`)
    * `<USER>` = Login of the system user the script should be run as (for example `myuser`)

    Start and test your service:

    ```sh
    service $YOUR_SERVICE_NAME start
    service $YOUR_SERVICE_NAME stop
    ```

    Install service to be run at boot-time:

    ```sh
    update-rc.d $YOUR_SERVICE_NAME defaults
    ```

    Enjoy

    ## I'm noob and/or lazy

    Yep, I'm lazy too.

    I may edit the gist and provide an automated way to install new services.
    51 changes: 51 additions & 0 deletions service.sh
    Original file line number Diff line number Diff line change
    @@ -0,0 +1,51 @@
    #!/bin/sh
    ### BEGIN INIT INFO
    # Provides: <NAME>
    # Required-Start: $local_fs $network $named $time $syslog
    # Required-Stop: $local_fs $network $named $time $syslog
    # Default-Start: 2 3 4 5
    # Default-Stop: 0 1 6
    # Description: <DESCRIPTION>
    ### END INIT INFO

    SCRIPT=<COMMAND>
    RUNAS=<USER>

    PIDNAME=$(basename "$0")


    start() {
    if [ -f /var/run/$PIDNAME ] && kill -0 $(cat /var/run/$PIDNAME); then
    echo 'Service already running' >&2
    exit 1
    fi
    echo 'Starting service…' >&2
    local CMD="$SCRIPT &> /dev/stderr & echo \$!"
    su -c "$CMD" $RUNAS > /var/run/$PIDNAME
    echo 'Service started' >&2
    }

    stop() {
    if [ ! -f /var/run/$PIDNAME ] || ! kill -0 $(cat /var/run/$PIDNAME); then
    echo 'Service not running' >&2
    exit 1
    fi
    echo 'Stopping service…' >&2
    kill -15 $(cat /var/run/$PIDNAME) && rm -f /var/run/$PIDNAME
    echo 'Service stopped' >&2
    }

    case "$1" in
    start)
    start
    ;;
    stop)
    stop
    ;;
    retart)
    stop
    start
    ;;
    *)
    echo "Usage: $0 {start|stop|restart}"
    esac