LinuxQuestions.org

LinuxQuestions.org (/questions/)
-   Programming (https://www.linuxquestions.org/questions/programming-9/)
-   -   bash: countdown (https://www.linuxquestions.org/questions/programming-9/bash-countdown-4175435577/)

fatalerror0x00 11-04-2012 10:57 AM

bash: countdown
 
So I see some post on this when doing a simple google search but none seem to fit what i want and almost feel my idea is almost just as good and I like getting your opinion. So before I start getting into details about what I need I think I should tell you a little about my script. I'm writing a script that will send a countdown to a screen and right now I pretty much have that all done already :) but the problem this is a set time. I also just added an argument check for the text now to just shut down right then and there no countdown. So i think now we are ready for what I need.

What I need is (I believe) a countdown of some sort so I can make it so you can enter any time and when the countdown timer reaches that many seconds it displays a message to my screen how many seconds are left before the server goes down.

also while we are here lets also find out how to keep sleep commands from interupting my terminal if we can please? thank you :)

Snark1994 11-04-2012 12:39 PM

Can we see what you've got already? You should just need to use the 'sleep' command.

Quote:

also while we are here lets also find out how to keep sleep commands from interupting my terminal if we can please? thank you
You mean tasks which you've sent to the background? You can redirect their I/O:

Code:

command >/dev/null 2>&1 &
should work, I believe.

fatalerror0x00 11-04-2012 01:01 PM

This process is running foreground if possible to do it like that and just make sleep commands not interupt me but still do everything else as normal I just want to be able to continue my work while I'm waiting for the server to shutdown if I put sleep in the background it seems to do everything instantly? (thats when I put only sleep in the background of code).

sleep command will not work for my situation if I've figured this out correctly cause it could be any given time I put in and I need on elike 60 seconds or 30 seconds there to be a message how would i tell it to sleep for sure everytime will theres a minute left?get what I mean? and well i guess I can show you what I've got at least but it really doesn't tell you much. but here goes nothing! :) gonna shorten the example up of unneccsary stuff because I am using ssh almost all the time because I'm on my laptop always either away from home and just home and the chair the computer desk has is broken (very uncomfortable) also if there are any mistakes in the code below i promise you they are not actually in the code itself since I can't figure out how to copy and paste the ssh terminal vim i can't just do a simple copy paste to my windows -.-

Code:

#!/bin/bash

screen -S PhantasyCraft -X stuff "say Server will restart in 30 seconds!^M"
sleep 15
. . .
#more of the same thing over and over again just different stuff and different sleep times
screen -S PhantasyCraft -X stuff "say Server will restart in 1 second!^M"
sleep 1
screen -S PhantasyCraft -X stuff "say Server will restart NOW!^M"
screen -S PhantasyCraft -X stuff "stop^M"
sleep 10
startphantasycraft
#the above startphantasycraft is a a symlink to a script i wrote that you don't need anything from it has no real effect on our results

Maybe it will help more then i think but like I say I wanna be able to type in like restartphantasycraft 89 and have it say restarting in 89 seconds and then the next mesage always be at 1 minute maybe and then next 30 seconds so I mean i need a count down of some sort to tell it when I hate 60 cause if that variable keeps changing i can't have a set sleep time and like if it keeps up to 300 seconds i want it to say like 300seconds til restart it needs to work interchangably

Snark1994 11-05-2012 03:59 AM

Quote:

Originally Posted by fatalerror0x00 (Post 4822067)
This process is running foreground if possible to do it like that and just make sleep commands not interupt me but still do everything else as normal I just want to be able to continue my work while I'm waiting for the server to shutdown if I put sleep in the background it seems to do everything instantly? (thats when I put only sleep in the background of code).

Here:
Code:

echo "Shut down in 5 seconds!"
(sleep 5; echo "Shutting down!") &

will print "Shut down in 5 seconds", then return you to your prompt (so you can run whatever commands you like) and after the 5 seconds are up will print "Shutting down". Is that what you wanted?

Quote:

sleep command will not work for my situation if I've figured this out correctly cause it could be any given time I put in and I need on elike 60 seconds or 30 seconds there to be a message how would i tell it to sleep for sure everytime will theres a minute left?get what I mean?
I'm not sure I do get what you mean... Something like:

Code:

seconds=10
(
for i in $(seq $seconds -1 1); do
    echo "$i seconds to shutdown...";
    sleep 1;
done;
echo "Shutdown now!") &


fatalerror0x00 11-05-2012 10:45 AM

yes thats what i wanted and when I said sleep command wasn't what i wanted that was wrong. I some reason put that there NOT thinking :P. But we will try this for statement cause thats pretty much waht I though would work. but I didn't know how to put it together and is a different style of an for statement then expected but it all works for me.

only issue still at this moment til I test this for statememt is that i cannot get sleep to run so that it doesn't interupt my terminal nothing I find works as soon as in anyway theres a & after it on the same line sleep just says no not gonna run :P and this is driving me insane. I've always hated the sleep command for this one reason :( nothing seems towork

fatalerror0x00 11-05-2012 10:49 AM

So actually the everything in ( ) worked actually not when it echoes something it's a little annoyingbut thats fine I'm thinking of getting rid of anyway so we should be good till i figure out that for statement and get it implemented

fatalerror0x00 11-05-2012 04:46 PM

So i implemented my own stuff to this for statement (my if statements to check what i enntered and/or to see if the current amount of seconds left equals any of the if's till it reaches zero and stops the server. Right now it just keeps saying the same result over and over again so after the countdown reaches 0 it still doesn't the same thing

example i type in stopphantasycraft 15 means 15 seconds (it's my minimum for the argument other then the string now) so the text server will stop in 15 seconds comes up 15 times and then the server doesn't shutdown it just stops but theres an if when the countdown reaches 0 to issue the stop command but it's not even counting down some reason I had this issue i thought i fixed it and so i added all my if statements and it's back

FIXED: I was using the wrong variable :) we are all good now this works now. SOLVED! :)


All times are GMT -5. The time now is 07:56 AM.