I’m writing a very simple script to control a process,
script checks if the process is running, if not: executes it.
I need to expand this with the ability to kill a specific process instance (very one one script has started) after a specified amount of time.
Problem is, once bash script is executed it isn’t running until the executed program finishes.Ability to pass some input to the process would also be useful, as I have a graceful exit function built in to the service I’m running.
Basically, my bash script should run along the process it’s started, and send a kill command via stdin after sleep x times out.
How do I forward some scripted input to the process script has started, and how do I keep the script running after executing the process?
Thanks in advance.
If you want to run the process alongside the script, you need to run it in the background:
Now
pidwill contain the PID of the process you just started. Next you need a way to communicate with the process. If you just need to send some input to the process, use a “here document”:That gets more tricky if you still need to run the process in the background. Either write the input to a temporary file (see
mktemp(1)):or you can use named pipes (FIFOs). This article should get you started.