Code that has to end no matter what

Since this is maxxing out my CPU, is there anything I can add that will make sure stress always ends?

echo "CPU Stress Test" >> CPU_Stress_Test.txt
echo "stress --cpu 8 --io 4 --vm 2 --vm-bytes 128M --timeout 1m" >> CPU_Stress_Test.txt
echo >> CPU_Stress_Test.txt
date +"%Y-%m-%d-%H-%M" >> CPU_Stress_Test.txt
echo >> CPU_Stress_Test.txt
sensors -f | grep "temp4" >> CPU_Stress_Test.txt
# Send fan speed as well
sensors -f | grep "fan1" >> CPU_Stress_Test.txt
echo >> CPU_Stress_Test.txt
#
stress --cpu 8 --io 4 --vm 2 --vm-bytes 128M --timeout 2m

I found this.

#!/bin/bash 
function finish {
  # Your cleanup code here
}
trap finish EXIT 


        You place any code that you want to be certain to run in this     "finish" function.  A good common example: creating a temporary     scratch directory, then deleting it after.
   #!/bin/bash
scratch=$(mktemp -d -t tmp.XXXXXXXXXX)
function finish {
  rm -rf "$scratch"
}
trap finish EXIT 

What is your problem exactly? Does the option "--timeout 2m" not work? If that is the case, the manual says:

-t, --timeout N
    timeout after N seconds 

So try "--timeout 120" . And if you still do not trust it you might consider to use the "timeout" command:

$ timeout --help
Usage: timeout [OPTION] DURATION COMMAND [ARG]...
  or:  timeout [OPTION]
Start COMMAND, and kill it if still running after DURATION.
1 Like

Hello,

I'm not 100% clear on what you might mean here, but I see two possibilities. A script will normally exit either when the last command it contains is executed, or when an exit statement is explicitly executed. So you don't really have to worry about "making sure it always ends" - by its very nature, a script will end so long as it contains no loop or logic that would cause it to keep running.

However, if what you're meaning here is that you want a command in your script to always exit after a certain amount of time to prevent it from running indefinitely, then you could try something like this:

#!/bin/bash

command="/usr/bin/yes"
maxruntime=10

"$command" >/dev/null 2>/dev/null &
watchpid=$!

/usr/bin/sleep "$maxruntime"

if [ -d "/proc/$watchpid" ]
then
        echo "Max runtime exceeded, killing PID $watchpid"

        if /bin/kill -9 "$watchpid" >/dev/null 2>/dev/null
        then
                echo "Killed."
                exit 0
        else
                echo "Could not kill, please investigate manually."
                exit 1
        fi
fi

Here you define command as the full command you want to run, and maxruntime as the maximum number of seconds it can be permitted to run for. The script then executes the command specified in the variable command in the background, with all output re-directed to /dev/null . It then waits for maxruntime seconds, and if the process still exists, it will attempt to kill it and show you the result.

Note that in its current form the script will always wait for maxruntime seconds no matter what, so even if your command has exited before then the script will wait at least that long. You could amend that easily enough though, but this is just to give you an idea of how this might work.

Hope this helps - if not, or if I've not quite grasped what you're actually struggling with here, then if you could provide a bit more info I'd be happy to help further.

1 Like

Yes, I wanted to make sure the stress command does end.

If it didn't, it could burn up my CPU.

Thanks, I will study your code.

------ Post updated at 06:49 AM ------

stress is never executed.?

command="stress --cpu 8 --io 4 --vm 2 --vm-bytes 128M --timeout 1m"
maxruntime=60

"$command" >/dev/null 2>/dev/null &
watchpid=$!

/bin/sleep "$maxruntime"

if [ -d "/proc/$watchpid" ]
then
        echo "Max runtime exceeded, killing PID $watchpid"

        if /bin/kill -9 "$watchpid" >/dev/null 2>/dev/null
        then
                echo "Killed."
                exit 0
        else
                echo "Could not kill, please investigate manually."
                exit 1
        fi
