LinuxQuestions.org
Did you know LQ has a Linux Hardware Compatibility List?
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 07-23-2008, 02:01 AM   #1
kkpal
Member
 
Registered: Oct 2007
Posts: 101

Rep: Reputation: 15
Question how to stop execution of a program if it is already running?


Hi all
how to stop execution of a program if it is already running?

I wrote a bash script and create a icon for that script.

In that script first I check running process with
ps -ef

if that process in list I will stop further execution of that script with message "Application is already running".
Script starts with single click on that icon if I use double click on that icon or clicked many times on that icons continuously It display a message "Application is already running" for all clicked.

If I clicked many times on that icons with some interval in that case it works fine.
 
Old 07-23-2008, 02:17 AM   #2
Mr. C.
Senior Member
 
Registered: Jun 2008
Posts: 2,529

Rep: Reputation: 59
kill PID, where PID is the process ID of the process you want to terminate.
 
Old 07-23-2008, 02:33 AM   #3
kkpal
Member
 
Registered: Oct 2007
Posts: 101

Original Poster
Rep: Reputation: 15
Quote:
Originally Posted by Mr. C. View Post
kill PID, where PID is the process ID of the process you want to terminate.
I do not want to kill process. I want if process is already running then it will not start.

Last edited by kkpal; 07-23-2008 at 02:34 AM.
 
Old 07-23-2008, 02:39 AM   #4
Mr. C.
Senior Member
 
Registered: Jun 2008
Posts: 2,529

Rep: Reputation: 59
Ok, you mean "prevent", not "stop".

The typical way this is done is to create lock file, such as programname.pid file in /var/run when your program starts up, and remove it when it ends. The first thing your program does is to check the existence of that file, and refuse to run if it exists.
 
Old 07-23-2008, 02:50 AM   #5
kkpal
Member
 
Registered: Oct 2007
Posts: 101

Original Poster
Rep: Reputation: 15
Question

Quote:
Originally Posted by Mr. C. View Post
Ok, you mean "prevent", not "stop".

The typical way this is done is to create lock file, such as programname.pid file in /var/run when your program starts up, and remove it when it ends. The first thing your program does is to check the existence of that file, and refuse to run if it exists.
How do I create lock file. Please explain it.
What I understood is:
if [ -e /var/run/programname.pid ]; then
"already running"
else
create /var/run/programname.pid
start process
rm -f /var/run/programname.pid
fi

Last edited by kkpal; 07-23-2008 at 03:02 AM.
 
Old 07-23-2008, 03:14 AM   #6
Mr. C.
Senior Member
 
Registered: Jun 2008
Posts: 2,529

Rep: Reputation: 59
touch /var/run/myprogname.pid

perhaps this is an easier test:

[ -e /var/run/progname.pid ] && { echo Already running ; exit 1 ; }

Then just place the rest of your script below this; no need to place it in an else.

Last edited by Mr. C.; 07-23-2008 at 12:52 PM.
 
Old 07-23-2008, 03:25 AM   #7
kkpal
Member
 
Registered: Oct 2007
Posts: 101

Original Poster
Rep: Reputation: 15
it is not solve my problem. When I double click on icon it will start start same process twice. If i gave some interval between two clicks thee it will works fine.
 
Old 07-23-2008, 11:13 AM   #8
smoked kipper
Member
 
Registered: May 2008
Location: UK
Distribution: Slackware,Slamd64
Posts: 81

Rep: Reputation: 15
Code:
[ -e /var/run/progname.pid ] && ( echo Already running ; exit 1 ; )
() runs a command in a sub-shell, so all the exit does is exit the sub-shell...

Replace the () with {}
 
Old 07-23-2008, 12:52 PM   #9
Mr. C.
Senior Member
 
Registered: Jun 2008
Posts: 2,529

Rep: Reputation: 59
Oops, my mistake. Yes, braces are correct. I've corrected above.
 
  


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
I want to know the program execution time daggulasreedhar Programming 6 03-15-2007 04:07 AM
HELP:Stopping execution of a program allomeen Programming 4 03-10-2006 06:42 PM
execution of one program inside of another microsoft/linux Programming 2 03-18-2005 10:24 AM
Key bindings program (custom pasting, program execution, etc.) jrdioko Linux - Software 2 02-05-2005 09:09 PM
Program on boot-up won't stop running in a continuous loop... CanadianSlacker Slackware 6 01-14-2004 01:06 AM


All times are GMT -5. The time now is 03:35 AM.

Main Menu
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
identi.ca: @linuxquestions
Facebook: linuxquestions Google+: linuxquestions
Open Source Consulting | Domain Registration