?
Solved

Display time in ubuntu terminal

Posted on 2011-05-11
6
Medium Priority
?
748 Views
Last Modified: 2012-05-11
Hi experts,

How can I display time (and date if possible) in Ubuntu terminal, while a compilation process is going on; ie; for each line, I need to display time.

How can I accomplish this?

I need to do this, since I normally have compilations which will long for hours. So, I need to precisely know at what time the compilation gets over. Else, I have to constantly look at the terminal screen without taking my eyes off the screen!

Any help in this regard will be well appreciated.

Best Regards,
Sree
0
Comment
Question by:Sreejith22
  • 3
  • 2
6 Comments
 
LVL 6

Expert Comment

by:bummerlord
ID: 35737397
You could create your own tool that add date and time to be start of each line it reads and then pipe the output from your build command through it.
There might be available tools that does just this, but it's so easy to make it's not worth googling for even :-)

e.g
$ make 2>&1 | ./add-timestamp

Example add-timestamp.c  --
/* build:  gcc -o add-timestamp add-timestamp.c */
#include <stdio.h>
#include <sys/time.h>
#include <time.h>

int main(void) {

        char buf[4097];
        char stamp[20];

        struct timeval tv;
        struct timezone tz;

        time_t lt;
        struct tm *t;

        while ( fgets(buf, 4096, stdin) != NULL ) {
                buf[4096]=0;
                gettimeofday(&tv,&tz);
                lt = (time_t) tv.tv_sec;
                t = localtime(&lt);
                strftime(stamp, 20, "%F %H:%M:%S", t);
                printf("%s: %s\n", stamp, buf);
        }
return 0;
}

0
 

Author Comment

by:Sreejith22
ID: 35767267
This is the file I use for compilation. I give the following command to compile ./build_Gingerbread.sh  . How should I incorporate the code you gave, in order to view the time stamp . Where(in which directory) should I place the C file? How should I modify the build command?

 
#! /bin/bash
#
# SYNOPSYS:
#
# ./build_Gingerbread.sh
# ./build_Gingerbread.sh amss
# ./build_Gingerbread.sh androidu showcommands -j1 V=1
#

export OPERATOR=GLOBAL

if [ -d "$PWD/AMSS/$OPERATOR" ]; then
	./customize.sh AMSS
else
	if [ -d "$PWD/LINUX/android/$OPERATOR" ]; then
		./customize.sh LINUX
	fi
fi

./miyabuild.sh Gingerbread ${@:-androidu}

if [[ -d "$PWD/LINUX/android/$OPERATOR" || -d "$PWD/AMSS/$OPERATOR" ]]; then
	./customize.sh restore
fi

./release_Gingerbread.sh

echo -e "\n"
read -p "Press ENTER to exit..."

Open in new window



Any help which would lead me to the correct solution will be well appreciated with p;oints for sure.
0
 
LVL 6

Accepted Solution

by:
bummerlord earned 1500 total points
ID: 35788121
You can store the C file anywhere you like.

Store the c file in /var/tmp then, assuming you have gcc installed, issue the following commands (dont actually type the $ nor # prefix, its there to symbolize the command prompt);

$ cd /var/tmp
$ gcc -o add-timestamp add-timestamp.c

You should now have a binary executable in /var/tmp called add-timestamp
For easy access copy this executable to a directory that is included in the PATH variable. E.g /usr/local/bin

$ su -
(enter the root password when prompted)
# cp /var/tmp /usr/local/bin
# exit

Launch your build command as usual, but now also add this " 2>&1 | add-timestamp" at the end of the command.
E.g.
$ ./build_Gingerbread.sh 2>&1 | add-timestamp

The terminal output (both stdout and stderr) from build_Gingerbread.sh and whatever commands/scripts it launches should now be caught by 'add-timestamp' that will prefix each line with the date and time before writing it to the terminal.
0
 [eBook] Windows Nano Server

Download this FREE eBook and learn all you need to get started with Windows Nano Server, including deployment options, remote management
and troubleshooting tips and tricks

 
LVL 6

Expert Comment

by:bummerlord
ID: 35788150
Ops :-) Read me first!
the line that says
# cp /var/tmp /usr/local/bin

in the above comment, should say;

# cp /var/tmp/add-timestamp /usr/local/bin

(not sure if/how to edit comments in this place :-))
0
 
LVL 8

Expert Comment

by:JIEXA
ID: 35813691
For a "good makefile" that uses $(CC), you can try:
  make CC="date ; gcc" -s
0
 

Author Comment

by:Sreejith22
ID: 35928793
Will try this and update.
0

Featured Post

Important Lessons on Recovering from Petya

In their most recent webinar, Skyport Systems explores ways to isolate and protect critical databases to keep the core of your company safe from harm.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

These days software publishers make it possible to move all the possible applications on smartphones, tablets, smartwatches, and more. Although behind every logic and decision is a gross doubt: how do they make it possible? In this blog post, we sha…
If you use the Google Now Launcher, as an aftermarket add on, have a Samsung Note 5 and are worried about power consumption be wary of using the ultra power saving mode.  Here is what happened to me when I made the mistake of trying this out...
This video is in connection to the article "The case of a missing mobile phone (https://www.experts-exchange.com/articles/28474/The-Case-of-a-Missing-Mobile-Phone.html)". It will help one to understand clearly the steps to track a lost android phone.
How to Install VMware Tools in Red Hat Enterprise Linux 6.4 (RHEL 6.4) Step-by-Step Tutorial
Suggested Courses

840 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question