File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
http://aspose.com/file-tools
The moose likes Java in General and the fly likes Execute Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of JavaScript Promises Essentials this week in the JavaScript forum!
JavaRanch » Java Forums » Java » Java in General
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: 3649
    
  17

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: 18965
    
  40


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).
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: Execute "echo $BASHPID" in Linux using Java