Prepend text to stdout of running job

nginx 2>&1 | awk '{print "[NGINX] " $0}' &
Run a job in the background and prefix it's output with some string. This is particularly useful if you are running inside a docker container in a startup script (sue me, I'll run two jobs in a docker container if I want to) and you can run something like: /usr/sbin/nginx 2>&1 | awk '{print "[NGINX] " $0}' & /opt/jws-3.1/tomcat8/bin/catalina.sh run 2>&1 | awk '{print "[TOMCAT] " $0}' & while true; do ; done it can also be combined with tee to create a file log as well as a stdout log, for example if the script above where a script called "/bin/start-container.sh" then you could run /bin/start-container.sh | tee /var/log/containerlogs
Sample Output
[1] 17232 17233
[NGINX] 2017/04/26 08:13:19 [notice] 17239#17239: using the "epoll" event method                                                                        
[NGINX] 2017/04/26 08:13:19 [notice] 17239#17239: nginx/1.11.10 (nginx-plus-r12-p1)
[NGINX] 2017/04/26 08:13:19 [notice] 17239#17239: built by gcc 4.8.5 20150623 (Red Hat 4.8.5-4) (GCC) 
[NGINX] 2017/04/26 08:13:19 [notice] 17239#17239: OS: Linux 3.10.0-514.16.1.el7.x86_64
[NGINX] 2017/04/26 08:13:19 [notice] 17239#17239: getrlimit(RLIMIT_NOFILE): 4096:4096
[NGINX] 2017/04/26 08:13:19 [notice] 17246#17246: start worker processes
[NGINX] 2017/04/26 08:13:19 [notice] 17246#17246: start worker process 17248
[NGINX] 2017/04/26 08:13:19 [notice] 17246#17246: start worker process 17249
[NGINX] 2017/04/26 08:13:19 [notice] 17246#17246: start worker process 17250
[NGINX] 2017/04/26 08:13:19 [notice] 17246#17246: start worker process 17251
[NGINX] 127.0.0.1 - - [26/Apr/2017:08:13:21 +1000] "GET / HTTP/1.1" 200 612 "-" "curl/7.29.0" "-"
[NGINX] 127.0.0.1 - - [26/Apr/2017:08:13:22 +1000] "GET / HTTP/1.1" 200 612 "-" "curl/7.29.0" "-"
[NGINX] 127.0.0.1 - - [26/Apr/2017:08:13:22 +1000] "GET / HTTP/1.1" 200 612 "-" "curl/7.29.0" "-"

0
By: hvindin
2017-04-25 22:18:38

These Might Interest You

What do you think?

Any thoughts on this command? Does it work on your machine? Can you do the same thing with only 14 characters?

You must be signed in to comment.

What's this?

commandlinefu.com is the place to record those command-line gems that you return to again and again. That way others can gain from your CLI wisdom and you from theirs too. All commands can be commented on, discussed and voted up or down.

Share Your Commands



Stay in the loop…

Follow the Tweets.

Every new command is wrapped in a tweet and posted to Twitter. Following the stream is a great way of staying abreast of the latest commands. For the more discerning, there are Twitter accounts for commands that get a minimum of 3 and 10 votes - that way only the great commands get tweeted.

» http://twitter.com/commandlinefu
» http://twitter.com/commandlinefu3
» http://twitter.com/commandlinefu10

Subscribe to the feeds.

Use your favourite RSS aggregator to stay in touch with the latest commands. There are feeds mirroring the 3 Twitter streams as well as for virtually every other subset (users, tags, functions,…):

Subscribe to the feed for: