0

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.

Braiam
  • 67,791
  • 32
  • 179
  • 269
Dhiren Hamal
  • 215
  • 3
  • 10

2 Answers2

2

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 

[source]

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)

αғsнιη
  • 35,660
  • 1
    thank 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
1

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}')
αғsнιη
  • 35,660