Bash nohup with two teams

I have a small function in mine bashrc, so I can run the script and send an email when it ends. The code is as follows:

run() {
    email=example@gmail.com
    last="${!#}"
    rest="${@:1:($#-1)}"

    (nohup $rest &> $last < /dev/null; mail -s "$rest" "$email" < "$last") &
}

This function is used as

run ./script.sh arg1 arg2 output

and it seems to work most of the time, but sometimes the message is not sent even when the script ends. I suspect that this is due to the fact that I am closing the server terminal. In these cases, the script is still running due to nohup, but does not send an email at the end. I would really appreciate some pointers to make it work fine.

+4
source share
1 answer

, :

run() {
  email=example@gmail.com
  rest="${@:1:($#-1)}"

  nohup bash -c "$rest < /dev/null 2>&1 | mail -s \"$rest\" \"$email\"" &
}

nohup, /dev/null , , mail. ( ... ) , , nohup ( ... ) , bash -c "..." ( ${SHELL} -c " ..." , ). , , . , ( - - , ; |). rest=... run script.sh arg1 arg2, , , - ...

+5

Source: https://habr.com/ru/post/1526192/


All Articles