get execution time only from the command gtime - macos

I need to execute a command and get the execution time of it. I am using gtime but the output is a bit different from what I want.
gtime returns the result of the execution and then the execution time. I need to store the output of the command(which I need it to be the time only) in a variable and use it afterward.
Is there a way to change the command to get the execution time only?
So if I write the command below:
executiontime=$(gtime -f "%U" /Users/Desktop/SemanticLocality/optimathsat-1.6.2-macos-64-bit/bin/optimathsat < file.smt2)
echo "$executiontime"
then this is an example of the output I get:
sat
(objectives
(misses_80 1)
)
9.94

To get executable time only from the output you can modify your command on such way:
executiontime=$(gtime -f "%U" /Users/ouafaelachhab/Desktop/SemanticLocality/optimathsat-1.6.2-macos-64-bit/bin/optimathsat < file.smt2|tail -1)

Related

how to wait until a program finishes to pipe the output

I'm trying to pass a program output as input to another program, but I don't know how to make it wait until the first one is finished.
npm run test | ./script.js
I'm trying to run that, but when I run it, tests are starting and the script is throwing an error immediately
the pipe gets the standard output directly to standard input, you can use an subshell to execute first then get the standard output into the standard input.
./script.js <<< "$(npm run test)"
other way to do this is to store the output into an temp_file then use this file as input of the script
nmp run test >> temp_file ; ./script.js < temp_file

How to use tee or > within "screen" while also supplying the name of the screen to them?

I am trying to start a number of jobs in different screens from a shell script. Each job will read in a different value of a parameter from a premade input file and run a simulation based on that value, then tee or > the output to a differently named file. So in a do loop around all the jobs, job 40 on screen "session40" will read in line 40 of the input file, run the simulation, and output to output40.dat, for example. (I am basically trying to run a few jobs in parallel in a very elementary way; it appears my computer has plenty of RAM for this).
I am encountering the issue that the > and | tee commands do not seem to work when I use "exec" to run a command on the remote screen, despite having attempted to start a bash shell there; when I use these commands, it just prints to standard output. Although these commands do work with the command "stuff," I do not know how to pass the job number to stuff, as it appears to only work with string inputs.
The current attempted script is as follows. I have replaced the simulation script with echo and > for a simpler example of the problem. Neither of the last two screen lines work.
for i in 1:10; do
screen -Sdm session$i bash
screen -S session$i -X exec echo $i > runnumber$i.output (method 1)
screen -S session$i -X stuff $'echo $i > runnumber$i.output\r' (method 2)
done
Might there be an easy fix?

Single result of Virt-Top

Is it possible to get one single result of the current stats from virt-top?
I´ve tried to use the --stream parameter but with that I get a new result every second.
I only need one result every execute of the command.
How can I reach that?
From the virt-top man page:
-b
Batch mode. In this mode keypresses are ignored.
-n iterations
Set the number of iterations to run. The default is to run continuously.
So I think what you want is:
virt-top -b -n 1
This is exactly the same as how you would achieve the same with normal "top".

shell : how to start taking inputs from a file some time (say two minutes) after command execution starts

I have to run a script, which takes app. 1 minute for initialization, and then asks inputs from the user.
If I do like
bash-3.2$ java -d64 -jar myJarFile.jar < input_file
It takes input instantly and when myJarFile.jar actually needs inputs, it gets nothing.
So, how I can I tackle this?
Use expect script.
Example:
#!/usr/bin/expect
set timeout -1
spawn ./run.sh
expect "\[Next]> " {send "Next\r"}
interact
In this example, it runs run.sh and asynchronously wait for this string on terminal "[Next]>". whenever it gets this message on terminal, it sends Next as input to the script.
According to your prompt you're using BASH, so you should be able to use process substitution that includes a sleep. Example:
$ seq 1 20 >numbers
$ cat < <(sleep 5; cat numbers)
# 5 second pause and then the contents of the 'numbers' file is output
So with your command you would run:
java -d64 -jar myJarFile.jar < <(sleep 120; cat input_file)

How to make a program in a pipe not to be executed until the first data arrive without tempfile?

Specific case:
generate_data | curl -T - http://someserver/path
That means, I have a program that generate an output in the stdout, and I put that on a remote server with curl.
The problem is that if generate_data takes too much time, the server is going to return 408.
I know that pipes execute all the commands without waiting for data to be ready, so my next iteration was:
generate_data | ( sleep 20 ; curl -T - http://someserver/path )
The time was twice the max time needed to run generate_data, so all the things are ok. But... it is not a optimal solution.
I know I can create something more complex with read, and a proper shell script, but have the feeling that I an missing something obvious.
So... What can i use instead of the sleep 20; without creating a temp file?
generate_data > /tmp/generated_data; cat /tmp/generated_data | curl -T - http://someserver/path

Resources