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 12-25-2013, 11:45 PM   #1
dolphingg
LQ Newbie
 
Registered: Dec 2013
Posts: 4

Rep: Reputation: Disabled
Background process issue


Hi,

I have created one script and tried to run on test machine. PFB the code :

THRESHOLD=1
while :
do
## System usage
thiscpu_usageSys=$(top -b -n 1 | awk -F'[:,]' '/^Cpu/{sub("\\..*","",$3); print $3}')

file1=/tmp/cpu_usage.txt

## System usage
thiscpu_usageSys=$(top -b -n 1 | awk -F'[:,]' '/^Cpu/{sub("\\..*","",$3); print $3}')
if [ "$thiscpu_usageSys" -le "$THRESHOLD" ]; then
echo "System CPU USAGE CRITICAL $(date) Usage:$thiscpu_usageSys "

ps -eo pcpu,args | sort -k 1 -r | head -5 | tee -a $LOG
ps -eo pcpu,args | sort -k 1 -r | head -5 >> $file1
fi

done


Now i ran this script as :
$ cpu_usage.sh &

that means in background. After sometime i killed this process using "$!".
But still the file "/tmp/cpu_usage.txt" is getting records.

I don't know which process is using this file now? And how to solve the issue because file size is getting increas consistantly.

I am not able to get the process id of a process which is using this file now. Moreover, i am not even able to delete this file as it is getting used by some process.

Please respond
 
Old 12-26-2013, 12:37 AM   #2
divyashree
Senior Member
 
Registered: Apr 2007
Location: bbsr,orissa,India
Distribution: RHEL5 ,RHEL4,CENT OS5,FEDORA,UBUNTU
Posts: 1,363

Rep: Reputation: 135Reputation: 135
The command
Code:
lsof <filename>
will give the pid of the process accessing that file. or quick grep from the ps output will give the pid

And to run it in your background you should use nohup command. like
Code:
nohup cpu_usage.sh &
 
Old 12-26-2013, 01:06 AM   #3
dolphingg
LQ Newbie
 
Registered: Dec 2013
Posts: 4

Original Poster
Rep: Reputation: Disabled
i checked with lsof command its not giving any output.

---------- Post added 12-26-13 at 11:37 AM ----------

and what is the issued if i use & directly for background process?
 
Old 12-26-2013, 01:28 AM   #4
divyashree
Senior Member
 
Registered: Apr 2007
Location: bbsr,orissa,India
Distribution: RHEL5 ,RHEL4,CENT OS5,FEDORA,UBUNTU
Posts: 1,363

Rep: Reputation: 135Reputation: 135
Quote:
Originally Posted by dolphingg View Post
i checked with lsof command its not giving any output.

---------- Post added 12-26-13 at 11:37 AM ----------

and what is the issued if i use & directly for background process?
If the file is accessed by any process then only
PHP Code:
lsof <filename
or
PHP Code:
fuser <filename
will list the processid.

With only & the job goes background in the current session to the child session of the current terminal. If the terminal is logged-out/closed the process will also get a hangup signal and exits. But with nohup it will not attach the job to the current terminal ,so will not receive hangup signal when terminal exits. So if its a continuous process or you want to run it as daemon use nohup.

nohup is not required if huponexit is off.

Last edited by divyashree; 12-26-2013 at 01:31 AM.
 
Old 12-26-2013, 01:50 AM   #5
dolphingg
LQ Newbie
 
Registered: Dec 2013
Posts: 4

Original Poster
Rep: Reputation: Disabled
Ok agree.

But the main question is , i have already killed that background process but then also still that particular text file is getting data. and file size is getting increased. Now, how to check the reason behind that?
 
Old 12-26-2013, 01:56 AM   #6
divyashree
Senior Member
 
Registered: Apr 2007
Location: bbsr,orissa,India
Distribution: RHEL5 ,RHEL4,CENT OS5,FEDORA,UBUNTU
Posts: 1,363

