LinuxQuestions.org

LinuxQuestions.org (/questions/)
-   Linux - Newbie (https://www.linuxquestions.org/questions/linux-newbie-8/)
-   -   how to stop execution of a program if it is already running? (https://www.linuxquestions.org/questions/linux-newbie-8/how-to-stop-execution-of-a-program-if-it-is-already-running-657610/)

kkpal 07-23-2008 02:01 AM

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.

Mr. C. 07-23-2008 02:17 AM

kill PID, where PID is the process ID of the process you want to terminate.

kkpal 07-23-2008 02:33 AM

Quote:

Originally Posted by Mr. C. (Post 3223432)
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.

Mr. C. 07-23-2008 02:39 AM

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.

kkpal 07-23-2008 02:50 AM

Quote:

Originally Posted by Mr. C. (Post 3223453)
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

Mr. C. 07-23-2008 03:14 AM

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.

kkpal 07-23-2008 03:25 AM

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.

smoked kipper 07-23-2008 11:13 AM

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 {}

Mr. C. 07-23-2008 12:52 PM

Oops, my mistake. Yes, braces are correct. I've corrected above.


All times are GMT -5. The time now is 04:32 AM.