LinuxQuestions.org
Welcome to the most active Linux Forum on the web.
Go Back   LinuxQuestions.org > Forums > Linux Forums > Linux - Newbie
User Name
Password
Linux - Newbie This Linux forum is for members that are new to Linux.
Just starting out and have a question? If it is not in the man pages or the how-to's this is the place!

Notices


Reply
  Search this Thread
Old 05-18-2010, 12:39 PM   #1
sauj
LQ Newbie
 
Registered: May 2010
Location: London
Posts: 5

Rep: Reputation: 0
Talking fuser to kill silently


I have a script that writes a log as it runs...
Code:
>> output.txt
If the script is called while it's all ready been run, It does a
Code:
tail-f output.txt file
I was wondering if there's a way to free it at the end, i.e the script should (when it's finished logging) do a
Code:
fuser -k output.txt
which works fine, except it reports back to the script to say it ran...
Code:
./myscript: line 85: 12081 Killed       tail -f output.txt
I've tried the parameters " -k -s" but still no joy... I'd prefer it did not output
Code:
./myscript: line 85: 12081 Killed       tail -f output.txt
I've tried redirecting standard error and output to /dev/null...

no joy...

Any help most welcome
 
Old 05-19-2010, 06:25 AM   #2
alli_yas
Member
 
Registered: Apr 2010
Location: Johannesburg
Distribution: Fedora 14, RHEL 5.5, CentOS 5.5, Ubuntu 10.04
Posts: 559

Rep: Reputation: 92
Quote:
I was wondering if there's a way to free it at the end, i.e the script should (when it's finished logging) do a
Could you perhaps post your script and explain what you're trying to do as you go through the script? Its a bit difficult to follow your logic unfortunately.

Is fuser only invoked if your script is executed; whilst already running?
Do you want to kill ALL processes should there be contention in terms of accessing your log file?

Cheers,
Yas
 
Old 05-19-2010, 07:23 AM   #3
sauj
LQ Newbie
 
Registered: May 2010
Location: London
Posts: 5

Original Poster
Rep: Reputation: 0
Here's a simplified version...

Code:
#!/bin/bash
clear
echo '                  Script A                               '
lock_file=/tmp/clone.lock/lock.info

echo "You want to do this (y/n) ?"
read answer
# locking mechanism, prevents the script running concurrenty, and outputs the current status
if [ -d /tmp/clone.lock ]; then
	tail -f $lock_file
	exit 1
else
   mkdir /tmp/clone.lock
   touch /tmp/clone.lock/lock.info
   echo "WARNING locked process - currently it is doing...." >> $lock_file
fi

	if [ "$answer" == "y" ]; then
        echo `date '+%T %d-%m-%Y'`
        echo "Step 1 completed" | tee -a $lock_file
		sleep 5
        echo "Step 2 completed" | tee -a $lock_file
        sleep 5
		echo "Step 3 completed" | tee -a $lock_file
        sleep 5
		echo "Step 4 completed" | tee -a $lock_file
        echo `date '+%T %d-%m-%Y'`
	else
        echo -e "Not doing this"
	fi
	
# this should kill tail -f on the $lock_file, which only get's called if the script is aready running
/sbin/fuser -k -s $lock_file &> /dev/null 
rm $lock_file
rm -r /tmp/clone.lock
 
Old 05-19-2010, 09:11 AM   #4
alli_yas
Member
 
Registered: Apr 2010
Location: Johannesburg
Distribution: Fedora 14, RHEL 5.5, CentOS 5.5, Ubuntu 10.04
Posts: 559

Rep: Reputation: 92
Quote:
if [ -d /tmp/clone.lock ]; then
Shouldn't the flag be -e in the above? Aren't you checking if the file exists? My (rusty) knowledge of bash tells me that the -d flag is to check if a file is a directory?

Cheers,
Yas
 
Old 05-19-2010, 09:58 AM   #5
sauj
LQ Newbie
 
Registered: May 2010
Location: London
Posts: 5

Original Poster
Rep: Reputation: 0
Quote:
Originally Posted by alli_yas View Post
Aren't you checking if the file exists?
Nope, Checking if the file is a directory (which it is). Basically if there is a directory, then the script is running already, if there's not...then run.
 
Old 05-19-2010, 10:15 AM   #6
alli_yas
Member
 
Registered: Apr 2010
Location: Johannesburg
Distribution: Fedora 14, RHEL 5.5, CentOS 5.5, Ubuntu 10.04
Posts: 559

Rep: Reputation: 92
Hi

Forgive me if I'm being daft - but I've taken your situation and done a little practical test on my machine (Fedora 12):

1. I created a new text file /root/test1.txt

2. I create a script called newscript.sh with the following:

Code:
#!/bin/bash
/sbin/fuser -k -s test1.txt &> /dev/null
3. I opened up 2 further shells and ran #tail -f test1.txt on both of them.

4. I then ran:

Code:
#/root/newscript.sh >> logfile.file
Doing this, it killed both the tail -f processes; and logfile.file is empty...is this not what you want to achieve?

Thanks,
Yas
 
Old 05-19-2010, 01:26 PM   #7
sauj
LQ Newbie
 
Registered: May 2010
Location: London
Posts: 5

Original Poster
Rep: Reputation: 0
Almost, the problem that I have is when you call the script...
* it does kill tail -f (awesome)
* it reports back to the terminal (i.e. the other two screens, that tail -f has been killed by (insert script name here) and by line....xx (not good)
e.g.
Code:
./scriptA: line 14: 25514 Killed             tail -f $lock_file
I'm hoping there's a way to have that NOT been returned to the terminal.

Last edited by sauj; 05-19-2010 at 01:31 PM.
 
Old 05-20-2010, 03:13 AM   #8
alli_yas
Member
 
Registered: Apr 2010
Location: Johannesburg
Distribution: Fedora 14, RHEL 5.5, CentOS 5.5, Ubuntu 10.04
Posts: 559

Rep: Reputation: 92
Hmmm....you got me on that one buddy, even on my side I've tried a couple of different things; but you can't get it NOT to output the "Killed" to the terminal
 
Old 05-20-2010, 07:18 AM   #9
sauj
LQ Newbie
 
Registered: May 2010
Location: London
Posts: 5

Original Poster
Rep: Reputation: 0
Quote:
Originally Posted by alli_yas View Post
you can't get it NOT to output the "Killed" to the terminal
I know I tried all my bash foo, and nothing. I guess it stands to reason, imagine if your looking at a file with tail -f and all of a sudden you're back a shell prompt. No explanation and the like, I'd be thinking WTF?

Genuinely thanks for giving it a go.
 
  


Reply


Thread Tools Search this Thread
Search this Thread:

Advanced Search

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 Off
HTML code is Off



Similar Threads
Thread Thread Starter Forum Replies Last Post
Nmbd crashes silently tobiascapin Debian 5 10-24-2009 06:05 PM
bash `kill`: process 'B' silently dies; but process 'A' = `kill` spews back debris! GrapefruiTgirl Programming 9 06-23-2009 10:42 AM
How to kill processes using fuser? yuxingpiao Linux - Newbie 1 05-16-2008 10:18 AM
How to silently automount DVD in FC4? darthfoolish Linux - Software 3 07-31-2006 03:11 AM
Installing Packages Fails: Silently pizmooz Slackware - Installation 2 06-21-2006 01:30 AM


All times are GMT -5. The time now is 01:07 PM.

Main Menu
Advertisement
My LQ
Write for LQ
LinuxQuestions.org is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
Syndicate
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
Facebook: linuxquestions Google+: linuxquestions
Open Source Consulting | Domain Registration