LinuxQuestions.org
Visit Jeremy's Blog.
Home Forums Tutorials Articles Register
Go Back   LinuxQuestions.org > Forums > Non-*NIX Forums > Programming
User Name
Password
Programming This forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game.

Notices


Reply
  Search this Thread
Old 02-03-2014, 03:10 AM   #1
desiretolearn
LQ Newbie
 
Registered: Jan 2014
Posts: 25

Rep: Reputation: Disabled
clarification


hi guys
i had written a shell script Display Information of all the File Systems
i want to find the pid and kill the process after few minutes.how can i obtain the pid and kill it???

emp.sh




Code:
df -a >> /tmp/sample.log
and my cron to execute every minute every hour every day





Code:
* * * * * /tmp/emp.sh
 
Old 02-03-2014, 03:17 AM   #2
colucix
LQ Guru
 
Registered: Sep 2003
Location: Bologna
Distribution: CentOS 6.5 OpenSuSE 12.3
Posts: 10,509

Rep: Reputation: 1983Reputation: 1983Reputation: 1983Reputation: 1983Reputation: 1983Reputation: 1983Reputation: 1983Reputation: 1983Reputation: 1983Reputation: 1983Reputation: 1983
Do you mean you want to kill the df process if it hangs for some reason? Try
Code:
df -a >> /tmp/sample.log &
while ps $! > /dev/null
do
  sleep 300
  kill -9 $!
done
This will kill the process after 5 minutes. $! is the PID of the last background job.
 
1 members found this post helpful.
Old 02-03-2014, 03:24 AM   #3
desiretolearn
LQ Newbie
 
Registered: Jan 2014
Posts: 25

Original Poster
Rep: Reputation: Disabled
what i meant was my
cron:

* * * * * /tmp/emp.sh
wil execute my script every minute i want to find its pid and kill it at some point in time
 
Old 02-03-2014, 03:34 AM   #4
colucix
LQ Guru
 
Registered: Sep 2003
Location: Bologna
Distribution: CentOS 6.5 OpenSuSE 12.3
Posts: 10,509

Rep: Reputation: 1983Reputation: 1983Reputation: 1983Reputation: 1983Reputation: 1983Reputation: 1983Reputation: 1983Reputation: 1983Reputation: 1983Reputation: 1983Reputation: 1983
In that case you can retrieve the PID of the process using pgrep or kill it at once using pkill:
Code:
pkill -9 -f emp.sh
this doesn't prevent the process to start again at the next minute, anyway.
 
1 members found this post helpful.
Old 02-03-2014, 03:40 AM   #5
desiretolearn
LQ Newbie
 
Registered: Jan 2014
Posts: 25

Original Poster
Rep: Reputation: Disabled
i can directly write it on my console and kill it or should i include tis in my script

this is really silly i know please bear with me i am new to this scripting
 
Old 02-03-2014, 03:57 AM   #6
colucix
LQ Guru
 
Registered: Sep 2003
Location: Bologna
Distribution: CentOS 6.5 OpenSuSE 12.3
Posts: 10,509

Rep: Reputation: 1983Reputation: 1983Reputation: 1983Reputation: 1983Reputation: 1983Reputation: 1983Reputation: 1983Reputation: 1983Reputation: 1983Reputation: 1983Reputation: 1983
You can do it directly on the console. If you want to do it automatically, then you may think to put a while loop inside the script itself. May I ask what is the problem with this script? Why do you need to kill it and then restart it every minute from the crontab?
 
  


Reply



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
[SOLVED] I need some clarification JackNielson Linux - Newbie 5 04-22-2011 05:06 AM
[SOLVED] ASCII clarification Ronin-8 Linux - General 19 04-29-2010 12:07 AM
rsync clarification salimshahzad Linux - Server 2 02-24-2010 06:51 PM
need some clarification/help superhumanCA Linux - General 4 03-18-2008 12:19 PM
I just need some clarification Duneatreides Linux - Newbie 2 03-12-2007 05:48 AM

LinuxQuestions.org > Forums > Non-*NIX Forums > Programming

All times are GMT -5. The time now is 03:51 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
Open Source Consulting | Domain Registration