Skip to content

Instantly share code, notes, and snippets.

@ralph
Forked from mattheworiordan/restart.sh
Last active July 17, 2020 08:42
Show Gist options
  • Select an option

  • Save ralph/b1b4023e0c03aa8c68f8219cd089de46 to your computer and use it in GitHub Desktop.

Select an option

Save ralph/b1b4023e0c03aa8c68f8219cd089de46 to your computer and use it in GitHub Desktop.

Revisions

  1. ralph revised this gist Jul 17, 2020. 2 changed files with 20 additions and 19 deletions.
    20 changes: 20 additions & 0 deletions heroku_restart.sh
    Original file line number Diff line number Diff line change
    @@ -0,0 +1,20 @@
    #!/bin/bash

    # Set up the Heroku scheduler to run this command every hour.

    # Source: https://gist.github.com/ralph/b1b4023e0c03aa8c68f8219cd089de46

    utc_hour=`date -u +'%H' | sed 's/^0*//'`
    modulo="${RESTART_EVERY_X_HOURS:-6}"
    n=$(($utc_hour % $modulo))
    echo $n

    echo "Current UTC hour is ${utc_hour}, restarting every ${modulo} hours."

    if [[ "$n" -eq 0 ]]; then
    echo "Restarting..."
    curl -i -n -X DELETE https://api.heroku.com/apps/${HEROKU_APP_NAME}/dynos \
    -H "Authorization: Bearer ${HEROKU_TOKEN}" \
    -H "Content-Type: application/json" \
    -H "Accept: application/vnd.heroku+json; version=3"
    fi
    19 changes: 0 additions & 19 deletions restart.sh
    Original file line number Diff line number Diff line change
    @@ -1,19 +0,0 @@
    #!/bin/sh

    # Set up the Heroku scheduler to run this command every hour. See example setup at https://goo.gl/nMCSH3
    #
    # Requires env vars to be set in Heroku with `heroku config:set`:
    # - HEROKU_APP_NAME: this is just the app name in Heroku, i.e. `heroku apps` will list all apps you have access to
    # - HEROKU_CLI_USER: Once Heroku CLI is authenticated (https://goo.gl/Qypr4x), check `cat .netrc` (or `_netrc` on Windows),
    # look for `login` under `machine api.heroku.com`
    # - HEROKU_CLI_TOKEN: As above, but use the `password` field
    #
    # It helps if this file has execute privileges `chmod +x restart.sh`
    #
    # Test this script works by running `heroku run "~/restart.sh"`
    #
    # Heroku API: Restart all Dynos, see https://devcenter.heroku.com/articles/platform-api-reference#dyno-restart-all
    curl -X DELETE "https://api.heroku.com/apps/${HEROKU_APP_NAME}/dynos" \
    --user "${HEROKU_CLI_USER}:${HEROKU_CLI_TOKEN}" \
    -H "Content-Type: application/json" \
    -H "Accept: application/vnd.heroku+json; version=3"
  2. @mattheworiordan mattheworiordan created this gist Aug 19, 2017.
    19 changes: 19 additions & 0 deletions restart.sh
    Original file line number Diff line number Diff line change
    @@ -0,0 +1,19 @@
    #!/bin/sh

    # Set up the Heroku scheduler to run this command every hour. See example setup at https://goo.gl/nMCSH3
    #
    # Requires env vars to be set in Heroku with `heroku config:set`:
    # - HEROKU_APP_NAME: this is just the app name in Heroku, i.e. `heroku apps` will list all apps you have access to
    # - HEROKU_CLI_USER: Once Heroku CLI is authenticated (https://goo.gl/Qypr4x), check `cat .netrc` (or `_netrc` on Windows),
    # look for `login` under `machine api.heroku.com`
    # - HEROKU_CLI_TOKEN: As above, but use the `password` field
    #
    # It helps if this file has execute privileges `chmod +x restart.sh`
    #
    # Test this script works by running `heroku run "~/restart.sh"`
    #
    # Heroku API: Restart all Dynos, see https://devcenter.heroku.com/articles/platform-api-reference#dyno-restart-all
    curl -X DELETE "https://api.heroku.com/apps/${HEROKU_APP_NAME}/dynos" \
    --user "${HEROKU_CLI_USER}:${HEROKU_CLI_TOKEN}" \
    -H "Content-Type: application/json" \
    -H "Accept: application/vnd.heroku+json; version=3"