LinuxQuestions.org

LinuxQuestions.org (/questions/)
-   Programming (https://www.linuxquestions.org/questions/programming-9/)
-   -   run script error (https://www.linuxquestions.org/questions/programming-9/run-script-error-4175568867/)

sub320 01-31-2016 09:43 AM

run script error
 
I have a script as below

Code:

if [[ ... ]] ; then
top
fi

it works fine , when run it , it run "top" command on the foreground.

Now , I wold like to run the command "top" in background , it changed the script to as below , it pops the error "top: failed tty get" , would advise what is wrong , how to fix it . thanks .

Code:

if [[ ... ]] ; then
top &
fi


grail 01-31-2016 10:48 AM

Well the error message appears to be quite clear on the problem, ie. you are trying to run top without a tty attached.
I guess the real question would be why you would want to run it in the background??

I am also not quite sure on the point of '...' as a test, but as it always seems to be true what is the point of the if at all?

pan64 01-31-2016 01:07 PM

you ought to try xterm -e top & (or choose your favorite terminal emulator)

sub320 01-31-2016 08:54 PM

Quote:

Originally Posted by pan64 (Post 5490750)
you ought to try xterm -e top & (or choose your favorite terminal emulator)

thanks reply ,

when run it , it pops the below error , would advise how to fix it ?

Code:

Warning: This program is an suid-root program or is being run by the root user.
The full text of the error or warning message cannot be safely formatted
in this environment. You may get a more descriptive message by running the
program as a non-root user or by removing the suid bit on the executable.
xterm Xt error: Can't open display: s
xterm:  DISPLAY is not set


thanks

pan64 02-01-2016 12:32 AM

so that means there is no X environment available - or you have not set that DISPLAY variable (probably you only need to set it). top needs a terminal to work properly.
From the other hand you need to provide more information, it is not really enough to give any hints

sub320 02-01-2016 12:44 AM

Quote:

Originally Posted by pan64 (Post 5490950)
so that means there is no X environment available - or you have not set that DISPLAY variable (probably you only need to set it). top needs a terminal to work properly.
From the other hand you need to provide more information, it is not really enough to give any hints

thanks reply ,

set that DISPLAY variable <<== would advise how to do it ?

besides , what infomation that I need to provide ? thanks

pan64 02-01-2016 12:50 AM

if you have no idea what is that probably you won't need that.
Do you have graphical environment or just command line?
what do you want to achieve at all?

sub320 02-01-2016 01:25 AM

Quote:

Originally Posted by pan64 (Post 5490954)
if you have no idea what is that probably you won't need that.
Do you have graphical environment or just command line?
what do you want to achieve at all?

we have command line only

just for testing only , we will implement another program later , would you help me for providing suggestion about how to do it , may be I may explain what my project target is .

grail 02-01-2016 01:42 AM

Maybe you could start by explaining why you picked top? Does it have similarities the 'other' program?

pan64 02-01-2016 01:47 AM

Quote:

Originally Posted by sub320 (Post 5490957)
we have command line only

just for testing only, ... would you help me for providing suggestion about how to do it , may be I may explain what my project target is .

Testing what? how should I give any suggestion without knowing any details?

NevemTeve 02-01-2016 01:47 AM

> may be I may explain what my project target is.

Quite an idea. I, for one, am not a clairvoyant.

sub320 02-01-2016 10:10 AM

thanks reply ,

what I just want is use the process of running the command "top" in background to eating up system resource , this is only testing purpose , is there any way to make it work ?


thanks

grail 02-01-2016 12:17 PM

Not sure about the others, but that didn't really clear anything up for me :(

Again, why does it need to be top?

If you want to eat all your system resources (not sure why you would want to on purpose), just create a recursive loop to call itself forever.

sub320 02-01-2016 06:58 PM

Quote:

Originally Posted by grail (Post 5491144)
Not sure about the others, but that didn't really clear anything up for me :(

Again, why does it need to be top?

If you want to eat all your system resources (not sure why you would want to on purpose), just create a recursive loop to call itself forever.

thanks reply ,


"just create a recursive loop to call itself forever" << I am trying to do that but fail , as above the script , it pops error when run it , would advise how to modify it ? thanks

astrogeek 02-01-2016 07:55 PM

Here try this:

Code:

#!/bin/sh
# Eat up resources with "top"
function top {
TOP=100000
        if [[ "$1" -le "$TOP" ]]; then
                top $(($1 + 1)) "$2 $2"
        fi
}
top 1 "Seed string - Make this as long as you want depending on the amount of memory you have.";

I named the function top only to be as obfuscated as the question, so that you can call top recursively and eat up resources. You may rename it if that is too confusing.

I was nice and put a limit so that it will not (necessarily) run until your system melts down, but you will need to adjust it depending on your system resources and how much you want to waste them.

Save this to a script named resource_killer.sh, make it executable (chmod +x resource_killer.sh) then run it in the background with ./resource_killer.sh &.

It will consume CPU and memory resources until there are no more...

Offered as a complete freebie in order to facilitate having this thread marked as SOLVED.


All times are GMT -5. The time now is 08:21 AM.