LinuxQuestions.org
Go Job Hunting at the LQ Job Marketplace
Go Back   LinuxQuestions.org > Forums > Linux Forums > Linux - Software
User Name
Password
Linux - Software This forum is for Software issues.
Having a problem installing a new program? Want to know which application is best for the job? Post your question in this forum.

Notices

Reply
 
Search this Thread
Old 02-27-2012, 08:42 AM   #1
thomas2004ch
Member
 
Registered: Aug 2009
Posts: 501

Rep: Reputation: 31
[bash script] How to catch the return string?


Hi all,

I am not sure if this is the correct place for my question, but I couldn't find out anywhere so I try here. Maybe there is someone who can help.

I have a bash-script file used to start/stop the Jboss. I want to extend this script so that each time if one starts the Jboss it will check if a Jboss instance is already started. This will avoid double or multipul startup of Jboss.

I will use the folloing code to check the Jboss:
Code:
...
ps -ef | grep java | grep org.jboss.Main
...
If I got no hit, it means no Jboss is started. But how can I get the return string or return code?

Thomas
 
Old 02-27-2012, 08:51 AM   #2
druuna
LQ Veteran
 
Registered: Sep 2003
Posts: 10,532
Blog Entries: 7

Rep: Reputation: 2374Reputation: 2374Reputation: 2374Reputation: 2374Reputation: 2374Reputation: 2374Reputation: 2374Reputation: 2374Reputation: 2374Reputation: 2374Reputation: 2374
Hi,

$? gives you the exit status of the last command that is executed.

In general: 0 (zero) all is ok and >= 1 something went wrong.

grep specific:
Quote:
EXIT STATUS
The exit status is 0 if selected lines are found, and 1 if not found.
If an error occurred the exit status is 2. (Note: POSIX error handling
code should check for '2' or greater.)
The following will show the exit status:
Code:
...
ps -ef | grep java | grep org.jboss.Main
echo "$?"
...
to quit if already running:
Code:
...
ps -ef | grep java | grep org.jboss.Main
[[ "$?" == "0" ]] && echo "already running" && exit 255
...
Hope this helps.

Last edited by druuna; 02-27-2012 at 08:52 AM.
 
1 members found this post helpful.
Old 02-27-2012, 08:59 AM   #3
thomas2004ch
Member
 
Registered: Aug 2009
Posts: 501

Original Poster
Rep: Reputation: 31
Many thanks!
 
Old 02-27-2012, 09:06 AM   #4
druuna
LQ Veteran
 
Registered: Sep 2003
Posts: 10,532
Blog Entries: 7

Rep: Reputation: 2374Reputation: 2374Reputation: 2374Reputation: 2374Reputation: 2374Reputation: 2374Reputation: 2374Reputation: 2374Reputation: 2374Reputation: 2374Reputation: 2374
You're welcome

BTW: Can you put up the [SOLVED] tag.
first post -> Thread Tools -> Mark this thread as solved
 
  


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
[SOLVED] split a string into array in bash return wrong size xeon123 Linux - Newbie 2 03-21-2011 07:16 AM
return part of a string in bash xeon123 Linux - Newbie 5 03-20-2011 09:08 PM
Bash script: catch file not found error and send to /dev/null noir911 Programming 7 04-24-2010 08:37 PM
Bash script: return code ftp TalkingMarble Linux - Newbie 3 05-09-2008 09:37 AM
return from a try catch block manolakis Programming 5 10-24-2007 02:30 PM


All times are GMT -5. The time now is 09:55 PM.

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