Rep: Reputation: 135Reputation: 135
Quote:
Originally Posted by dolphingg View Post
Ok agree.

But the main question is , i have already killed that background process but then also still that particular text file is getting data. and file size is getting increased. Now, how to check the reason behind that?
No, then its not true. If the process is killed and no process is accessing the file, it will not get updated.

Either you have killed the wrong process or you have not checked correctly. Use the script-file-name with the lsof/fuser.

Last edited by divyashree; 12-26-2013 at 04:34 AM.
 
Old 12-26-2013, 04:20 AM   #7
deep27ak
Senior Member
 
Registered: Aug 2011
Location: Bangalore, India
Distribution: RHEL 7.x, SLES 11 SP2/3/4
Posts: 1,195
Blog Entries: 4

Rep: Reputation: 221Reputation: 221Reputation: 221
Quote:
Originally Posted by dolphingg View Post
i checked with lsof command its not giving any output.

---------- Post added 12-26-13 at 11:37 AM ----------

and what is the issued if i use & directly for background process?
Which file are you checking exactly using lsof or fuser?

As per your script the command should be
Code:
# lsof /tmp/cpu_usage.txt
or
Code:
# fuser /tmp/cpu_usage.txt
I hope you are running the above command just to be sure.
 
Old 12-26-2013, 04:34 AM   #8
dolphingg
LQ Newbie
 
Registered: Dec 2013
Posts: 4

Original Poster
Rep: Reputation: Disabled
I have checked with lsof command also. Check the output below :

$lsof top_cpuusage.sh
lsof: status error on top_cpuusage.sh: No such file or directory.

While if i check the same .sh file with "ps" command , i am getting output like below:

$ps -ef | grep top_cpuusage.sh
root 28413 27607 0 04:25 pts/1 00:00:00 grep top_cpuusage.sh
Attached Thumbnails
Click image for larger version

Name:	1.PNG
Views:	13
Size:	18.3 KB
ID:	14318  
 
Old 12-26-2013, 04:42 AM   #9
deep27ak
Senior Member
 
Registered: Aug 2011
Location: Bangalore, India
Distribution: RHEL 7.x, SLES 11 SP2/3/4
Posts: 1,195
Blog Entries: 4

Rep: Reputation: 221Reputation: 221Reputation: 221
Quote:
Originally Posted by dolphingg View Post
I have checked with lsof command also. Check the output below :

$lsof top_cpuusage.sh
lsof: status error on top_cpuusage.sh: No such file or directory.

While if i check the same .sh file with "ps" command , i am getting output like below:

$ps -ef | grep top_cpuusage.sh
root 28413 27607 0 04:25 pts/1 00:00:00 grep top_cpuusage.sh
I think you didn't noticed to what I replied above. Also I would recommend you to read man page for lsof to understand its usage.

And the second thing even though you are running the wrong command, you are also running at wrong path. Simply running that command on any location of your Linux box wouldn't give you an o/p. You need to give the path of the file i.e. path to top_cpuusage.sh
 
Old 12-26-2013, 04:58 AM   #10
divyashree
Senior Member
 
Registered: Apr 2007
Location: bbsr,orissa,India
Distribution: RHEL5 ,RHEL4,CENT OS5,FEDORA,UBUNTU
Posts: 1,363

Rep: Reputation: 135Reputation: 135
Quote:
Originally Posted by dolphingg View Post
I have checked with lsof command also. Check the output below :

$lsof top_cpuusage.sh
lsof: status error on top_cpuusage.sh: No such file or directory.

While if i check the same .sh file with "ps" command , i am getting output like below:

$ps -ef | grep top_cpuusage.sh
root 28413 27607 0 04:25 pts/1 00:00:00 grep top_cpuusage.sh
You need to give the path-to-file as argument to lsof/fuser. Or run those commands directly from inside the folder where the script exists.
 
