How to automate command line when specific application is launched? So basically I want to run a few line of code each time a specific application is launched and closed. 

Is there application which could do that ? (Busybox ?) 

I actually have to print it my self in a terminal emulator and it's a bit harsh :/
Using Terminal Emulator and Macrodroid, an automation app

Create a macro in which the Trigger is launch application of your choice and action is Launch Shortcut where you choose Term Shortcut and key in the commands in the arguments . See Firelord's answer here for additional details Home screen icon for terminal program?


To understand how MacroDroid works and required settings,  my answer here
See this to enter multiple commands Run 2 commands in the same line in terminal emulator  ( since you mentioned you have a few lines of code not a single command )