fi

------ Post updated at 06:59 AM ------

Yes, the command does work. I want to make sure nothing occurs whereby the stress commands does not end.

Hi,

OK, two things to try here:

  1. Use the full path in your command - so /usr/bin/stress (assuming that's where it's installed on your system, naturally) rather than just stress
  2. Remove the double-quotes from around the "$command" statement, so that it simply reads $command

Hope this helps.

1 Like

Is there a way so I don't have to manually change maxruntime?

So that --timeout "time" = maxruntime

command="/usr/bin/stress --cpu 8 --io 4 --vm 2 --vm-bytes 128M --timeout 15s"
maxruntime=20s

Hi,

One modification you could make here would be to change the line defining maxruntime to this instead:

maxruntime=$1

The variable $1 has a special meaning referring to the first command-line parameter passed into the script. So for a ten-second timeout, you could then run the script by typing script.sh 10 , and when the script ran maxruntime would take the definition of the first parameter passed to the script - 10, in this case.

Do NOT run this unless you are prepared to reboot. :frowning:

When I ran the code below....

test.sh 5
Max runtime exceeded, killing PID 2749
/home/andy/bin/test.sh: line 30: 2749 Killed $command > /dev/null 2> /dev/null
Killed.

command="/usr/bin/stress --cpu 8 --io 4 --vm 2 --vm-bytes 128M"

maxruntime=$1

$command >/dev/null 2>/dev/null &
watchpid=$!

/bin/sleep "$maxruntime"

if [ -d "/proc/$watchpid" ]
then
        echo "Max runtime exceeded, killing PID $watchpid"

        if /bin/kill -9 "$watchpid" >/dev/null 2>/dev/null
        then
                echo "Killed."
                exit 0
        else
                echo "Could not kill, please investigate manually."
                exit 1
        fi
fi

If you are gonna slice haberno peppers, either use gloves or wash hands 3 times. I forgot and rubbed my eyes.

Hi,

I'm not entirely clear on what could have caused a reboot here - from the output you've provided it seems that after five seconds the script proceeded to kill the previously-backgrounded command. If you can give us a bit more info on what went wrong, what necessitated a reboot, and how this came about then I'd be happy to offer further suggestions as to how to proceed.

It did not cause a reboot, but the stress test never ended resulting in an unchecked rise in my cpu temp.

I made some corrections.

#!/bin/bash
# Ubuntu_Mate 18.04 LTS
#-----------------------------------
# Much help from https://www.unix.com/shell-programming-and-scripting/
# https://ubuntuforums.org/ https://askubuntu.com/ https://ubuntu-mate.community                                 
#                                 
# don't use ALLCAPS for variable names
#-----------------------------------

LogFile="/home/andy/bin/CPU_Stress_Test.txt"
maxruntime=15s
command=(/usr/bin/stress --cpu 8 --io 4 --vm 2 --vm-bytes 128M --timeout "$maxruntime")
# remove logfile if it exists
if [ -f "$Scripts" ];
then
rm $Scripts
fi

"${command[@]}" & watchpid=$!

date +"%Y-%m-%d-%H:%M:%S" >> $LogFile
sensors -f | grep "temp4" >> $LogFile
sensors -f | grep "fan1" >>  $LogFile

/bin/sleep "$maxruntime"

if [ -d "/proc/$watchpid" ]
then
        echo "Max runtime exceeded, killing PID $watchpid"

        if /bin/kill -9 "$watchpid" >/dev/null 2>/dev/null
        then
                echo "Killed."
                exit 0
        else
                echo "Could not kill, please investigate manually."
                exit 1
        fi
fi
date +"%Y-%m-%d-%H:%M:%S" >> $LogFile
sensors -f | grep "temp4" >> $LogFile
sensors -f | grep "fan1" >>  $LogFile