Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Bash: How to show full list of job processes and kill them

I issue a command task1 & task2 dir/ && task3 &

  • Task1 never ends, so we send it to the background.
  • Task2 changes the directory
  • Task3 depends on task2 completing.

Issuing: jobs -l will show:

[1]- 39281 Running                 task1 &
[2]+ 39282 Running                 task2 && task3 &

Issuing: ps will show:

39281 ttys002    0:04.17 task1  
39282 ttys002    0:00.00 task2
39283 ttys002    0:03.66 task3

Questions:

  1. Is there a way to show task3 in the jobs command output?
  2. Why doesn't task3 show up as a its own job?
  3. Is there a way to kill all three tasks simultaneously?
  4. Why doesn't task3 die if I kill task2?
  5. Is there a better way to do this?

Goal: Issue a single line of commands to init a workflow and send it to the background. When ready kill all started processes in one go.

For context and in my case, task1 is a grunt task with livereload, so it should be sent to the background. Task2 changes the directory so that task3 can watch files changed in that directory.

like image 575
Scorpius Avatar asked Oct 02 '22 03:10

Scorpius


1 Answers

Using bash with job control you can kill the most recent two background tasks more-or-less simultaneously with

kill %% %-

If you want the job not to show the cd part you'll need to change the directory in the current shell, which means isolating the cd expression:

sleep 10000 & cd .. && { sleep 20000 & }
like image 150
kojiro Avatar answered Oct 04 '22 20:10

kojiro