This week's book giveaway is in the Agile and other Processes forum.
We're giving away four copies of The Mikado Method and have Ola Ellnestam and Daniel Brolund on-line!
See this thread for details.
The moose likes Java in General and the fly likes Execute Big Moose Saloon
  Search | Java FAQ | Recent Topics
Register / Login


Win a copy of The Mikado Method this week in the Agile and other Processes forum!
JavaRanch » Java Forums » Java » Java in General
Reply Bookmark "Execute "echo $BASHPID" in Linux using Java" Watch "Execute "echo $BASHPID" in Linux using Java" New topic
Author

Execute "echo $BASHPID" in Linux using Java

Erik Deveza
Greenhorn

Joined: Feb 21, 2011
Posts: 15
Hi, I am new to programming Java in Linux environment.

I need to get to execute "echo $BASHPID" in the terminal console of Linux and get the value returned.

I tried the following codes:



however, an error occurs when I execute the program. It does not recognized "echo $BASHPID". It throws "java.io.IOException: Cannot run program "echo $BASHPID": java.io.IOException: error=2, No such file or directory." Did I miss out a command function?
Stephan van Hulst
Bartender

Joined: Sep 20, 2010
Posts: 3047
    
    1

Hi Erik. Take some time to read what the java.lang.Runtime documentation says on the various exec() methods.

Note that exec() does not work like the command prompt.
Erik Deveza
Greenhorn

Joined: Feb 21, 2011
Posts: 15
Thanks for reminding me. I got the solution. I changed it to ProcessBuilder.

Henry Wong
author
Sheriff

Joined: Sep 28, 2004
Posts: 16687
    
  19


I don't see the value of this code... you are starting a new shell (bash), so you can ask the shell, what is its PID? What does that serve? The shell will terminate upon completion, so the PID is now for a terminated application. What can you do with the PID?

Henry


Books: Java Threads, 3rd Edition, Jini in a Nutshell, and Java Gems (contributor)
Erik Deveza
Greenhorn

Joined: Feb 21, 2011
Posts: 15
Henry Wong wrote:
I don't see the value of this code... you are starting a new shell (bash), so you can ask the shell, what is its PID? What does that serve? The shell will terminate upon completion, so the PID is now for a terminated application. What can you do with the PID?

Henry


Thanks, Henry, for pointing this one out. I am new to programming in Linux. I am trying to get the session id of a user in Linux. Would you know how to execute BASHPID in such a way that it gets the PID of the currently active user?
Ernie Mcracken
Ranch Hand

Joined: Feb 13, 2011
Posts: 33

Are you sure $BASHPID is really what you want, this gives your the process ID of the current shell. Maybe you want $UID the current user id?

Also $BASHPID is just an environment variable so you can't "execute" it just get its value


You're on a gravy train with biscuit wheels Roy.
Erik Deveza
Greenhorn

Joined: Feb 21, 2011
Posts: 15
Ernie Mcracken wrote:Are you sure $BASHPID is really what you want, this gives your the process ID of the current shell. Maybe you want $UID the current user id?

Also $BASHPID is just a system variable so you can't "execute" it just get its value


Thanks Ernie for dropping your comment! $UID is the current user id? I was hoping to get the user's session id. I was just thinking of this scenario: If there are 2 users using the same username and logs in from 2 remote machines to a separate Linux machine, their session user id will be their identifier because both would only return a single $UID. Would you happen to know how to get the user's session id?
Ernie Mcracken
Ranch Hand

Joined: Feb 13, 2011
Posts: 33

tty should give you a unique for every value instance of a login to a shell regardless of it the UID is the same (i think).
 
jQuery in Action, 2nd edition
 
subject: Execute "echo $BASHPID" in Linux using Java
 
Similar Threads
Launching telnet using java program fails with Win 7 OS
Running a .bat/ .cmd file from Java
How to close dos 'pause' command in Java
Calling Perl from Java
Problem in executing shell script using JSP..Code is provided