LinuxQuestions.org

LinuxQuestions.org (/questions/)
-   Linux - Server (https://www.linuxquestions.org/questions/linux-server-73/)
-   -   Shell script for time count (https://www.linuxquestions.org/questions/linux-server-73/shell-script-for-time-count-4175540296/)

sagar666 04-21-2015 01:03 AM

Shell script for time count
 
Hi Guys,

I want a syntax or suggestion for implement below.

-->start x command and count time
--->send y cmd after 20 mins and stop count time
---> send z cmd and start count time.
Thanks in advance

T3RM1NVT0R 04-21-2015 01:34 AM

It will be good if you share the script that you have written so far. Let us know where exactly you are stuck and what exactly you are trying to do?

sagar666 04-21-2015 07:06 AM

Below is my script what i am trying but it is not working because timeout will kill the process after 20 minutes and executes further commands. I don't want to kill the process it has go to execute further commands.
#!/bin/sh

timeout 20m sh /dv4/delete-dv4.sh
PID=`echo $$`
if ps -p $PID > /dev/null
then
echo "$PID is running"
fi
timeout 20m sh /dv4/delete-dv4.sh
PID=`echo $$`
if ps -p $PID > /dev/null
then
echo "$PID is still running"
fi
kill -s 15 $PID

1. My requirement is sh /dv4/delete-dv4.sh has to be run up to 20 minutes and count time in X varable
3. After 20 minutes i need to send My process running
4.After 20 minutes again i need to be start sh /dv4/delete-dv4.sh and start count time from where it stopped earlier (In x Variable)
5. After 20 minutes i need to send My process is still running
6 If count time reached 40 minutues
7 kill the process

T3RM1NVT0R 04-21-2015 07:28 AM

Quote:

Originally Posted by sagar666 (Post 5350655)
Below is my script what i am trying but it is not working because timeout will kill the process after 20 minutes and executes further commands. I don't want to kill the process it has go to execute further commands.
#!/bin/sh

timeout 20m sh /dv4/delete-dv4.sh
PID=`echo $$`
if ps -p $PID > /dev/null
then
echo "$PID is running"
fi
timeout 20m sh /dv4/delete-dv4.sh
PID=`echo $$`
if ps -p $PID > /dev/null
then
echo "$PID is still running"
fi
kill -s 15 $PID

1. My requirement is sh /dv4/delete-dv4.sh has to be run up to 20 minutes and count time in X varable
3. After 20 minutes i need to send My process running
4.After 20 minutes again i need to be start sh /dv4/delete-dv4.sh and start count time from where it stopped earlier (In x Variable)
5. After 20 minutes i need to send My process is still running
6 If count time reached 40 minutues
7 kill the process

It is always good to put your script in code tags as it makes it easy to read. In the script I don't see that you are killing the script the first time. So where the question comes to start it again? Are you saying that it should run for 20 minutes, send the status, kill the process and then start the same process again with 20 minutes timer, at the end of timer send the status, kill the process and shouldn't start again?

veerain 04-21-2015 09:51 AM

This may do:

Code:

TX=`date +s`
PID=`pidof sh /dv4/delete-dv4.sh`
echo running  $PID ...
TC=`date +%s`
TD=$(($TC-$TX))
while [ ! $TD -ge 1200 ]; do
sleep 5s
TC=`date +%s`
TD=$(($TC-$TX))
done
kill $PID
PID=`pidof sh /dv4/delete-dv4.sh`
echo running $PID ...
TC=`date +%s`
TD=$(($TC-$TX))
while [ ! $TD -ge 2400 ]; do
sleep 5s
TC=`date +%s`
TD=$(($TC-$TX))
done
kill $PID



All times are GMT -5. The time now is 04:12 AM.