wait for predefined time before exit the script
Clash Royale CLAN TAG#URR8PPP
up vote
1
down vote
favorite
I have an app server running in my unix box with process name as abc_test
. I have a shell script in which I have to stop my server using /opt/abc/bin/stop
. Now sometimes my server doesn't get stopped so I need to check whether my process is still running or not. If it is running then sleep for some seconds and then check again whether process is still running or not. If after 5 minutes process is still running, then I want to exit successfully from the shell script but if the process is not running then exit successfully as well from the shell script.
So I came up with below script but I am not able to understand how to add this 5 minute thing in my below shell script. Does my below script will do exactly as I am thinking? I guess this might be improved as well.
#!/bin/bash
/opt/abc/bin/stop
# Wait for 10 second
sleep 10
# Number of seconds to wait
WAIT_SECONDS=300
# Counter to keep count of how many seconds have passed
count=0
while pgrep abc_test > /dev/null
do
echo "server is still running."
# Wait for one second
sleep 1
# Increment the second counter
((count++))
# Has the process been killed? If so, exit the loop.
if ! pgrep abc_test > /dev/null ; then
break
fi
# Have we exceeded $WAIT_SECONDS? If so exit the loop
if [ $count -gt $WAIT_SECONDS ]; then
break
fi
done
shell-script kill
add a comment |Â
up vote
1
down vote
favorite
I have an app server running in my unix box with process name as abc_test
. I have a shell script in which I have to stop my server using /opt/abc/bin/stop
. Now sometimes my server doesn't get stopped so I need to check whether my process is still running or not. If it is running then sleep for some seconds and then check again whether process is still running or not. If after 5 minutes process is still running, then I want to exit successfully from the shell script but if the process is not running then exit successfully as well from the shell script.
So I came up with below script but I am not able to understand how to add this 5 minute thing in my below shell script. Does my below script will do exactly as I am thinking? I guess this might be improved as well.
#!/bin/bash
/opt/abc/bin/stop
# Wait for 10 second
sleep 10
# Number of seconds to wait
WAIT_SECONDS=300
# Counter to keep count of how many seconds have passed
count=0
while pgrep abc_test > /dev/null
do
echo "server is still running."
# Wait for one second
sleep 1
# Increment the second counter
((count++))
# Has the process been killed? If so, exit the loop.
if ! pgrep abc_test > /dev/null ; then
break
fi
# Have we exceeded $WAIT_SECONDS? If so exit the loop
if [ $count -gt $WAIT_SECONDS ]; then
break
fi
done
shell-script kill
2
Does my below script will do exactly as I am thinking?
. Run it and see? (And then edit with your results, if not).
â Michael Homer
Dec 17 '15 at 0:51
You could use theSECONDS
variable to see how many seconds have passed since the script began.
â muru
Dec 17 '15 at 1:01
add a comment |Â
up vote
1
down vote
favorite
up vote
1
down vote
favorite
I have an app server running in my unix box with process name as abc_test
. I have a shell script in which I have to stop my server using /opt/abc/bin/stop
. Now sometimes my server doesn't get stopped so I need to check whether my process is still running or not. If it is running then sleep for some seconds and then check again whether process is still running or not. If after 5 minutes process is still running, then I want to exit successfully from the shell script but if the process is not running then exit successfully as well from the shell script.
So I came up with below script but I am not able to understand how to add this 5 minute thing in my below shell script. Does my below script will do exactly as I am thinking? I guess this might be improved as well.
#!/bin/bash
/opt/abc/bin/stop
# Wait for 10 second
sleep 10
# Number of seconds to wait
WAIT_SECONDS=300
# Counter to keep count of how many seconds have passed
count=0
while pgrep abc_test > /dev/null
do
echo "server is still running."
# Wait for one second
sleep 1
# Increment the second counter
((count++))
# Has the process been killed? If so, exit the loop.
if ! pgrep abc_test > /dev/null ; then
break
fi
# Have we exceeded $WAIT_SECONDS? If so exit the loop
if [ $count -gt $WAIT_SECONDS ]; then
break
fi
done
shell-script kill
I have an app server running in my unix box with process name as abc_test
. I have a shell script in which I have to stop my server using /opt/abc/bin/stop
. Now sometimes my server doesn't get stopped so I need to check whether my process is still running or not. If it is running then sleep for some seconds and then check again whether process is still running or not. If after 5 minutes process is still running, then I want to exit successfully from the shell script but if the process is not running then exit successfully as well from the shell script.
So I came up with below script but I am not able to understand how to add this 5 minute thing in my below shell script. Does my below script will do exactly as I am thinking? I guess this might be improved as well.
#!/bin/bash
/opt/abc/bin/stop
# Wait for 10 second
sleep 10
# Number of seconds to wait
WAIT_SECONDS=300
# Counter to keep count of how many seconds have passed
count=0
while pgrep abc_test > /dev/null
do
echo "server is still running."
# Wait for one second
sleep 1
# Increment the second counter
((count++))
# Has the process been killed? If so, exit the loop.
if ! pgrep abc_test > /dev/null ; then
break
fi
# Have we exceeded $WAIT_SECONDS? If so exit the loop
if [ $count -gt $WAIT_SECONDS ]; then
break
fi
done
shell-script kill
shell-script kill
edited Dec 17 '15 at 22:36
Gilles
516k12210301557
516k12210301557
asked Dec 17 '15 at 0:48
user1950349
2511312
2511312
2
Does my below script will do exactly as I am thinking?
. Run it and see? (And then edit with your results, if not).
â Michael Homer
Dec 17 '15 at 0:51
You could use theSECONDS
variable to see how many seconds have passed since the script began.
â muru
Dec 17 '15 at 1:01
add a comment |Â
2
Does my below script will do exactly as I am thinking?
. Run it and see? (And then edit with your results, if not).
â Michael Homer
Dec 17 '15 at 0:51
You could use theSECONDS
variable to see how many seconds have passed since the script began.
â muru
Dec 17 '15 at 1:01
2
2
Does my below script will do exactly as I am thinking?
. Run it and see? (And then edit with your results, if not).â Michael Homer
Dec 17 '15 at 0:51
Does my below script will do exactly as I am thinking?
. Run it and see? (And then edit with your results, if not).â Michael Homer
Dec 17 '15 at 0:51
You could use the
SECONDS
variable to see how many seconds have passed since the script began.â muru
Dec 17 '15 at 1:01
You could use the
SECONDS
variable to see how many seconds have passed since the script began.â muru
Dec 17 '15 at 1:01
add a comment |Â
2 Answers
2
active
oldest
votes
up vote
3
down vote
accepted
Assuming /opt/abc/bin/stop
doesn't block your script seem to work.
As muru suggested you could skip the $count
variable and use the builtin $SECONDS
. This would lead to code like this:
/opt/abc/bin/stop
# Wait for 10 second
sleep 10
# Number of seconds to wait
WAIT_SECONDS=300
while pgrep abc_test > /dev/null
do
echo "server is still running. Seconds: $SECONDS"
# Wait for one second
sleep 1
# Have we exceeded $WAIT_SECONDS? If so exit the loop
if [ $SECONDS -gt $WAIT_SECONDS ]; then
break
fi
done
In case /opt/abc/bin/stop
DOES block, just call it in the background like:
/opt/abc/bin/stop &
add a comment |Â
up vote
0
down vote
Write the Unix command for the following: to wait for a specified number of seconds before exit.
New contributor
add a comment |Â
2 Answers
2
active
oldest
votes
2 Answers
2
active
oldest
votes
active
oldest
votes
active
oldest
votes
up vote
3
down vote
accepted
Assuming /opt/abc/bin/stop
doesn't block your script seem to work.
As muru suggested you could skip the $count
variable and use the builtin $SECONDS
. This would lead to code like this:
/opt/abc/bin/stop
# Wait for 10 second
sleep 10
# Number of seconds to wait
WAIT_SECONDS=300
while pgrep abc_test > /dev/null
do
echo "server is still running. Seconds: $SECONDS"
# Wait for one second
sleep 1
# Have we exceeded $WAIT_SECONDS? If so exit the loop
if [ $SECONDS -gt $WAIT_SECONDS ]; then
break
fi
done
In case /opt/abc/bin/stop
DOES block, just call it in the background like:
/opt/abc/bin/stop &
add a comment |Â
up vote
3
down vote
accepted
Assuming /opt/abc/bin/stop
doesn't block your script seem to work.
As muru suggested you could skip the $count
variable and use the builtin $SECONDS
. This would lead to code like this:
/opt/abc/bin/stop
# Wait for 10 second
sleep 10
# Number of seconds to wait
WAIT_SECONDS=300
while pgrep abc_test > /dev/null
do
echo "server is still running. Seconds: $SECONDS"
# Wait for one second
sleep 1
# Have we exceeded $WAIT_SECONDS? If so exit the loop
if [ $SECONDS -gt $WAIT_SECONDS ]; then
break
fi
done
In case /opt/abc/bin/stop
DOES block, just call it in the background like:
/opt/abc/bin/stop &
add a comment |Â
up vote
3
down vote
accepted
up vote
3
down vote
accepted
Assuming /opt/abc/bin/stop
doesn't block your script seem to work.
As muru suggested you could skip the $count
variable and use the builtin $SECONDS
. This would lead to code like this:
/opt/abc/bin/stop
# Wait for 10 second
sleep 10
# Number of seconds to wait
WAIT_SECONDS=300
while pgrep abc_test > /dev/null
do
echo "server is still running. Seconds: $SECONDS"
# Wait for one second
sleep 1
# Have we exceeded $WAIT_SECONDS? If so exit the loop
if [ $SECONDS -gt $WAIT_SECONDS ]; then
break
fi
done
In case /opt/abc/bin/stop
DOES block, just call it in the background like:
/opt/abc/bin/stop &
Assuming /opt/abc/bin/stop
doesn't block your script seem to work.
As muru suggested you could skip the $count
variable and use the builtin $SECONDS
. This would lead to code like this:
/opt/abc/bin/stop
# Wait for 10 second
sleep 10
# Number of seconds to wait
WAIT_SECONDS=300
while pgrep abc_test > /dev/null
do
echo "server is still running. Seconds: $SECONDS"
# Wait for one second
sleep 1
# Have we exceeded $WAIT_SECONDS? If so exit the loop
if [ $SECONDS -gt $WAIT_SECONDS ]; then
break
fi
done
In case /opt/abc/bin/stop
DOES block, just call it in the background like:
/opt/abc/bin/stop &
edited Dec 17 '15 at 2:35
muru
34.4k579149
34.4k579149
answered Dec 17 '15 at 1:09
SleepProgger
36518
36518
add a comment |Â
add a comment |Â
up vote
0
down vote
Write the Unix command for the following: to wait for a specified number of seconds before exit.
New contributor
add a comment |Â
up vote
0
down vote
Write the Unix command for the following: to wait for a specified number of seconds before exit.
New contributor
add a comment |Â
up vote
0
down vote
up vote
0
down vote
Write the Unix command for the following: to wait for a specified number of seconds before exit.
New contributor
Write the Unix command for the following: to wait for a specified number of seconds before exit.
New contributor
New contributor
answered 7 mins ago
Tara Kumari
1
1
New contributor
New contributor
add a comment |Â
add a comment |Â
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
StackExchange.ready(
function ()
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2funix.stackexchange.com%2fquestions%2f249877%2fwait-for-predefined-time-before-exit-the-script%23new-answer', 'question_page');
);
Post as a guest
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
2
Does my below script will do exactly as I am thinking?
. Run it and see? (And then edit with your results, if not).â Michael Homer
Dec 17 '15 at 0:51
You could use the
SECONDS
variable to see how many seconds have passed since the script began.â muru
Dec 17 '15 at 1:01