How to run a command in the background and get no output?
Use nohup
if your background job takes a long time to finish or you just use SecureCRT or something like it login the server.
Redirect the stdout and stderr to /dev/null
to ignore the output.
nohup /path/to/your/script.sh > /dev/null 2>&1 &
Waiting for background processes to finish before exiting script
If you want to wait for jobs to finish, use wait
. This will make the shell wait until all background jobs complete. However, if any of your jobs daemonize themselves, they are no longer children of the shell and wait will have no effect (as far as the shell is concerned, the child is already done. Indeed, when a process daemonizes itself, it does so by terminating and spawning a new process that inherits its role).
#!/bin/sh
{ sleep 5; echo waking up after 5 seconds; } &
{ sleep 1; echo waking up after 1 second; } &
wait
echo all jobs are done!
executing shell command in background from script
Leave off the quotes
$cmd &
$othercmd &
eg:
nicholas@nick-win7 /tmp
$ cat test
#!/bin/bash
cmd="ls -la"
$cmd &
nicholas@nick-win7 /tmp
$ ./test
nicholas@nick-win7 /tmp
$ total 6
drwxrwxrwt+ 1 nicholas root 0 2010-09-10 20:44 .
drwxr-xr-x+ 1 nicholas root 4096 2010-09-10 14:40 ..
-rwxrwxrwx 1 nicholas None 35 2010-09-10 20:44 test
-rwxr-xr-x 1 nicholas None 41 2010-09-10 20:43 test~
How to prevent a background process from being stopped after closing SSH client in Linux
Check out the "nohup" program.
How to wait in bash for several subprocesses to finish, and return exit code !=0 when any subprocess ends with code !=0?
wait
also (optionally) takes the PID
of the process to wait for, and with $!
you get the PID
of the last command launched in the background.
Modify the loop to store the PID
of each spawned sub-process into an array, and then loop again waiting on each PID
.
# run processes and store pids in array
for i in $n_procs; do
./procs[${i}] &
pids[${i}]=$!
done
# wait for all pids
for pid in ${pids[*]}; do
wait $pid
done
How to put an executable script on the background and wake it up after x minutes
Just add an & to the end of last line. That is:
sleep $answer && echo foo &
How do I kill background processes / jobs when my shell script exits?
To clean up some mess, trap
can be used. It can provide a list of stuff executed when a specific signal arrives:
trap "echo hello" SIGINT
but can also be used to execute something if the shell exits:
trap "killall background" EXIT
It's a builtin, so help trap
will give you information (works with bash). If you only want to kill background jobs, you can do
trap 'kill $(jobs -p)' EXIT
Watch out to use single '
, to prevent the shell from substituting the $()
immediately.
Related Topics
Measuring Stack Usage for Linux Multi-Threaded App
Ada Program Works in Linux But Not in Gps Windows 10
How to Delete Files Over (N) Days Old But Leave (N) Files Regardless of Age
Arm: Disabling Mmu and Updating Pc
Read Data from Proc/Sys/Kernel/
Shell Script: Hexadecimal Loop
Calculating Memory of a Process Using Proc File System
How to Determinate Destination MAC Address
Replace Text Between Two Lines with Contents of a File Stored in a Variable in Sed
Gatttool Non-Interactive Mode --Char-Write
Hash ("#") Symbol in /Etc/Environment Causes String to Be Split
Python3 Unicodeencodeerror When Run via Synology Task Scheduler
Script to Check for Process & Restart Program If Not Found
Will Process Lost Wake-Up Chance in a Preemptive Kernel