• Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 774
  • Last Modified:

Display time in ubuntu terminal

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
Sreejith22
Asked:
Sreejith22
  • 3
  • 2
1 Solution
 
bummerlordCommented:
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
 
Sreejith22Author Commented:
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
 
bummerlordCommented:
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
Introducing Cloud Class® training courses

Tech changes fast. You can learn faster. That’s why we’re bringing professional training courses to Experts Exchange. With a subscription, you can access all the Cloud Class® courses to expand your education, prep for certifications, and get top-notch instructions.

 
bummerlordCommented:
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
 
JIEXACommented:
For a "good makefile" that uses $(CC), you can try:
  make CC="date ; gcc" -s
0
 
Sreejith22Author Commented:
Will try this and update.
0
Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

Join & Write a Comment

Featured Post

Free Tool: Subnet Calculator

The subnet calculator helps you design networks by taking an IP address and network mask and returning information such as network, broadcast address, and host range.

One of a set of tools we're offering as a way of saying thank you for being a part of the community.

  • 3
  • 2
Tackle projects and never again get stuck behind a technical roadblock.
Join Now