Find out if a script has ended or still running


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting Find out if a script has ended or still running
# 1  
Old 04-04-2012
Lightbulb Find out if a script has ended or still running

I have a script which will run for sometime, i want to monitor it's progress by sending out a mail by runing a infinite loop from command line

for eg. this is the script
Code:
$cat script1.sh

Code:
#!/usr/bin/ksh
i=0
while [ $i -lt 20 ]
do
  sleep 2
  echo $i hi
  i=`expr $i + 1`
done > testprog.out

###################################

this is how the monitoring prog will run from command line
Code:
while 1
do
 
  if [ **script1 has finished ]
  then
    mailx -s "Script Finished" < echo " "
    exit 0
  else
    mailx -s "test out" abc@def.com < testprog.out
  fi
done

###################################
However, I am not able to figure out how to do the tasks marked as **
i.e how to find out script1 is still runing or has finished ?
Moderator's Comments:
Mod Comment Hello Welcome to the UNIX and Linux Forums. Please use code tags. Video tutorial on how to use them

Last edited by Scrutinizer; 04-04-2012 at 05:21 AM.. Reason: code tags and indenting
# 2  
Old 04-04-2012
Check for the process is running for that script..
# 3  
Old 04-04-2012
Depending on your OS, you may be able to check the result of ps -fC script1.sh or ps -ef | grep [s]cript1.sh.

Alternatively, get script1.sh to write its PID to a file somewhere and check for that.
# 4  
Old 04-04-2012
Code:
while [ 1 ]
do

processes_running=`ps -fu <username>|grep -e "$JOBNAME" |grep -v grep|wc -l`
if [ $processes_running -ge 1 ]
then

<whatever you want do if jobname is running>

else

<whatever you want do if jobname is not running>

fi

done

This User Gave Thanks to elixir_sinari For This Post:
# 5  
Old 04-04-2012
Depending on what you're trying to do, this could be a good use for lock files. There's a bunch of different ways you could do it. But based on the simplicity of your example, the easiest way would probably be to first alter the first script as such:

Code:
#!/usr/bin/ksh
echo "??" > /var/lock/scriptname.lock #echo the pid of this script into a "lock" file
i=0
while [ $i -lt 20 ]
do
  sleep 2
  echo $i hi
  i=`expr $i + 1`
done > testprog.out
rm /var/lock/scriptname.lock #Remove the lock file when script is done.


Then, monitoring would be as simple as (not formatted because I'm obviously not testing this, just throwing out ideas)

Code:
until [[ ! -f /var/lock/scriptname.lock ]];do echo "nope";sleep 2;done;<whatever you want to do when the script is over>

which is really just saying until the lock file doesn't exist, sleep for 2 seconds and then check again. Once it doesn't exist anymore, kill the loop and do the next command...which is whatever you want.

Last edited by DeCoTwc; 04-04-2012 at 02:11 PM.. Reason: change monitoring example file name
This User Gave Thanks to DeCoTwc For This Post:
# 6  
Old 04-04-2012
Thanks everyone, I got this now

Special thanks to DeCoTwc for this very useful post
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

Shell script to find out process name that are running for last 10 days.

Hi all, As i am new to shell script.Please help me to write a Shell script to find out process name that are running for last 10 days. Thank's in advance. (8 Replies)
Discussion started by: manas_1988
8 Replies

2. Shell Programming and Scripting

Shell Script to find out if a process is running on any all machines

Hi All, I have been a guest visitor from a long time and this forum is wonderful. I finally am a member of this forum too, so i am here stuck with a shell script that i was trying to write My requirement is that i should be able to create a shell script which will check if a process is running on... (3 Replies)
Discussion started by: Rex0226
3 Replies

3. Shell Programming and Scripting

Find the script running time and subtract from sleeptime

HI Guys, I want to find out the script running time and subtract from sleeptime. My Script Below Give me error :- #!/usr/bin/ksh timeout=100 start=$SECONDS sleep 20 end=$SECONDS echo "Time: $((end - start)) " ScTime = $((end - start)) (1 Reply)
Discussion started by: asavaliya
1 Replies

4. Homework & Coursework Questions

number ended by 0

i have to create a simple program for my programming class and mij ide is giving me some errors. The program has to calculate the number of figures entered by a hid ended by a "0" the code i've written is: // number of numbers.cpp : Defines the entry point for the console application. //... (2 Replies)
Discussion started by: metal005
2 Replies

5. Shell Programming and Scripting

Testing a process has ended (in the background)

Hi guys. Hopefully this question will make sense! Continuing on my script to automatically copy some huge files across the network onto various servers as background jobs, I need to be able to check that each job has finished successfully. The script below shows what I want - almost. The... (2 Replies)
Discussion started by: dlam
2 Replies

6. Shell Programming and Scripting

problem in using cat and ended up with no space error in aix

While doing cat on a large file (3 GB file) , I am getting the no space error in the shell script hugefile.sh. Eg: for i in `cat hugefile.txt` do echo "$i" done error: hugefile.sh: no space Please let me know your thoughts in handling this no space issue. (2 Replies)
Discussion started by: techmoris
2 Replies

7. Linux

Why had the OpenMosix project ended?

I want to build a load-balancing cluster and I the multiprocess support then I read: 'Any single program that can run as multiple processes can benefit from OpenMosix: "The GIMP" photo editor and the "kandel" fractal generator are known to do this. Unless I can find a load-balancing cluster... (1 Reply)
Discussion started by: Advice Pro
1 Replies

8. Shell Programming and Scripting

Find username of user running the script

Hello All, If I have a simple cgi script and want to find the name of the person accessing the page, is it possible? How do I get the name of this user? Please help. I was trying a vague method but it doesn't seem to work with all browsers and versions ... $val=$ENV{'HTTP_COOKIE'}; $name... (1 Reply)
Discussion started by: garric
1 Replies

9. Shell Programming and Scripting

How to diply Files that are not ended with .sh

Cal any one let me know how to disply the files that are not ended with .sh Is there any command that to reverse the regular expression. ls -l *.sh displys all files ended with .sh exactly opposite files(not ended with .sh). (3 Replies)
Discussion started by: svenkatareddy
3 Replies

10. UNIX for Dummies Questions & Answers

find the server name a script is running on

Is there any kind of command I can use to find out the name or IP address of the server I am currently on? We have a script that exists on multiple servers and I'd like to set a variable differently based on what server I am on. (3 Replies)
Discussion started by: shellburger
3 Replies
Login or Register to Ask a Question