Take the 2-minute tour ×
Ask Ubuntu is a question and answer site for Ubuntu users and developers. It's 100% free, no registration required.

I am launching two processes in a bash script:

run_webserver

xdg-open http://$(hostname)/dev-environment

The run_webserver is an ongoing process, so I need to be able to kill it with ctrl-c. The xdg-open command simply launches a page in the browser. If I separate these commands with an ampersand (&), the second process takes away "focus" from the first and I can no longer kill it with ctrl+c... must instead look it up in ps and run kill -9. Any ideas?

share|improve this question

3 Answers 3

&(Ampersand) sends the process to the background. It means if you run "run_webserver &" then this process will run in the backgrond.

You can bring this process to foreground by looking at the job id for the process in the terminal.

Steps to bring the process to foreground and kill

  1. jobs :--> this command will list all the process running on the terminal.

  2. fg :--> this command will bring the process to the foreground.

Then you can use your ctrl-c to kill the process, or any other sequence which kills the process.

Please find the attached screen shots:-

enter image description here

Pressing q helped me quit the process

share|improve this answer
    
Is there a way to do this without manually checking jobs? I tried running it as follows and I get the error "fg: no job control": run_webserver & xdg-open http://$(hostname)/dev-environment && fg %1 –  frankadelic Aug 6 '12 at 19:48
    
if you're sure that these are the only two processes running on the terminal , i.e a) run_webserver b) xdg-open http://$(hostname)/dev-environment then you can also use kill -15 %<job-id>. In our case run-webserver would have job-id 1 and 2nd process would have job-id:2. In that way you don't need to issue jobs command. Hope this helps. –  Ankit Aug 7 '12 at 1:58

You can set a trap to kill the background process on exit, and wait for the background process.

#!/bin/bash

trap 'kill $!' EXIT
run_webserver &
xdg-open "http://$HOSTNAME/dev-environment"
wait

Alternatively, run the other command in the background instead

#!/bin/bash

xdg-open "http://$HOSTNAME/dev-environment" &
run_webserver

See also http://mywiki.wooledge.org/ProcessManagement

share|improve this answer

fg will return the most recent process to the foreground. fg - will return the penultimate process to the foreground.

Also, you should only use kill -9 as a last resort, after everything else has failed, because SIGKILL (-9) doesn't permit the program to do any cleanup before exiting. First, just send kill by itself, which is equivalent to kill -TERM. If that fails after waiting a while, send SIGHUP: kill -HUP. Only after those signals fail should you try SIGKILL.

share|improve this answer

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.