Old 12-26-2013, 05:50 AM   #11
mddnix
Member
 
Registered: Mar 2013
Distribution: Redhat, Ubuntu
Posts: 525

Rep: Reputation: 141Reputation: 141
Quote:
Originally Posted by dolphingg View Post
I don't know which process is using this file now? And how to solve the issue because file size is getting increas consistantly.

I am not able to get the process id of a process which is using this file now. Moreover, i am not even able to delete this file as it is getting used by some process.
Command head is writing to this file. Also the process is respawning with different pid. So kill the parent pid.

Code:
$ while true; do lsof -R /tmp/cpu_usage.txt; done;
COMMAND   PID  PPID  USER   FD   TYPE DEVICE SIZE/OFF   NODE NAME
head    12692 11865 user2    1w   REG  252,2   134698 149570 /tmp/cpu_usage.txt
COMMAND   PID  PPID  USER   FD   TYPE DEVICE SIZE/OFF   NODE NAME
head    14654 11865 user2    1w   REG  252,2   135610 149570 /tmp/cpu_usage.txt
COMMAND   PID  PPID  USER   FD   TYPE DEVICE SIZE/OFF   NODE NAME
head    15634 11865 user2    1w   REG  252,2   136066 149570 /tmp/cpu_usage.txt
^C

$ kill -9 11865

Last edited by mddnix; 12-26-2013 at 05:57 AM.
 
Old 12-26-2013, 07:51 AM   #12
lemon09
Member
 
Registered: Jun 2009
Location: kolkata,India
Distribution: Mandriva,openSuse,Mint,Debian
Posts: 285
Blog Entries: 1

Rep: Reputation: 37
look you need to run the lsof command for the file you need to find the process using it. So here the file is cpu_usage.txt and not cpu_usage.sh. Refer to Deep27ak's post no #7.
Moreover the whole code you have written in while. So there may be a possibility that the outputs are in buffer. So even though you have killed the process the buffer isn't flushed yet. So from time to time it gets flushed into that file. So, it takes some time.
 
Old 12-26-2013, 09:16 AM   #13
jpollard
Senior Member
 
Registered: Dec 2012
Location: Washington DC area
Distribution: Fedora, CentOS, Slackware
Posts: 4,713

Rep: Reputation: 1279Reputation: 1279Reputation: 1279Reputation: 1279Reputation: 1279Reputation: 1279Reputation: 1279Reputation: 1279Reputation: 1279
In addition, I suspect you didn't really kill the parent process, so the commands get executed again.

and since the commands are one-shot, the times you look at the file with lsof could very likely be when nothing IS adding to the file.
 
Old 12-26-2013, 01:33 PM   #14
TB0ne
LQ Guru
 
Registered: Jul 2003
Location: Birmingham, Alabama
Distribution: SuSE, RedHat, Slack,CentOS
Posts: 19,270

Rep: Reputation: 4440Reputation: 4440Reputation: 4440Reputation: 4440Reputation: 4440Reputation: 4440Reputation: 4440Reputation: 4440Reputation: 4440Reputation: 4440Reputation: 4440
Quote:
edited out the removed post
This is an English only forum...please re-post your comments in English, as per LQ Rules.

Last edited by Mara; 12-27-2013 at 04:34 PM. Reason: previous post is removed now
 
  


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
Can I fork a foreground child process in a background process? henryyao Linux - Newbie 1 09-06-2013 04:32 PM
How to find the process ID of process executing in the background akeneche Linux - Newbie 1 01-03-2008 11:08 AM
Regarding Background process. mahabooba Linux - Distributions 3 12-26-2006 05:05 PM
how to run any binary in background - background process tej Linux - Newbie 1 08-16-2004 01:27 AM
Background Process mojozoox Linux - General 1 08-26-2003 03:16 PM

LinuxQuestions.org > Forums > Linux Forums > Linux - Newbie

All times are GMT -5. The time now is 10:18 AM.

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