There are multiple ways to resolve this, but I think I would recommend this:
run() {
email=example@gmail.com
rest="${@:1:($#-1)}"
nohup bash -c "$rest < /dev/null 2>&1 | mail -s \"$rest\" \"$email\"" &
}
The issue is your nohup
command ended at the semicolon after /dev/null
and thus didn't apply to the mail
part. The ( ... )
introduces a subshell, but is not an external command, so nohup ( ... )
wouldn't work, which is why bash -c "..."
is required (you could also use ${SHELL} -c " ..."
for added portability if that's important). The double quotes you had need to be escaped when enclosed in another set of quotes. Also, the temporary file is not necessary (unless you wanted to keep it around for some reason - if that's the case, feel free to put the redirections back and separate the commands with ;
instead of |
). Without needing the output file, you could also dispense with the rest=...
bit and just call it as run script.sh arg1 arg2
, but if you already have calls to it spread throughout other code, it might be simpler to leave that bit in - you'll have to figure that part out...