Skip to content

Instantly share code, notes, and snippets.

@dmarmugi
Created August 23, 2017 20:15
Show Gist options
  • Save dmarmugi/f70231ec8b4f85d150d6a285533a702a to your computer and use it in GitHub Desktop.
Save dmarmugi/f70231ec8b4f85d150d6a285533a702a to your computer and use it in GitHub Desktop.
Stop all Cloud Foundry apps in a space
cf apps | grep started | awk '{print $1}{system("cf sp " $1)}'
@dmarmugi
Copy link
Author

credit to Vijay for the awk-fu

@saedalavinia
Copy link

Convenient :)

To delete all Cloud Foundry apps, then
cf apps | grep started | awk '{print $1}{system("cf sp " $1)}'
cf apps | grep stopped | awk '{print $1}{system("cf d -f -r " $1)}'

Thanks a lot

@ivladucu-orange
Copy link

Awesome-sauce! I'm using it in GitLab CI/CD jobs to stop all apps at night and start them up again in the morning.

Using env variables so that no login credentials are exposed in the jobs directly and also to determine the actions to be taken.

  script:
  - cf login -a $CF_API_URL -u $CF_USER -o $CF_ORG -p $CF_PASS -s $CF_SPACE
  - cf apps | grep $CF_APP_STATE | awk {'print $1}{system ("cf $CF_APP_ACTION " $1)}'

All these variables are defined in the GitLab Pipeline Schedule. I have 2 schedules, one for stopping the apps and one for starting the apps.

  1. Stopping:
    CF_APP_STATE=started
    CF_APP_ACTION=stop

  2. Starting:
    CF_APP_STATE=stopped
    CF_APP_ACTION=start

Thanks for the gist!

@harishbab
Copy link

Im looking for info on how to start only the stopped apps?
In this script, I see it works only to stop apps. For starting apps it doesnt work as there may be few apps which we dont want to start them. These are the apps which were in stopped state before we stopping the started apps in the space(during night).

Thanks for any help.

@extrajulius
Copy link

Hi, @harishbab, you can see my comment and replace the variables with the ones from point 2 so that you start the stopped apps.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment