herokucommand-linedyno

Heroku: stop all one-off dynos without dyno name


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.


Solution

  • 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: