Awk|Bash – script to loading of processor cores.
Home page Ubuntu C++ Hardware HTML i javascript Java Software SQL

Awk|Bash – script to loading of processor cores.

Added: 2012-09-28

After changing the CPU cooling, worth checking out temperature and noise in pc. You can do it run cpu stress.

Below is script, which doing cpu stress. It performs operations on numbers in an infinite loops. Accepts as a parameter the number of cores in cpu:

./cpustress.bash 4

If you want to stop cpu stress test, type as parameter -9. Script working after logout user.

./cpustress.bash -9

#!/bin/bash

if [ "$1" == "" -o "$1" == "--help" ]; then
	echo "";
	echo "The script is designed to hard loading the CPU cores.";
	echo "Necessary start parameter, is the number of cores in the processor.";
	echo "In some cases, you type a value multiplied by two.";
	echo "To kill process stress type as argumnet '-9'";
	echo "";

	exit 0;
fi

if [ "$1" == "-9" ]; then
	ps -aux | grep $0 | awk -vnf=$0 '{
		print $0;
		if ($10 != "0:00"){
			ki="pkill -f "nf;
			print ki;
			system(ki);
		}
	}'
	exit 0;
fi

function isNumber(){
	echo $1 | grep '^[0-9]*$' > NULL
	if [ "$?" -eq "0" ]; then
		echo "1"
	else 
		echo "0"
	fi
}

if [ "`isNumber $1`" = "0"  ]; then
	echo "The parameter must be a number!!!";
	exit 106;
fi

i=0;
temp=0;
while [ $i -ne $1 ]; do
	let temp=i%4;	
	echo "Core of CPU: "$i+1;
	if [ $temp -eq 0 ]; then 
		nohup `a=1; while [ true ]; do let a+=1; if [ $a -eq 1000000000 ]; then a=1; fi; done;` &
	fi
 
	if [ $temp -eq 1 ]; then
		 nohup `a=1000000000; while [ true ]; do let a/=3; if [ $a -le 1 ]; then a=1000000000; fi; done;` &
	fi;

        if [ $temp -eq 2 ]; then
                nohup `a=1; while [ true ]; do let a*=3; if [ $a -ge 1000000000 ]; then a=1; fi;  done;` &
        fi;

	let i+=1;
done;