3

I have a bash script that calls various other scripts, one of which has a bunch of commands that launch scripts inside a screen session like this:

screen -S $SESSION_NAME -p $f -X stuff "$CMD\n"

Will running my top script with /usr/bin/time -v capture the peak memory usage everything? I want to have this script run as a cron job but I need to know how much memory it will take before I cause problems for other users on the machine.

Thanks

  • time will only give information for its children, and by stuffing a command into an existing screen session, that command will not be a child of your script, so will not be observed by time. You might use another time when you start the screen session, but note that it only gives the max RSS of the biggest process. – meuh Oct 10 '16 at 19:04
  • @meuh Not even of the biggest one if it runs in background, see the example in my answer. – rudimeier Oct 10 '16 at 19:17
3

Yes and no, GNU time tries to show the summary/peak of everything.

You may check that using a small C program, say mal.c:

#include <stdlib.h>
#include <string.h>

int main(int argc, char **argv)
{
    long bytes;
    void *buf;

    bytes = atol(argv[1]) * 1024;
    buf = malloc(bytes);
    memset(buf, 0, bytes);
    printf("");
    return 0;
}

And see:

$ gcc mal.c -o mal
$ /usr/bin/time -f "%M" sh -c "./mal 5000"
5452
$ /usr/bin/time -f "%M" sh -c "./mal 10000"
10452
$ /usr/bin/time -f "%M" sh -c "./mal 5000; ./mal 10000"
10452

But as mentioned in the getrusage(2) man page (Linux), this is the max RSS of any single child process, not the instantaneous cumulative RSS of a tree of processes, all simultaneously using memory

In other words it does not summarize parallel or background processes, as you can see:

$ /usr/bin/time -f "%M" sh -c "./mal 1000000 & ./mal 5000"
5452

This also implies that you need to run time inside of your screen session to measure $CMD and not only screen.

FYI the difference between the shell built-in is that the time binary can't directly summarize pipes or functions:

$ time /bin/sleep 1 | /bin/sleep 2
 real 2.00
 user 0.00
 sys 0.00

$ /usr/bin/time -p  /bin/sleep 1 | /bin/sleep 2
 real 1.00
 user 0.00
 sys 0.00

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.