Tag Info

Hot answers tagged

14

I suggest to take a look at bash variable SECONDS: SECONDS: Each time this parameter is referenced, the number of seconds since shell invocation is returned. If a value is assigned to SECONDS, the value returned upon subsequent references is the number of seconds since the assignment plus the value assigned. Thus you can simply ...


12

To be able to time a subshell, you need the time keyword, not command. The time keyword, part of the language, is only recognised as such when entered literally. Even entering "time" won't work let alone $TIME (and would be taken as a call to the time command instead). You could use aliases here which are expanded before another round of parsing is ...


9

Prefix your command with /usr/bin/time and the time command will output the time it took the script to run. This is more portable than the using something bash specific.


9

date +%s%N will give the nano seconds since epoch To get the micro seconds just do an eval expr `date +%s%N` / 1000


8

It doesn't work because time is a shell keyword. There are external time binaries, but you don't appear to have one installed. This will likely work: nohup bash -c 'time sleep 2'


8

There's no much point asking for this kind of precision in a shell script, given that running any command (even the date command) will take at least a few hundreds of those microseconds. In particular, you can't really use the date command to time the execution of a command with this kind of precision. For that, best would be to use the time command or ...


6

While using SECONDS and time will give you relative values. If you'd like to have absolute values for Auditing and Reporting Purposes as to when the script ran and when it completed, you might want to try something like this before and after your commands date '+%Y%m%d%H%M%S.%N' . This could also give you a better granularity as it can capture ...


5

In bash: $ type -a time time is a shell keyword time is /usr/bin/time You called time, cause bash invoked time built in keyword instead of external /usr/bin/time command. time built in keyword does not have option -v. bash interpreted that you calling built in time on command -v, causing this error. Try: /usr/bin/time -v ls or: command time -v ls


5

The system clock and the hardware clock are not the same. The command hwclock -r should show you the time the hardware clock is set to. If it is incorrect, use the command hwclock -w to update it when the time is correct. If you dual boot with windows, you will want to use local time. Otherwise, you may want to set the harward clock to UTC with the ...


4

This is kind of hacky and unscriptable, but might get you what you're looking for. Firefox can run javascript via command line like so: firefox "javascript:alert(Date.now())" That will open Firefox and run javascript to pop up a message box containing the number of milliseconds in epoch time. You can get the number of milliseconds elapsed in epoch time ...


4

For this answer, I'll assume that there may be several elements working hard to set your time straight. Since I don't really want to wild-guess about which one is working against you, I'll try and give you an answer which should help you find it yourself instead. On a UNIX system, the clock can typically be set using the stime system call. As things ...


4

As you said date +%s returns the number of seconds since the epoch. So, date +%s%N returns the seconds and the current nanoseconds. Dividing date +%s%N the value by 1000 will give in microseconds.i.e echo $(($(date +%s%N)/1000))


3

The simplest way is to use the --printf option as suggested by @don_crissti: stat --printf='%A %h %U %G %s %.16y %n\n' .bashrc If, for whatever reason, you can't do that you can parse the output of `stat -c '%y': $ stat -c'%A %h %U %G %s %y %n' .bashrc | awk '{$7=substr($7,1,8); $8=""}1;' -rw-r--r-- 1 terdon terdon 9737 2015-02-01 18:12:18 .bashrc Or ...


3

While an alias is one way to do it, this can be done with eval as well - it's just that you don't so much want to eval the command execution as you want to eval the command declaration. I like aliases - I use 'em all the time, but I like functions better - especially their ability to handle parameters and that they needn't necessarily be expanded in command ...


3

Use systemd-analyze built-in tool. You are especially interested in two options: blame and plot systemd-analyze blame systemd-analyze plot > graph.svg blame: Print list of running units ordered by time to init plot: Output SVG graphic showing service initialization


3

