I'm trying to achieve the following:
- A script shall echo some strings and execute some fast returning commands and I want to see all it's stderr and stdout output on stdout and in syslog (using logger).
- Somewhere in the script I want to start a background task that shall be fully detached (like a daemon).
- The output of this background task shall only be logged into syslog
- When the script exits, the background task shall be running as long as it needs, never being killed if the calling shell exits.
- The script's process shall be killed immediately, not be kept open by something that was started by the script.
I've tried so many things. None have worked. There is always something that keeps the scripts process open as long as the background task runs. Here is what I've tried:
#!/bin/bash
exec > >(tee >(logger -t script) 2>&1
echo "Start"
# bash just as an example for the background task
bash -c "echo 1; sleep 2; echo 2; sleep 2; echo end" > >(logger -t daemon) 2>&1 < /dev/null &
sleep 1
echo "End"
The problem here is that the scripts process keeps being open as long as the background task runs, because the subprocess inherits the tee
FIFO.
When closing FD 1 and 2 prior to starting the background task, then the script keeps being open forever, because the logger -t
daemon process never ends. Somehow the script still keeps it open after the subprocess exits.
#!/bin/bash
exec > >(tee >(logger -t script) 2>&1
echo "Start"
# bash just as an example for the background task
exec 1>&- 2>&-
bash -c "echo 1; sleep 2; echo 2; sleep 2; echo end" > >(logger -t daemon) 2>&1 < /dev/null &
sleep 1
echo "End"
Also using setsid or nohup does not change anything on this:
#!/bin/bash
exec > >(tee >(logger -t script) 2>&1
echo "Start"
# bash just as an example for the background task
exec 1>&- 2>&-
setsid bash -c "echo 1; sleep 2; echo 2; sleep 2; echo end" > >(logger -t daemon) 2>&1 < /dev/null &
sleep 1
echo "End"
I have no idea. I do not fully understand all this parent child relationship and file descriptor inheritance. Maybe someone can point me into the right direction.
I'm trying to achieve the following:
- A script shall echo some strings and execute some fast returning commands and I want to see all it's stderr and stdout output on stdout and in syslog (using logger).
- Somewhere in the script I want to start a background task that shall be fully detached (like a daemon).
- The output of this background task shall only be logged into syslog
- When the script exits, the background task shall be running as long as it needs, never being killed if the calling shell exits.
- The script's process shall be killed immediately, not be kept open by something that was started by the script.
I've tried so many things. None have worked. There is always something that keeps the scripts process open as long as the background task runs. Here is what I've tried:
#!/bin/bash
exec > >(tee >(logger -t script) 2>&1
echo "Start"
# bash just as an example for the background task
bash -c "echo 1; sleep 2; echo 2; sleep 2; echo end" > >(logger -t daemon) 2>&1 < /dev/null &
sleep 1
echo "End"
The problem here is that the scripts process keeps being open as long as the background task runs, because the subprocess inherits the tee
FIFO.
When closing FD 1 and 2 prior to starting the background task, then the script keeps being open forever, because the logger -t
daemon process never ends. Somehow the script still keeps it open after the subprocess exits.
#!/bin/bash
exec > >(tee >(logger -t script) 2>&1
echo "Start"
# bash just as an example for the background task
exec 1>&- 2>&-
bash -c "echo 1; sleep 2; echo 2; sleep 2; echo end" > >(logger -t daemon) 2>&1 < /dev/null &
sleep 1
echo "End"
Also using setsid or nohup does not change anything on this:
#!/bin/bash
exec > >(tee >(logger -t script) 2>&1
echo "Start"
# bash just as an example for the background task
exec 1>&- 2>&-
setsid bash -c "echo 1; sleep 2; echo 2; sleep 2; echo end" > >(logger -t daemon) 2>&1 < /dev/null &
sleep 1
echo "End"
I have no idea. I do not fully understand all this parent child relationship and file descriptor inheritance. Maybe someone can point me into the right direction.
Share Improve this question edited Apr 1 at 1:27 John Kugelman 362k69 gold badges552 silver badges596 bronze badges asked Mar 31 at 21:28 maikelmeyersmaikelmeyers 3581 gold badge3 silver badges9 bronze badges 4 |1 Answer
Reset to default 0If I understand correctly, you want to deattach the process from the script (like a service),
so you want disown
. disown
makes the original script not own the job which means
it runs like a service:
#!/bin/bash
exec > >(tee >(logger -t script) 2>&1
echo "Start"
# bash just as an example for the background task
bash -c "echo 1; sleep 2; echo 2; sleep 2; echo end" > >(logger -t daemon) 2>&1 < /dev/null &
disown
sleep 1
echo "End"
help disown
:
bash-5.2$ help disown
disown: disown [-h] [-ar] [jobspec ... | pid ...]
Remove jobs from current shell.
Removes each JOBSPEC argument from the table of active jobs. Without
any JOBSPECs, the shell uses its notion of the current job.
Options:
-a remove all jobs if JOBSPEC is not supplied
-h mark each JOBSPEC so that SIGHUP is not sent to the job if the
shell receives a SIGHUP
-r remove only running jobs
Exit Status:
Returns success unless an invalid option or JOBSPEC is given.
bash-5.2$
logger -t script
to have any TTY handles, f/e, then you should be redirecting its stdout and stderr. (nohup
does nothing whatsoever you can't do in native bash, btw -- in a noninteractive script the only useful thing it does is redirections; at an interactive shell it also sets HUP signals to be ignored, but you can do that yourself withdisown -h
) – Charles Duffy Commented Mar 31 at 21:46sysdig
? See sysdig/ps-lsof-netstat-time-travel for an intro to the relevant functionality -- being able to ask "okay, so what was the file descriptor table oflogger
at this specific point in time?" is extremely useful for building a mental model of what's going on and why. Similarly, it can show you each individual subshell and the FD table of that subshell, which syscalls are hanging, etc etc. – Charles Duffy Commented Mar 31 at 21:49