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.

10
  • do you mean switch to background program? and run in foreground? Sep 21, 2014 at 9:27
  • You mean to stop it or control it (connect)? Sep 21, 2014 at 9:31
  • check out this link askubuntu.com/questions/331534/…
    – kenn
    Sep 21, 2014 at 9:36
  • @KasiyA yes sir, adjactly Sep 21, 2014 at 9: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. Sep 21, 2014 at 12:55

2 Answers 2

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)

1
  • 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. Sep 21, 2014 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}')
7
  • +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? Sep 22, 2014 at 6: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. Sep 22, 2014 at 6:28
  • @user2271502 for your second Q in above comment read here and here and also alternative Sep 22, 2014 at 6: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? Sep 22, 2014 at 6:41
  • @user2271502 It's hardly to understand what exactly you want. skype doesn't use any PID to switch to itself. Sep 22, 2014 at 6:50

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .