<?xml version="1.0" encoding="utf-8" standalone="yes"?>
<rss version="2.0" xmlns:atom="http://www.w3.org/2005/Atom" xmlns:content="http://purl.org/rss/1.0/modules/content/">
  <channel>
    <title>Cron on Abdullah Diab&#39;s Blog</title>
    <link>https://mpcabd.xyz/tags/cron/</link>
    <description>Recent content in Cron on Abdullah Diab&#39;s Blog</description>
    <generator>Hugo</generator>
    <language>en-us</language>
    <lastBuildDate>Wed, 13 Feb 2013 06:03:59 +0000</lastBuildDate>
    <atom:link href="https://mpcabd.xyz/tags/cron/index.xml" rel="self" type="application/rss+xml" />
    <item>
      <title>Adding a timestamp to command output in linux</title>
      <link>https://mpcabd.xyz/adding-a-timestamp-to-command-output-in-linux/</link>
      <pubDate>Wed, 13 Feb 2013 06:03:59 +0000</pubDate>
      <guid>https://mpcabd.xyz/adding-a-timestamp-to-command-output-in-linux/</guid>
      <description>&lt;p&gt;This is just a quick solution to put a timestamp with each line of the output of some command in Linux (*nix). It’s a very simple thing, thought I’d write it down here so maybe it’ll help somebody some day.&lt;/p&gt;
&lt;p&gt;I have a cron job running on my server, it’s a high frequency job so I don’t want to send email reports each time, I’m logging it into a file, but I want to log the timestamp of each time it runs and logs something to this file, I didn’t want to change the code behind it to also print the timestamp with each write to the output, so I pipelined the command to a simple bash script that will append the timestamp to each line in the output then write the result to the stdout again.&lt;/p&gt;</description>
      <content:encoded><![CDATA[<p>This is just a quick solution to put a timestamp with each line of the output of some command in Linux (*nix). It’s a very simple thing, thought I’d write it down here so maybe it’ll help somebody some day.</p>
<p>I have a cron job running on my server, it’s a high frequency job so I don’t want to send email reports each time, I’m logging it into a file, but I want to log the timestamp of each time it runs and logs something to this file, I didn’t want to change the code behind it to also print the timestamp with each write to the output, so I pipelined the command to a simple bash script that will append the timestamp to each line in the output then write the result to the stdout again.</p>
<p>Here is the bash script:</p>
<pre class="lang:bash decode:true">#!/bin/bash
while read x; do
	echo -n `date +%d/%m/%Y\ %H:%M:%S`;
	echo -n " ";
	echo $x;
done
</pre>
<p>Then I edited the cron job line to be like this:</p>
<pre class="lang:bash decode:true">... /path/to/my/command.sh 2&gt;&1 | /path/to/timestap.sh &gt;&gt; /var/log/cron/my_log.log
</pre>
<p>And you’re done!</p>
<p>Enjoy it.</p>]]></content:encoded>
    </item>
  </channel>
</rss>