With zsh: mutt -s "Log" -a /path/to/*.log(.om[1]) example@example.com That uses zsh glob qualifiers. While other shell globs can only generate filenames based on their name, in zsh, you can use those qualifiers ((.om[1]) above), to select based on file attributes (type, size, times, permissions...) or other criteria of your own, affect the order, apply ...


3

#!/bin/sh EPOCH='jan 1 1970' sum=0 for i in 00:03:34 00:00:35 00:12:34 do sum="$(date -u -d "$EPOCH $i" +%s) + $sum" done echo $sum|bc date -u -d "jan 1 1970" +%s gives 0. So date -u -d "jan 1 1970 00:03:34" +%s gives 214 secs.


3

It's because the time in the first command is a shell keyword. The time in the second command is the executable. See the type output of time: $ type -a time time is a shell keyword time is /usr/bin/time The command unbuffer needs a program as argument not a shell keyword. It cannot interpret a shell keyword, this is a bash internal keyword. And the ...


3

From the section of execute_cmd.c of bash-4.2+dfsg that deals with TIMEFORMAT: if (*s == 'R' || *s == 'E') len = mkfmt (ts, prec, lng, rs, rsf); else if (*s == 'U') len = mkfmt (ts, prec, lng, us, usf); else if (*s == 'S') len = mkfmt (ts, prec, lng, ss, ssf); else { internal_error (_("TIMEFORMAT: `%c': invalid format character"), *s); free ...


3

I don't think you can pause system time. Lots of programs wouldn't like it anyway, because they generally expect time to go forward. If you paused the time, then any program going into sleep would never wake up. I don't understand exactly what you're trying to do, but I suspect that faketime would do what you're after. It lets you run programs with a fake ...


2

Note: Although NTP had this idea of a nanokernel which could be used to patch OS's that don't use NTP, in Linux in particular is not in this case. The NTP code is in the kernel itself as you allude to in question 1. 0: How does this Nanokernel manage to deliver an accuracy less than the system clock tick (such as ns accuracy)? Accuracy greater than ...


2

The time command will print its output to standard error, not standard output. So that's what you need to capture. Then, you need to capture the output of time and not the output of the command you are timing. Typically, this is done by grouping or running the commands in a subshell (in { } or ( ) respecitvely), redirecting the group's output to /dev/null ...


2

If you cannot (or don't want) use TIMEFORMAT your just need to transfer time into seconds, then add it together. For example pipe output through: { sum=0 while IFS="[ :]" proc_name h m s do let sum+=$((60*($m+60*$h)+$s)) done echo $sum } Or if you'd like can exchange last echo-command by printf ...


2

Assuming you are using the bash 'time' builtin command, before you run your program, you can export TIMEFORMAT=%0R. The output will then be in whole seconds addable by awk. More information is available in the 'Shell Variables' section of the bash man page.


2

start a listening netcat in the background call firefox to connect it wait it with a wait bash builtin. finally kill everything nc -l 64738 & firefox http://127.0.0.1:64738 & wait <...yet to be solved that only the nc should be waited for...> killall firefox <..yet to be solved to not kill your girlfriends browser>


2

To me, NTP is the obvious answer. Reliable and consistent - assuming some sort of Internet connection, with an option that's enabled on many distributions to set the clock directly during the boot-up process. However, this is an opinion-based answer, probably not suited to SE.


2

Bash's built-in time returns the exit status of the command. You can test that fairly easily with time false; afterwards, echo $? prints 1 as expected. You can also test something with a different exit code to confirm other codes are preserved: $ time bash -c 'exit 42'; echo "Exit code: $?" real 0m0.002s user 0m0.000s sys 0m0.000s Exit code: 42


2

By definition, a.out is a child process of time. So time is the parent pid of a.out! here's a test where I replace a.out with sleep 60: $ time sleep 50 & timepid=$! $ aoutpid=$(pgrep -P $timepid) $ ps -o ppid,pid,start,cmd w -p $$,$timepid,$aoutpid PPID PID STARTED CMD 2065 2068 21:34:57 -bash 2068 3297 22:16:05 -bash 3297 3298 22:16:05 sleep 50 ...


2

If the time to start a shell is negligible compared to the time it takes to run the command, you can run an intermediate shell, and use the exec builtin to ensure that a.out replaces the shell rather than being executed as a subprocess. time sh -c 'echo $$; exec ./a.out'


2

If you have configured all of the Raspberry Pis to a local NTP Server, i.e. you've set up an NTP Server on your LAN, then their synchronization should be adequate for your video frame timestamping task. Both Bash and Python need to make a system function call to retrieve the system time. There's no advantage in using Bash to make that call. Both Python ...



Only top voted, non community-wiki answers of a minimum length are eligible