2

I'm scripting a deploy to cloud foundry using the CF CLI, and would like to capture the logs related to the new application being started up for analysis. This requires two different commands typically, cf push [appname] -f [manifest.yml] and cf logs [appname] > [appname].log. The problem is that until mid-way though the push command, the application will not exist yet, and cf logs requires an existing application in order to run successfully.

I think my script would have read the output stream from cf push to look for the line stating that the service has been created, then fire off a cf logs command in a second thread to capture those logs. I'm looking for a mechanism by which I could inspect the output of cf push and trigger that second process.

Two questions

  • Is this even possible?
  • What would this kind of thing even be called so I can find some documentation on a sound approach?
  • do you mean something like: cf push ...etc.. | grep "service has been created" && cf logs ... etc .. ? – Jeff Schaller Apr 19 '16 at 15:25
  • I actually figured out a way to handle it by issuing the --no-start argument to cf push, which prevents the application from starting after it's deployed, this gives me a chance to fire off the cf logs process in the background before calling cf start to capture the whole startup process, but I would be interested in the answer to the original question. – tom.dietrich Apr 19 '16 at 16:10
  • does the 'cf push...' command return immediately (before the app starts/exists), or does it exit only after the app has started? – Jeff Schaller Apr 19 '16 at 16:21
  • Only after the app has started. – tom.dietrich Apr 19 '16 at 18:22

1 Answers1

1

Given that the cf push sequence completes only after it has started the service (or died trying, I suppose), you can chain together a pipeline that says (in English): "start the cf push and look for a 'success' message; if see it, run cf logs".

In shell syntax, that would be (with your example commands):

cf push [appname] -f [manifest.yml] | grep -q "service has been created" && \
 cf logs [appname] > [appname].log

This uses the shell's pipeline to connect cf push's output with the grep command. The grep command (-q for quietly) searches for the success message (replace the string I used with what cf push actually produces). If grep finds the message, it will exit with a successful return code; that will allow the && operator to run the cf logs command. If grep is not successful, the && operator will not run the cf logs command. I used a \ backslash after the && just to wrap the code nicely; the commands could all be on one line.

For more reading, see What are the shell's control and redirection operators?

Jeff Schaller
  • 67,283
  • 35
  • 116
  • 255
  • Would there be a way to do this without suppressing the output of cf push on stdout? – tom.dietrich Apr 19 '16 at 19:11
  • 1
    Someone asked for the same thing earlier today! See http://unix.stackexchange.com/a/277580/117549 and/or http://unix.stackexchange.com/questions/277467/log-script-output-to-file-and-terminal-at-the-same-time/277469#277469 for examples of using tee to do that. – Jeff Schaller Apr 19 '16 at 19:21