Run Automator workflow or shell script at application launch/shutdown
To keep tracking if an app is running you need to use a background loop. I don't know if there is another method but this will work, although it will eat a little bit of your memory. The loop will keep waiting until you open the application, and then it will wait for you to quit.

Save your workflow as an application and set it to run on startup. As your first action run an Applescript like this:



repeat until application "TextEdit" is running -- change for the app you want
    delay 1 -- it will check every one second for the app
end repeat
delay 3 -- you can change this value if the app will take some time to get ready
if application "TextEdit" is running then 
    -- Your code for starting here
end if



you can set other actions after this or just use applescript.

At the end of the workflow place another action running Applescript:



repeat until application "TextEdit" is not running -- change here too
    delay 1 -- it will check every one second if the app is gone
end repeat
delay 3 -- you can change this value if the app will take some time to vanish
if application "TextEdit" is not running then 
    -- Your code for quitting here
end if



Again you can set other actions below.

Let me know if you have specific goals and we can work to improve this workflow.