1

As you can see in the following output the variable $i in the output of ps aux is expanded to sleep 1, sleep 2, etc. Is there a way to make zsh do the same? In the jobs output all the commands get the same name, which is sleep $i.

$ for i in {1..10}; do sleep $i& done; ps aux | grep sleep; jobs
[6] 1630
[7] 1631
[8] 1632
[9] 1633
[10] 1634
[11] 1635
[12] 1636
[13] 1637
[14] 1638
[15] 1639
root      1630  0.0  0.0   5224   684 pts/3    SN   10:06   0:00 sleep 1
root      1631  0.0  0.0   5224   684 pts/3    SN   10:06   0:00 sleep 2
root      1632  0.0  0.0   5224   744 pts/3    SN   10:06   0:00 sleep 3
root      1633  0.0  0.0   5224   744 pts/3    SN   10:06   0:00 sleep 4
root      1634  0.0  0.0   5224   748 pts/3    SN   10:06   0:00 sleep 5
root      1635  0.0  0.0   5224   752 pts/3    SN   10:06   0:00 sleep 6
root      1636  0.0  0.0   5224   680 pts/3    SN   10:06   0:00 sleep 7
root      1637  0.0  0.0   5224   748 pts/3    SN   10:06   0:00 sleep 8
root      1638  0.0  0.0   5224   748 pts/3    SN   10:06   0:00 sleep 9
root      1639  0.0  0.0   5224   748 pts/3    SN   10:06   0:00 sleep 10
root      1641  0.0  0.0   6144   880 pts/3    S+   10:06   0:00 grep --color=auto sleep
[6]    running    sleep $i
[7]    running    sleep $i
[8]    running    sleep $i
[9]    running    sleep $i
[10]    running    sleep $i
[11]    running    sleep $i
[12]    running    sleep $i
[13]    running    sleep $i
[14]  - running    sleep $i
[15]  + running    sleep $i

Cheers!

2

Note that it's not processes you put in foreground, but jobs, made of a shell command which could be a compound command starting several processes in parallel (like in sleep 10 | sleep 20 &) or one after the other (like in for i in {1..10}; do sleep $i; done &).

And each of these process could in turn start more processes (which would still be part of the job, but unknow to zsh as not direct descendants) or could change their argument list as reported by ps (like in sh -c 'exec env sleep 10' which runs a process that executes sh, then env, then sleep all in the same process) or could leave the job (by becoming a new process group leader).

It sounds like that for each job, you want to see the arg list of the processes in that job.

Maybe something like:

for job state ("${(@kv)jobstates}") {
  pgid=${${state%%=*}##*:}
  echo Job $job:
  pgrep -ag $pgid
}

Which on your example gives something like:

Job 2:
26590 sleep 1
Job 3:
26591 sleep 2
Job 4:
26592 sleep 3
Job 5:
26593 sleep 4
Job 6:
26594 sleep 5
Job 7:
26595 sleep 6
Job 8:
26596 sleep 7
Job 9:
26597 sleep 8
Job 10:
26598 sleep 9
Job 11:
26599 sleep 10
  • it's quite a luck that sleep isn't a builtin in zsh as in ksh. – pizdelect Feb 18 at 17:59

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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