When i take my console i get (as the most trivial thing i can invent to execute in Wine)
ubuntu#ip-172-31-15-113:~$ time wine cmd /C echo hello
hello
real 0m0.024s
user 0m0.020s
sys 0m0.000s
which is about as expected, but when i do same from Node subprocess, it takes 4000-5000ms!
var wine=cp.spawn('wine',['cmd', '/C', 'echo','hello'])
i suspect this is because the environment is somehow different for that child process and my command line. what can be the fix?
Related
system:macos 10.13.6
shell: zsh or bash
node: 10.12.0
In my terminal run : time node -v
real 0m10.197s
user 0m0.006s
sys 0m0.007s
I use time /Users/luxueyan/.nvm/versions/node/v10.12.0/bin/node -v,the result is the same. The real time is 10s.
I want to find just how long my program takes to run from start to finish in order to compare it with a past version.
How would I go about finding the time it takes for both of these versions? I'm running ubuntu 12.04LTS
Use the time command:
time yourprogram
By default it will output something similar to this:
real 0m0.020s
user 0m0.004s
sys 0m0.000s
real means the total time your program runs. user means the time your program spent in user land code and sys is the time your program spent in kernel calls.
Run time myprogram
The time command will display all the details you need to know.
Example:
rh63-build(greg)~>time ls >/dev/null
real 0m0.003s
user 0m0.001s
sys 0m0.002s
Here is more about the time command: http://linux.die.net/man/1/time
Linux comes with the 'time' program.
$time ./myapp
real 0m0.002s
user 0m0.000s
sys 0m0.000s
Well, I'm basically trying to make a bash script runs a node script forever. I made the following bash script:
#!/bin/bash
while true ; do
cd /myscope/
unlink nohup.out
node myscript.js
sleep 6
done & echo $! > pid
I'm expecting that when it runs, it starts up node with the given script, checks if node exits, sleeps for 6 seconds if so and reopen node. Also, I'm expecting it to run in background and writes it's pid (the bash pid) on a file called "pid".
Everything explained above works as expected, apparently, but I'm also expecting that when the pid of the bash script is killed, the node script would stop running, I don't know why that made sense in my mind, but when it comes to practice, it doesn't work. The bash script is killed indeed, but the node script keeps running and that is freaking me out.
I've tested it in the terminal, by not sending the bash script to the background and entering ctrl+c, both scripts gets killed.
I'm obviously miss understanding something on the way the background process works. For god sake, can anybody help me?
There are lots of tools that let you do what you're trying, just two off the top of my head:
https://github.com/nodejitsu/forever - A simple CLI tool for ensuring that a given script runs continuously (i.e. forever)
https://github.com/remy/nodemon - Monitor for any changes in your node.js application and automatically restart the server - perfect for development
Maybe the second it's not what you're looking for, but still worth a look.
If you can't or don't want to use those then the problem is that if you kill the parent process the child one is still there, so, you should kill that too:
pkill -TERM -P $PID
where $PID is the parent PID.
My original problem was to kill a process & its children when timeout. And I found GNU timeout quite a good choice.
However, in this testcase, things become weird:
Assume that we have a test1.sh like this:
#!/bin/sh
# test1.sh
output=`timeout 2 ./run.sh`
echo $output
and run.sh like this:
#!/bin/sh
# run.sh
sleep 8s&
Intuitively we should expect test1.sh exits instantly, since init will take charge of that silly sleep process and run.sh will then exits.
However:
sh-4.2$ time ./test1.sh
real 0m8.022s
user 0m0.013s
sys 0m0.003s
And if I create this test2.sh:
#!/bin/sh
# test2.sh
timeout 2 ./run.sh
sh-4.2$ time ./test2.sh
real 0m0.014s
user 0m0.003s
sys 0m0.007s
So, obviously we hit something wrong during command substitution, but why?
It may be the way you have in shell script --
`timeout 2 ./run.sh`
-- you are using command substitution, so as long as the command has not finished execution, substitution cannot be done, because the output is not there...this may explain the output you are seeing.
Try this to see a similar outcome....
echo "hello `sleep 2 &`"
Another script of interest --
$ cat y.sh
echo "hi"
sleep 2 &
echo "bye"
sleep 2 &
Run using
echo "hello `sh y.sh`"
$time sh y.sh
hi
bye
real 0m0.006s
user 0m0.000s
sys 0m0.004s
$time echo "hello `sh y.sh`"
hello hi
bye
real 0m2.008s
user 0m0.004s
sys 0m0.000s
This page explains more about the relationship between background process and file descriptor. Basicly:
Background (better: forked) processes inherit the file descriptors,
and Running a command in backticks means to collect its stdout until
its stdout is closed
The problem is when I use time in shell I get output like that:
1.350u 0.038s 0:01.45 95.1% 0+0k 0+72io 1pf+0w
And when Im using it in script I get:
real 0m1.253s
user 0m1.143s
sys 0m0.047s
I mean why? And in shell script at the beginning I write:
#!/bin/bash
Bash has a built-in command time, and your system should also have a separate binary at /usr/bin/time:
$ help time
time: time [-p] pipeline
Report time consumed by pipeline's execution.
Execute PIPELINE and print a summary of the real time, user CPU time,
...
$ which time
/usr/bin/time
The two commands produce different outputs:
$ time echo hi
hi
real 0m0.000s
user 0m0.000s
sys 0m0.000s
$ /usr/bin/time echo hi
hi
0.00user 0.00system 0:00.00elapsed 100%CPU (0avgtext+0avgdata 0maxresident)k
0inputs+0outputs (0major+199minor)pagefaults 0swaps