LinuxQuestions.org
Welcome to the most active Linux Forum on the web.
Home Forums Tutorials Articles Register
Go Back   LinuxQuestions.org > Forums > Non-*NIX Forums > Programming
User Name
Password
Programming This forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game.

Notices


Reply
  Search this Thread
Old 10-17-2003, 01:26 AM   #1
nephilim
Member
 
Registered: Aug 2003
Location: Belgium
Distribution: Debian (server), Kubuntu (desktop)
Posts: 248

Rep: Reputation: 30
java program in background


I wrote a java program that reads from standard input.

However, if the program is launched in background, it crashes because a background process can't read from standard input. The message I get is:

[1] + Stopped (SIGTTIN) name_of_my_program

So, the obvious question to ask: is it possible to check if my java program is launched in the background so I can redirect standard input?
 
Old 10-17-2003, 02:11 AM   #2
UltimaGuy
Member
 
Registered: Aug 2003
Location: Chennai, India
Distribution: PCLinuxOS .92, FC4
Posts: 840

Rep: Reputation: 32
Yes, you can check whether the command line option includes '&' and if so, then you can use this to redirect the stdin. You have to check whether any of the args in main is '&'.
 
Old 10-17-2003, 02:33 AM   #3
nephilim
Member
 
Registered: Aug 2003
Location: Belgium
Distribution: Debian (server), Kubuntu (desktop)
Posts: 248

Original Poster
Rep: Reputation: 30
Thank you for the answer, but that won't work for me. The java command line is part of a script and the script is launched in the background. So there's no "&" in the JVM arguments.

I could define my own JVM parameter, but I would rather check in the code if the program is launched in the background. Is this possible?
 
Old 10-17-2003, 06:18 AM   #4
UltimaGuy
Member
 
Registered: Aug 2003
Location: Chennai, India
Distribution: PCLinuxOS .92, FC4
Posts: 840

Rep: Reputation: 32
Well, you have to check for the & in the script command line parameters. Are you using a bash script, or perl? Both these support command line parameters, and you have to just check for them. And if '&' is present, just call java with an option line "BACK", which you can check for in the args string, and redirect the stdin.
 
Old 10-17-2003, 06:31 AM   #5
nephilim
Member
 
Registered: Aug 2003
Location: Belgium
Distribution: Debian (server), Kubuntu (desktop)
Posts: 248

Original Poster
Rep: Reputation: 30
I see, that should do the trick indeed. Could you give me an example how a bash script can check for the "&" command line parameter?
 
Old 10-17-2003, 06:47 AM   #6
nephilim
Member
 
Registered: Aug 2003
Location: Belgium
Distribution: Debian (server), Kubuntu (desktop)
Posts: 248

Original Poster
Rep: Reputation: 30
What I actually mean is, how can I check if $1 (the first command line parameter) exists?

In other words, how to verify if there are command line parameters?
 
Old 10-17-2003, 06:51 AM   #7
UltimaGuy
Member
 
Registered: Aug 2003
Location: Chennai, India
Distribution: PCLinuxOS .92, FC4
Posts: 840

Rep: Reputation: 32
If there are no parameters, the value of the $1 will be ""(NULL), so you have no problem in checking for that. And also if there is no $1, there will be no $2 etc..

I am not very good in bash scripting, but if you want, you can build a perl script, which I have some experience, and which will be easier to customize.

But if what you do is simple, you must stick with bash.
 
Old 10-17-2003, 07:04 AM   #8
nephilim
Member
 
Registered: Aug 2003
Location: Belgium
Distribution: Debian (server), Kubuntu (desktop)
Posts: 248

Original Poster
Rep: Reputation: 30
Yes, it works now! Thank you VERY much, UltimaGuy!
 
Old 10-17-2003, 07:18 AM   #9
nephilim
Member
 
Registered: Aug 2003
Location: Belgium
Distribution: Debian (server), Kubuntu (desktop)
Posts: 248

Original Poster
Rep: Reputation: 30
I was too quick, it doesn't work...

I tested it with a bogus parameter and that worked, but it seems that "&" doesn't count as a command line parameter...

If I do ./myscript & and in the script I echo $#, the output is 0.

Now what?
 
Old 10-17-2003, 07:59 AM   #10
UltimaGuy
Member
 
Registered: Aug 2003
Location: Chennai, India
Distribution: PCLinuxOS .92, FC4
Posts: 840

Rep: Reputation: 32
In your script, check for '&' using '\&', as you have to escape system variables before checking with them.

And I don't know what echo $# does
 
Old 10-17-2003, 08:12 AM   #11
nephilim
Member
 
Registered: Aug 2003
Location: Belgium
Distribution: Debian (server), Kubuntu (desktop)
Posts: 248

Original Poster
Rep: Reputation: 30
echo $# displays the number of command line parameters. This is 0 because apparantly the & isn't considered to be a command line parameter.

I'm beginning to think I have to create 2 different scripts to solve my problem...
 
Old 10-20-2003, 06:49 AM   #12
UltimaGuy
Member
 
Registered: Aug 2003
Location: Chennai, India
Distribution: PCLinuxOS .92, FC4
Posts: 840

Rep: Reputation: 32
Whatever command line parameters I give, 'echo $#' just outputs 0(zero). I don't think that has anything to do with your problem. And, yes '&' is certainly a parameter, and I think you can check for it. I'll try this one with some of my friends and come back.
 
  


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
running rsync from a background c program BassJunkie Programming 2 08-26-2005 05:38 AM
Starting program in background jdhar Linux - Software 1 02-21-2005 02:12 PM
Run a program in the background??? KaptinKABOOM Linux - Software 5 07-06-2004 06:40 AM
Program In background frankprogramer Linux - Software 1 10-30-2002 02:56 PM
Telnet to background program Bingwiini Linux - General 0 07-09-2002 09:51 AM

LinuxQuestions.org > Forums > Non-*NIX Forums > Programming

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

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