The UNIX and Linux Forums  


Go Back   The UNIX and Linux Forums > Top Forums > Shell Programming and Scripting
.
google unix.com



Shell Programming and Scripting Post questions about KSH, CSH, SH, BASH, PERL, PHP, SED, AWK and OTHER shell scripts and shell scripting languages here.

More UNIX and Linux Forum Topics You Might Find Helpful
Thread Thread Starter Forum Replies Last Post
Run a shell script from one host which connext to remote host and run the commands SN2009 Shell Programming and Scripting 6 04-13-2009 05:39 AM
Kill the running program akhtar.bhat Shell Programming and Scripting 1 12-26-2008 07:48 AM
Script to kill rsh processes running for more than 10 mins amitsayshii UNIX for Dummies Questions & Answers 3 07-04-2006 01:45 PM
script to kill rsh processes running for more than 10 minutes amitsayshii Shell Programming and Scripting 1 06-27-2006 11:12 AM
script to kill rsh processes running for more than 10 minutes amitsayshii UNIX for Advanced & Expert Users 1 06-27-2006 11:07 AM

Reply
English Japanese Spanish French German Portuguese Italian Dutch Swedish Russian Norwegian Hungarian Hebrew Danish Bulgarian Greek Powered by Powered by Google
 
LinkBack Thread Tools Search this Thread Rate Thread Display Modes
  #1 (permalink)  
Old 10-01-2009
pcwiz pcwiz is offline
Registered User
  
 

Join Date: Aug 2008
Posts: 34
Kill shell script when host program not running/disk unmounted

I have my Mac OS X program executing a shell script (a script that copies files to a drive). I want to make it so that the shell script automatically kills itself if it finds that the host .app is not running OR kill itself if the drive that it is copying files to has been unmounted. Right now what I have is the shell script polls using the UNIX "ps" command every time before it executes a command, and then kills itself if the app, and then also uses the "df" utility to check if the disk is mounted. like this:


Code:
#!/bin/sh

checkrunning()
{
	PS=$( /bin/ps -aex )
	if [[ $PS != */Contents/MacOS/MyAppBinary* ]]
		then
		exit 0
	fi
	DISKS=$( /bin/df | grep "$VOLPATH" )
	if [[ $DISKS != *"$1"* ]]
		then
		exit 0
	fi
}

checkrunning
cp /something /something/here/

checkrunning
cp /anothersomething /something/here/

However this polling wastes resources, so is there a better way to do this?

I'm using Mac OS X 10.6 using the default shell.
  #2 (permalink)  
Old 10-02-2009
ripat ripat is offline Forum Advisor  
Registered User
  
 

Join Date: Oct 2006
Location: Belgium
Posts: 439
Quote:
Originally Posted by pcwiz View Post
However this polling wastes resources, so is there a better way to do this?
I wouldn't call this a waste of resources. All it takes is a ps and df commands which are not known as being particularly greedy.

You could maybe better target the .app application by using the -C option if provided by your OS:

Code:
if ps -C firefox-bin ;then
   echo "Firefox running"
fi

Just to make sure that I understand your question correctly: the application you are checking, is that the script itself or an external application?

If it is the very same script you want to check the running status of, you can just use a file as a lock. You create a empty file at the beginning of the script and rm it at the end.
  #3 (permalink)  
Old 10-02-2009
pcwiz pcwiz is offline
Registered User
  
 

Join Date: Aug 2008
Posts: 34
Its an external application. And you are right, I don't notice a hit in performance, so this should be OK

Thanks
Reply

Bookmarks

Tags
kill, polling, shell-script

Thread Tools Search this Thread
Search this Thread:

Advanced Search
Display Modes Rate This Thread
Rate This Thread:

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On




All times are GMT -4. The time now is 04:19 PM.


Powered by: vBulletin, Copyright ©2000 - 2006, Jelsoft Enterprises Limited. Language Translations Powered by .
vBCredits v1.4 Copyright ©2007 - 2008, PixelFX Studios
The UNIX and Linux Forums Content Copyright ©1993-2009. All Rights Reserved.Ad Management by RedTyger

Content Relevant URLs by vBSEO 3.2.0