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. |
kill PID, where PID is the process ID of the process you want to terminate.
|
Quote:
|
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. |
Quote:
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 |
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. |
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.
|
Code:
[ -e /var/run/progname.pid ] && ( echo Already running ; exit 1 ; ) Replace the () with {} |
Oops, my mistake. Yes, braces are correct. I've corrected above.
|
All times are GMT -5. The time now is 04:32 AM. |