1

In Ubuntu, I run date & in an interactive bash shell whose pid is 6913, and at the same time, trace the bash shell from another interactive bash shell by strace. The output of tracing the first shell 6913 in the second shell is :

$ sudo strace -f -e trace=process -p 6913
Process 6913 attached
clone(Process 12931 attached
child_stack=0, flags=CLONE_CHILD_CLEARTID|CLONE_CHILD_SETTID|SIGCHLD, child_tidptr=0x7f457c05ca10) = 12931
[pid 12931] execve("/bin/date", ["date"], [/* 66 vars */]) = 0
[pid 12931] arch_prctl(ARCH_SET_FS, 0x7f530c5ee740) = 0
[pid 12931] exit_group(0)               = ?
[pid 12931] +++ exited with 0 +++
--- SIGCHLD {si_signo=SIGCHLD, si_code=CLD_EXITED, si_pid=12931, si_status=0, si_utime=0, si_stime=0} ---
wait4(-1, [{WIFEXITED(s) && WEXITSTATUS(s) == 0}], WNOHANG|WSTOPPED|WCONTINUED, NULL) = 12931
wait4(-1, 0x7ffea6780718, WNOHANG|WSTOPPED|WCONTINUED, NULL) = -1 ECHILD (No child processes)

The bash shell 6913 clone() to create subprocess 12931.

Then the subprocess 12931 execve() date and exits.

Questions:

  • It is said that running a command in background makes the command run in a subshell of the original shell. Does that mean that the subshell (here 12931) should run the command, in the same way as the original shell 6913 runs the command directly (see below for its tracing output)?

  • If yes, why doesn't 12931 clone() itself, and then its clone execve() date? (actually 12931execve() date directly without clone() itself)


When running date instead of date &, the output of tracing the bash shell:

$ sudo strace -f -e trace=process -p 6913
[sudo] password for t: 
Process 6913 attached
clone(Process 12918 attached
child_stack=0, flags=CLONE_CHILD_CLEARTID|CLONE_CHILD_SETTID|SIGCHLD, child_tidptr=0x7f457c05ca10) = 12918
[pid  6913] wait4(-1,  <unfinished ...>
[pid 12918] execve("/bin/date", ["date"], [/* 66 vars */]) = 0
[pid 12918] arch_prctl(ARCH_SET_FS, 0x7ff00c632740) = 0
[pid 12918] exit_group(0)               = ?
[pid 12918] +++ exited with 0 +++
<... wait4 resumed> [{WIFEXITED(s) && WEXITSTATUS(s) == 0}], WSTOPPED|WCONTINUED, NULL) = 12918
--- SIGCHLD {si_signo=SIGCHLD, si_code=CLD_EXITED, si_pid=12918, si_status=0, si_utime=0, si_stime=0} ---
wait4(-1, 0x7ffea6781518, WNOHANG|WSTOPPED|WCONTINUED, NULL) = -1 ECHILD (No child processes)

Basically I want to know how a command is run in background, in bash -c, and directly in bash shell in terms of execve() and clone().

  • 2
  • Have you read them, at least the titles? The other post is about running command directly, the post here is about running command in background, and comparing running command directly and in background. – Tim Mar 3 '16 at 7:12
  • Why is the shell executing /bin/sed? Do you have date defined as an alias or shell function? – Scott Mar 3 '16 at 10:44
  • This post is not about the prompt setting as the other post linked, but about how a command is run in background, in bash -c, and directly in bash shell in terms of execve() and clone(). The part of output for my prompt setting and possibly my second part of question might make some people think this post is a duplicate of the other one, while it isn't. – Tim Mar 3 '16 at 16:23
  • Remove the prompt setting part of the tracing output, to make the post concentrate on how a command is run in background. – Tim Mar 3 '16 at 17:35

Your Answer

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

Browse other questions tagged or ask your own question.