LinuxQuestions.org
Visit Jeremy's Blog.
Home Forums Tutorials Articles Register
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 02-18-2016, 01:01 AM   #1
Sir_Q1
LQ Newbie
 
Registered: Feb 2016
Posts: 3

Rep: Reputation: Disabled
How do I prevent my .sh or .bat file from launching a second instance is I already have the file running?


I am very new to the world of Linux, so please with as I describe my problem. I have .sh file that I'm using to launch a .jar file. My launching script in the file is:

java -Djava.net.preferIPv4Stack=true -Dsun.java2d.opengl=true -jar file-name.jar "/home/user/CIP_01/config/file-name.properties"

It works just fine and is quite reliable, however, if a user is not paying attention and minimizes the program (out of sight, out my thing) they can actually launch a second instance of the program. This causes a few problems for them.

My question is how can I prevent this from happening.

Thanks in advance.
 
Old 02-18-2016, 01:40 AM   #2
grail
LQ Guru
 
Registered: Sep 2009
Location: Perth
Distribution: Manjaro
Posts: 10,007

Rep: Reputation: 3191Reputation: 3191Reputation: 3191Reputation: 3191Reputation: 3191Reputation: 3191Reputation: 3191Reputation: 3191Reputation: 3191Reputation: 3191Reputation: 3191
Have a look at the pgrep command. You can then look for your running program before launching.

Another alternative would be to store the PID at the start of the program in a temp file and then access it at the start to see if it exists and check that the PID is still running.
 
Old 02-18-2016, 05:21 AM   #3
Sir_Q1
LQ Newbie
 
Registered: Feb 2016
Posts: 3

Original Poster
Rep: Reputation: Disabled
Thanks for the quick reply. I'll go and check it out. Hopefully it's fairly intuitive.
 
Old 02-18-2016, 08:47 AM   #4
BW-userx
LQ Guru
 
Registered: Sep 2013
Location: Somewhere in my head.
Distribution: Slackware (15 current), Slack15, Ubuntu studio, MX Linux, FreeBSD 13.1, WIn10
Posts: 10,342

Rep: Reputation: 2242Reputation: 2242Reputation: 2242Reputation: 2242Reputation: 2242Reputation: 2242Reputation: 2242Reputation: 2242Reputation: 2242Reputation: 2242Reputation: 2242
add something like this in your script

Code:
#!/bin/bash

whatsrunning="$(pidof firefox)"

[[ $whatsrunning ]] && echo "$whatsrunning">/dev/null || echo " start applicating instead"
where firefox is your program you're checking to see if it
is already running. if yes then send results to NULL else start it.
OR
Code:
#!/bin/bash

whatsrunning="$(pidof firefox)"

[[ ! $whatsrunning ]] && echo "starting applicating"
if it is not running then start it, replace echo statement with the name of the app you want started to start it.

if it is already running then nothing will take place unless you code it to within your script.

Code:
#!/bin/bash


whatsrunning="$(pidof "whatever your program is called")"

[[ ! $whatsrunning ]] && java -Djava.net.preferIPv4Stack=true \
-Dsun.java2d.opengl=true \
-jar \
file-name.jar "/home/user/CIP_01/config/file-name.properties"
that might work, I don't know if I chopped it up properly in the right areas of that entire command.

Last edited by BW-userx; 02-18-2016 at 09:24 AM.
 
Old 02-18-2016, 03:00 PM   #5
Sir_Q1
LQ Newbie
 
Registered: Feb 2016
Posts: 3

Original Poster
Rep: Reputation: Disabled
Cool thanks. It looks easy enough. I'll when I get back to the site.

Thank you very much
 
Old 02-18-2016, 07:33 PM   #6
grail
LQ Guru
 
Registered: Sep 2009
Location: Perth
Distribution: Manjaro
Posts: 10,007

Rep: Reputation: 3191Reputation: 3191Reputation: 3191Reputation: 3191Reputation: 3191Reputation: 3191Reputation: 3191Reputation: 3191Reputation: 3191Reputation: 3191Reputation: 3191
The suggestion above is good, but be careful if you have more than one java application running as you cannot do pidof the other identifying pieces of the command.
 
Old 02-18-2016, 07:51 PM   #7
BW-userx
LQ Guru
 
Registered: Sep 2013
Location: Somewhere in my head.
Distribution: Slackware (15 current), Slack15, Ubuntu studio, MX Linux, FreeBSD 13.1, WIn10
Posts: 10,342

Rep: Reputation: 2242Reputation: 2242Reputation: 2242Reputation: 2242Reputation: 2242Reputation: 2242Reputation: 2242Reputation: 2242Reputation: 2242Reputation: 2242Reputation: 2242
good point @grail

I yanked this out of one of my other scripts. he maybe able to mod it. first he got a do, ps -au after he starts it for a test run then pick out all of the names of everything that whole command starts if it is in fact more then one app/proccess then write down all of the names of each peice that is running as a result of that whole command line. then ...

brain teaser for you @Sir_Q1

it is all here for you. All you got a do is figure out what to change and or add, or remove to make it work for you.... Learning is a proccess

Code:
if [ $(pgrep -u $USER setbg | wc -l) -ge 2 ] && [ $(pgrep -u $USER sleep | wc -l ) -ge 1 ]; then
	echo $(pgrep -u $USER setbg | wc -l) "pid# --" $(pgrep -u $USER sleep | wc -l)
	pkill -u $USER -o sleep #the program name is here
	pkill -u $USER -o setbg  #the script name is here
else

something else

fi

Last edited by BW-userx; 02-18-2016 at 08:03 PM.
 
1 members found this post helpful.
  


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
Help required for Running SQLPLUS command from Bat file PavanPatil General 15 01-28-2014 10:35 PM
.bat file conversion to .sh file (debian) AlanFletcher Programming 7 04-19-2012 06:47 PM
[SOLVED] prevent rsync from running if another instance is running - how? van_Zeller Linux - Software 5 04-13-2011 03:43 AM
How to convert a batch file(.bat) to script file(.sh) manas_sem Programming 4 06-28-2007 12:10 PM

LinuxQuestions.org > Forums > Linux Forums > Linux - Newbie

All times are GMT -5. The time now is 06:39 PM.

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