-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathexecinbg
executable file
·44 lines (40 loc) · 1.14 KB
/
execinbg
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
#!/usr/bin/env bash
#
# File:
# execinbg
#
# Description:
# Execute a command in the background.
#
# Usage:
# execinbg <command>
#
# Returns:
# The PID of the executed command or nothing if stdout points to a terminal.
#
# Source:
# (link is now dead)
# http://felixmilea.com/2014/12/running-bash-commands-background-properly/
#
# Notes:
# This script "will remove the command from the shell’s job list and redirect
# all of its output to /dev/null unless a file descriptor other than a
# terminal is provided."
#
# Using this script has the following advantage over using the nohup command
# directly:
# The script works by putting together all the arguments as a string and
# evaluating it as a command. The script also checks stdout and stderr to
# see if they point to a terminal, and if they do, it will pass /dev/null
# instead, otherwise it will let the command inherit the specified stdout
# and stderr.
#
if test -t 1; then
exec 1>/dev/null
fi
if test -t 2; then
exec 2>/dev/null
fi
nohup "${@}" &>/dev/null &
# Print the PID of the executed command so the caller can retrieve it.
echo "$!"