Heroku: stop all one-off dynos without dyno name

114 Views Asked by At

I run the following command twice to start my two one-off dynos

heroku run:detached python main.py

and then stop it using

heroku ps:stop <dyno-name>

however, I wish to stop all my dynos without knowing dyno names before hand, since I am trying to automate turning on and off my dynos using crontab.

1

There are 1 best solutions below

0
On BEST ANSWER

I wrote a python script to achieve the same, not that optimised but does the job.

import os

path = 'cd <path-to-project>'

def stop():
    online_ps = os.popen(path + ' && heroku ps').read()
    for ps in online_ps.split():
        if(ps.startswith('run')): # process name example run.1234
            os.system(path + ' && heroku ps:stop ' + ps)

def start():
    os.system(path + ' && heroku run:detached python main.py <arg1>') # dyno 1
    os.system(path + ' && heroku run:detached python main.py <arg2>') # dyno 2

stop()
start()

Note:

  • mind the spaces in the command
  • this code works on the assumption that each process name starts with run like run.1234