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 |
Welcome to LinuxQuestions.org, a friendly and active Linux Community.
You are currently viewing LQ as a guest. By joining our community you will have the ability to post topics, receive our newsletter, use the advanced search, subscribe to threads and access many other special features. Registration is quick, simple and absolutely free. Join our community today!
Note that registered members see fewer ads, and ContentLink is completely disabled once you log in.
Are you new to LinuxQuestions.org? Visit the following links:
Site Howto |
Site FAQ |
Sitemap |
Register Now
If you have any problems with the registration process or your account login, please contact us. If you need to reset your password, click here.
Having a problem logging in? Please visit this page to clear all LQ-related cookies.
Get a virtual cloud desktop with the Linux distro that you want in less than five minutes with Shells! With over 10 pre-installed distros to choose from, the worry-free installation life is here! Whether you are a digital nomad or just looking for flexibility, Shells can put your Linux machine on the device that you want to use.
Exclusive for LQ members, get up to 45% off per month. Click here for more info.
|
 |
07-23-2008, 02:01 AM
|
#1
|
Member
Registered: Oct 2007
Posts: 101
Rep:
|
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.
|
|
|
07-23-2008, 02:17 AM
|
#2
|
Senior Member
Registered: Jun 2008
Posts: 2,529
Rep:
|
kill PID, where PID is the process ID of the process you want to terminate.
|
|
|
07-23-2008, 02:33 AM
|
#3
|
Member
Registered: Oct 2007
Posts: 101
Original Poster
Rep:
|
Quote:
Originally Posted by Mr. C.
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.
|
|
|
07-23-2008, 02:39 AM
|
#4
|
Senior Member
Registered: Jun 2008
Posts: 2,529
Rep:
|
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.
|
|
|
07-23-2008, 02:50 AM
|
#5
|
Member
Registered: Oct 2007
Posts: 101
Original Poster
Rep:
|
Quote:
Originally Posted by Mr. C.
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.
|
|
|
07-23-2008, 03:14 AM
|
#6
|
Senior Member
Registered: Jun 2008
Posts: 2,529
Rep:
|
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.
|
|
|
07-23-2008, 03:25 AM
|
#7
|
Member
Registered: Oct 2007
Posts: 101
Original Poster
Rep:
|
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.
|
|
|
07-23-2008, 11:13 AM
|
#8
|
Member
Registered: May 2008
Location: UK
Distribution: Slackware,Slamd64
Posts: 81
Rep:
|
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 {}
|
|
|
07-23-2008, 12:52 PM
|
#9
|
Senior Member
Registered: Jun 2008
Posts: 2,529
Rep:
|
Oops, my mistake. Yes, braces are correct. I've corrected above.
|
|
|
All times are GMT -5. The time now is 04:32 PM.
|
LinuxQuestions.org is looking for people interested in writing
Editorials, Articles, Reviews, and more. If you'd like to contribute
content, let us know.
|
Latest Threads
LQ News
|
|