Issue with time command - linux

When I run several background jobs, I need to get a runtime for the set of jobs.
I use the following example successfully:
$ sleep 10 &
$ time wait
real 0m9.74s
user 0m0.00s
sys 0m0.00s
However, if I try to format the output to seconds, I get the following error:
$ time -f %e wait
time: cannot run wait: No such file or directory
0.00
This only seems to happen with shell builtins. Is there a workaround for this?

The problem here is two-fold.
we don't know if you're using the bash (assuming you're using the centos default shell) built-in or the binary time command.
in either case wait is a bash built-in
Ways to deal with this:
/usr/bin/time -f %e bash -c wait
TIMEFORMAT="%pR" time bash -c wait

Related

Regarding cronjob execution time

I am having list of cron job functions.Each function run for certain period of time.Is there any way to find how long each cron function execute,Like time of start executing function and end time?
Expected output:
Cron tab start time: timestamp
cront tab end time: timestamp
Read crontab(5). A cron job is just a shell command or script. That shell could, for example, use logger(1) mixed with date(1) and time(1). See also time(7). See also GNU bash documentation.
A cron job could also be a GUILE script, or a Python one. See execve(2), fork(2) and other syscalls(2) which is called by crond
And cron is open source, so you are allowed to study its source code.... (e.g. of GNU mcron). Or use strace(1) on it to understand its behavior.
Consider reading Advanced Linux Programming and see Linux From Scratch.
For example, I am running thru cron a backup script starting with
#!/bin/bash
cd $HOME
renice -n +2 -p $$
ionice -c Best-effort -n +5 -p $$
tm=$(date +"backup-home+%Y-%b-%d-%Hh%M")
logger --id=$$ -t backup-home start $tm
sync

How to check if a command is hanged in a bash script?

I have a bash script, in which I call another script and sometimes second script hangs. Is there any way to check if it is hung or not. And I can't make any changes is the second script.
#!/bin/bash
calling second script(thata might hang)
if hang then do something
If you already know a threshold time, that after that script is considered hung. you can use timeout.
timeout 30 bash script.sh
command bash script.sh will run until it finish in less that 30 seconds or gets killed by timeout. You can adjust the time according to your environment.
Command Reference:
timeout
Usage: timeout [OPTION] DURATION COMMAND [ARG]...
or: timeout [OPTION]
Start COMMAND, and kill it if still running after DURATION.
Please refer to respective man page for options.

time option doesn't work

I tried to measure execution time and format it with this command:
time -f "%e" ./1 1000 1
-f: command not found
real 0m0.066s
user 0m0.044s
sys 0m0.023s
But such command works:
/usr/bin/time -f "%e" ./1 1000 1
31245 212 443
0.00
I tried to determine where another time is located, but all showes to /usr/bin/time
which time
/usr/bin/time
or
whereis time
time: /usr/bin/time /usr/bin/X11/time /usr/include/time.h /usr/share/man/man7/time.7.gz /usr/share/man/man2/time.2.gz /usr/share/man/man1/time.1.gz
or
type -a time
time is a shell keyword
time is /usr/bin/time
How to define where another time is located?
Users of the bash shell need to use an explicit path in order to run
the external time command and not the shell builtin variant. On system
where time is installed in /usr/bin, the first example would become
/usr/bin/time wc /etc/hosts
OR
Note: some shells (e.g., bash(1)) have a built-in time command that
provides less functionality than the command described here. To
access the real command, you may need to specify its pathname
(something like /usr/bin/time).
http://man7.org/linux/man-pages/man1/time.1.html

how to get time execution of a program in linux terminal separately

I want to get time of execution of a program in my terminal. I know that I should use this command:
time chmod +x ~/example
but the output is this:
real 0m0.088s
user 0m0.057s
sys 0m0.030s
But I want to access each one separately. for example just real. how can I get that?
You can use -f to format the time command:
$ /usr/bin/time -f "\t%E Elapsed Real Time" touch a
0:00.00 Elapsed Real Time
The geek stuff has a very broad documentation on this topic:
12 UNIX / Linux Time Command Output Format Option Examples.
It is also funny that calling it with time alone did not work to me, I have to use the full path.
In bash, you can influence the ouput of time with the TIMEFORMAT variable, by setting
TIMEFORMAT=%R # real
TIMEFORMAT=%U # user
TIMEFORMAT=%S # sys
before calling it. However, your problems probably don't end there -- capturing the output of time is not trivial with bash because it's not a subprocess but a shell builtin. There's an entry in the bash FAQ on the topic. Going from there, I think you ultimately want
TIMEFORMAT=%R myvar=$( { time chmod +x ~/example > /dev/null 2>&1; } 2>&1 )
Then $myvar will be the real running time of the command.
You can do:
(time chmod +x ~/example) |& awk '$1=="real"{print $2}'
0m0.003s

How can I trigger a delayed system shutdown from in a shell script?

On my private network I have a backup server, which runs a bacula backup every night. To save energy I use a cron job to wake the server, but I haven't found out, how to properly shut it down after the backup is done.
By the means of the bacula-director configuration I can call a script during the processing of the last backup job (i.e. the backup of the file catalog). I tried to use this script:
#!/bin/bash
# shutdown server in 10 minutes
#
# ps, 17.11.2013
bash -c "nohup /sbin/shutdown -h 10" &
exit 0
The script shuts down the server - but apparently it returns just during the shutdown,
and as a consequence that last backup job hangs just until the shutdown. How can I make the script to file the shutdown and return immediately?
Update: After an extensive search I came up with a (albeit pretty ugly) solution:
The script run by bacula looks like this:
#!/bin/bash
at -f /root/scripts/shutdown_now.sh now + 10 minutes
And the second script (shutdown_now.sh) looks like this:
#!/bin/bash
shutdown -h now
Actually I found no obvious method to add the required parameters of shutdown in the syntax of the 'at' command. Maybe someone can give me some advice here.
Depending on your backup server’s OS, the implementation of shutdown might behave differently. I have tested the following two solutions on Ubuntu 12.04 and they both worked for me:
As the root user I have created a shell script with the following content and called it in a bash shell:
shutdown -h 10 &
exit 0
The exit code of the script in the shell was correct (tested with echo $?). The shutdown was still in progress (tested with shutdown -c).
This bash function call in a second shell script worked equally well:
my_shutdown() {
shutdown -h 10
}
my_shutdown &
exit 0
No need to create a second BASH script to run the shutdown command. Just replace the following line in your backup script:
bash -c "nohup /sbin/shutdown -h 10" &
with this:
echo "/sbin/poweroff" | /usr/bin/at now + 10 min >/dev/null 2>&1
Feel free to adjust the time interval to suit your preference.
If you can become root: either log in as, or sudo -i this works (tested on ubuntu 14.04):
# shutdown -h 20:00 & //halts the machine at 8pm
No shell script needed. I can then log out, and log back in, and the process is still there. Interestingly, if I tried this with sudo in the command line, then when I log out, the process does go away!
BTW, just to note, that I also use this command to do occasional reboots after everyone has gone home.
# shutdown -r 20:00 & //re-boots the machine at 8pm

Resources