Is there any way to open program which is running in background. I've developed an application in java for ubuntu and want to open that application through its pid if it is already being running just like skype, skype is not create new instance if it is already running whethere clicks from the launcher(menu) insted of new instance it reopen same process.
-
do you mean switch to background program? and run in foreground? – αғsнιη Sep 21 '14 at 09:27
-
You mean to stop it or control it (connect)? – Jacob Vlijm Sep 21 '14 at 09:31
-
check out this link http://askubuntu.com/questions/331534/resume-a-process-via-a-pid-number – kenn Sep 21 '14 at 09:36
-
@KasiyA yes sir, adjactly – Dhiren Hamal Sep 21 '14 at 09:47
-
@Mitch The recent edit clarifies this question is not actually about job control, as I'd thought it was. I'm voting to reopen. – Eliah Kagan Sep 21 '14 at 12:55
-
@EliahKagan I'll vote to close. This becomes a generic Linux programming question then. – muru Sep 21 '14 at 12:58
-
@muru A question about how to find and change focus to an application window seems like the sort of thing we tend to allow. We'll see how the votes fall. – Eliah Kagan Sep 21 '14 at 13:02
-
@EliahKagan still generic Linux (as in [unix.se]). As you say, though. – muru Sep 21 '14 at 13:09
-
As shown repeatedly in the comments, the OP only cares about Java-based solutions. I really don't see anything Ubuntu-specific here. – muru Sep 22 '14 at 17:10
-
Bro! this problem is directly releated to ubuntu because there is no any other way to fix it. – Dhiren Hamal Sep 22 '14 at 18:30
2 Answers
If you have already typed a command and forgot to use the &
, you can put a foreground job into the background by typing ^Z (Ctrl-Z) to suspend the job, followed by bg
to put it into the background:
$ sleep 99
^Z
[1]+ Stopped sleep 99
$ bg
[1]+ sleep 99 &
You can bring a background job into the foreground, so that the shell waits for it again, using fg
:
$ jobs
[1]+ Running sleep 99
$ fg
sleep 99
You can list the jobs of the current shell using the jobs command.
$ jobs
[1]+ Running sleep 99
If you want to run spacial job in foreground/background just use process id to move it to foreground/background. see this example:
$ sleep 99 # run first job
^Z
[1]+ Stopped sleep 99
$ bg
[1]+ sleep 99 &
$ sleep 1000 # run second job
^Z
[2]+ Stopped sleep 1000
$ jobs # view all jobs
[1]- Running sleep 99 &
[2]+ Stopped sleep 1000
$ bg %2 # move jobs number 2 to run in background
[2]+ sleep 1000 &
$ jobs # view all jobs
[1]- Running sleep 99 &
[2]+ Running sleep 1000 &
Both of jobs are running in background.
$ fg %2 #switch to second job and run it in foreground
sleep 1000 #sleep 1000 is now running in foreground
Note: You need to kill these jobs by using kill %processID
.
example: kill %2
(kill second job with process ID #2)

- 35,660
-
1thank you sir, for your comment. even I din't know what is foreground and background process, now its far cleared to me. But I've developed an application in java for ubuntu and want to open that application through its pid if it is already being running just like skype, skype is not create new instance if it is already running whethere clicks from the launcher(menu) insted of new instance it reopen same process. – Dhiren Hamal Sep 21 '14 at 12:16
According to @Peter.O's answer and question by @Omid in http://Unix.stackexchange.com, if you get the PID of the program that you know which program it is, then with follow command you can switch to you desired program:
for example if you take PID of firefox with pidof command, like pidof firefox
, you get the result something like 3547
, then pass this PID to below command and switch to your program that you are used its PID:
wmctrl -ia $(wmctrl -lp | awk -vpid=$PID '$3==3547 {print $1; exit}')
-
+1 thank you sir, It opens program through pid. Now is it possible to implement in java? what happen if wmctrl not already installed in computer? – Dhiren Hamal Sep 22 '14 at 06:24
-
@user2271502 the program that you want to open it MUST already opened before if you want to open it with its PID, as I said you need to get its PID and call above command(in answer) and use that in your program. – αғsнιη Sep 22 '14 at 06:28
-
@user2271502 for your second Q in above comment read here and here and also alternative – αғsнιη Sep 22 '14 at 06:30
-
any other commandline interface like wmctrl which is already in ubuntu, just like skype, skype not ask any enviroment that should install before its install to implement this feature? – Dhiren Hamal Sep 22 '14 at 06:41
-
@user2271502 It's hardly to understand what exactly you want. skype doesn't use any PID to switch to itself. – αғsнιη Sep 22 '14 at 06:50
-
sorry for the language, any other tool like wmctrl which is already installed in ubuntu? – Dhiren Hamal Sep 22 '14 at 06:54
-