Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Write a bash shell script that consumes a constant amount of RAM for a user defined time [closed]

I am trying to write a bash shell script that consumes a high amount of RAM on an embedded device for a user defined time. How do I do it without using arrays ?

like image 604
Ankur Agarwal Avatar asked Feb 11 '11 01:02

Ankur Agarwal


People also ask

What does [- Z $1 mean in bash?

$1 means an input argument and -z means non-defined or empty. You're testing whether an input argument to the script was defined when running the script. Follow this answer to receive notifications.

What does $() mean in shell script?

$() – the command substitution. ${} – the parameter substitution/variable expansion.

What is $? == 0 in shell script?

$? is the exit status of the most recently-executed command; by convention, 0 means success and anything else indicates failure. That line is testing whether the grep command succeeded. The grep manpage states: The exit status is 0 if selected lines are found, and 1 if not found.

What $@ means in bash?

bash [filename] runs the commands saved in a file. $@ refers to all of a shell script's command-line arguments. $1 , $2 , etc., refer to the first command-line argument, the second command-line argument, etc. Place variables in quotes if the values might have spaces in them.


2 Answers

Even if traditional Bash arrays are not supported, it may still be possible to create array-like variables using the eval command built into the particular shell.

The following example script is based on some scripting I did when using BusyBox in an embedded Linux project. BusyBox uses the Almquist shell (also known as A Shell, ash, and sh), which does not support arrays.

#!/bin/ash  for index in 1 2 3 4 5; do     value=$(($index * 1024))     eval array$index=\"array[$index]: $value\" done  for i in 1 3 5; do     eval echo \$array$i done 

Be careful with quoting when using eval!

Output:

array[1]: 1024 array[3]: 3072 array[5]: 5120 

Depending on your particular scenario, a script similar to the following may suffice.

#!/bin/ash  echo "Provide sleep time in the form of NUMBER[SUFFIX]" echo "   SUFFIX may be 's' for seconds (default), 'm' for minutes," echo "   'h' for hours, or 'd' for days." read -p "> " delay  echo "begin allocating memory..." for index in $(seq 1000); do     value=$(seq -w -s '' $index $(($index + 100000)))     eval array$index=$value done echo "...end allocating memory"  echo "sleeping for $delay" sleep $delay 

In my brief testing, this script consumed ~570M to ~575M physical memory* for the specified time period of 5 minutes.

* Monitored using top and memprof programs in separate tests

like image 96
jschmier Avatar answered Sep 23 '22 08:09

jschmier


Personally I would go with Nick's answer, since doing it in C is going to be much easier really.

But... if you really want to avoid writing a super-simple C program to do it, then (if the system is running Linux with the right stuff built in) you should be able to do it by mounting a tmpfs with a size limit of however much memory you want to use, then spewing data into a file in that tmpfs to fill it up (by, e.g., copying data from an infinite source (e.g., /dev/zero).

The C program is really easier though, as long as you can compile for the platform.

like image 30
John Bartholomew Avatar answered Sep 22 '22 08:09

John Bartholomew