Silently start task in background

I know that you can use this to start a process in the background, without getting the notification when the process is put in the background and when it is done like so:

(command &) &> /dev/null

However this removes the option to trap when the process finishes (trap child_done CHLD).

How can I have both?

Answers:

Thank you for visiting the Q&A section on Magenaut. Please note that all the answers may not help you solve the issue immediately. So please treat them as advisements. If you found the post helpful (or not), leave a comment & I’ll get back to you as soon as possible.

Method 1

task-spooler might help you.

As in freshmeat.net:

task spooler is a Unix batch system where the tasks spooled run one after the other. The amount of jobs to run at once can be set at any time. Each user in each system has his own job queue. The tasks are run in the correct context (that of enqueue) from any shell/process, and its output/results can be easily watched. It is very useful when you know that your commands depend on a lot of RAM, a lot of disk use, give a lot of output, or for whatever reason it’s better not to run them all at the same time, while you want to keep your resources busy for maximum benfit. Its interface allows using it easily in scripts.

Task-spooler is available in Debian, Ubuntu and other distros.

It lets you have a queue of tasks to run, and access their outputs just at wish.

For example:

$ tsp sleep 5
0 
$ tsp sleep 5 
1
$ tsp
ID   State      Output               E-Level  Times(r/u/s)   Command [run=1/1]
2    running    /tmp/ts-out.ZWn6Le                           sleep 5
1    finished   /tmp/ts-out.Fhlcle   0        5.00/0.00/0.01 sleep 5

If you run a lot of jobs in background, it will definitely help you.

You can either run them sequentially or in a preset amount of slots. You can also establish dependencies among the tasks (run task1 only if task0 completed successfully).

Method 2

Here’s how to do it in both {ba,z}sh.

Using a function helps not needing to save and restore the state of zsh‘s monitor variable by use of setopt local_options.

# Run the command given by "[email protected]" in the background
silent_background() {
  if [[ -n $ZSH_VERSION ]]; then  # zsh:  https://superuser.com/a/1285272/365890
    setopt local_options no_notify no_monitor
    "[email protected]" &
  elif [[ -n $BASH_VERSION ]]; then  # bash: https://stackoverflow.com/a/27340076/5353461
    { 2>&3 "[email protected]"& } 3>&2 2>/dev/null
  else  # Unknownness - just background it
    "[email protected]" &
  fi
}

Using this as follows:

trap 'echo child done' CHLD
silent_background sleep 1

Will produce the expected child done message.

Method 3

The redirection of the command output seems to be irrelevant because the notification is send by the shell when a job is started asynchronously. More precisely, it is a shell feature (fonctionality) related to job control.

Here, a quote that comes from the “Bash Reference Manual”, chapter “Job Control”, section one.

The shell associates a JOB with each pipeline. It keeps a table of
currently executing jobs, which may be listed with the jobs command.
When Bash starts a job asynchronously, it prints a line that looks like:

[1] 25647

indicating that this job is job number 1 and that the process ID of the last process in the pipeline associated with this job is 25647.
All of the processes in a single pipeline are members of the same job.
Bash uses the JOB abstraction as the basis for job control.

Note that a shell script does not display this notification.

$ cat test
#!/bin/bash

true & echo true
$ ./test
true

In facts

Zsh

The Zsh documentation provides similar indications about these notifications, see man 1 zshmisc, section “JOBS”. These notifications are not displayed when job control is disabled.

MONITOR (-m, ksh: -m)

    Allow job control. Set by default in interactive shell.

zsh_prompt % setopt no_monitor
zsh_prompt % true & echo true
true
zsh_prompt %
zsh_prompt %

Bash

It seems that Bash always displays e.g. [1] 25647. The “final notification” e.g. [1]+ Done true is not displayed when job control is disabled.

bash_prompt $ true & echo true
[1] 25647
true
bash_prompt $ 
[1]+  Done                    true

Job control disabled
bash_prompt $ set +m       # disable job control
bash_prompt $ true & echo true
[1] 25685
bash_prompt $
bash_prompt $

Conclusion

I do not know if disable job control to hide the notifications is a good thing?

Resources

Method 4

You may invoke zsh as a non-interactive shell to disable job notifications.

zsh -c '
   trap "echo trapped child_done" CHLD
   sleep 5 &
   #wait
   sleep 10 # simulate other running cmds
   echo script done
'

Method 5

This is an old question, but a possible answer (in bash, not tested with zsh) is to use the job control in the subshell: (command & wait)

Note: bash suppresses the start/end background job messages in the subshell, and there no need for redirection.

Method 6

See the following example :

trap 'echo "DONE"' 0
{ find /usr & } &> /dev/null
wait

That works well. No need ( ) : that makes a not needed subshell. { } are just grouping commands.

Method 7

2 Possible Answers:

– Subshell with redirects.
     or
– Push to a Screen ( a multiplexer / virtual terminal ) session
  • The ‘Screen’ option maybe a bit convoluted, but it also gives you the ability to attach the ‘Named’ screen session and see what’s happening.
1. You can run in a subshell, and redirect all output to /dev/null.

$( { sleep 10; touch a_file } >/dev/null 2>&1 & ) >/dev/null 2>&1;
2. A detached screen session.
  • Create Screen with a name Detached.

screen -dmS "${SCREEN_NAME}";
  • Run (stuff) the command(s) into the named screen session.

screen -S "${SCREEN_NAME}" -X stuff "${CMD_LINES}nsleep ${SECS}ntouch a_filenexitn";
I like adding a touched file to watch and wait for the backgrounded process to complete before Exiting the main script


All methods was sourced from stackoverflow.com or stackexchange.com, is licensed under cc by-sa 2.5, cc by-sa 3.0 and cc by-sa 4.0

0 0 votes
Article Rating
Subscribe
Notify of
guest
0 Comments
Inline Feedbacks
View all comments