I have a bash script which is executed via the GUI (lightdm).

In some line during the script, I want it to:

  1. sudo service lightdm stop - stop the GUI
  2. Take over a TTY (like tty1 or tty7) using e.g.: exec </dev/tty7 >/dev/tty7 2>&1
    (if agetty controls the TTY, we can easily send it SIGSTOP).
    tty7 is preferred since that while shutting down lightdm, it will be available.

The problem is that the bash script is a child of the lightdm service.
Therefore while executing sudo service lightdm stop - it will try to terminate my script/process.

I found out that I need to use nohup but I prefer to have my already running script/process immune to SIGHUP.
I've seen suggestions about disown and bg but a script file isn't in interactive mode therefore I can't bg it. Also using set -m won't help because that anyway I need to press CTRL+Z in order to suspend my process (Sending SIGTSTP will suspend it but the file won't continue to run...).

What is the reliable & robust approach to solve this?
Using Ubuntu.

Update 8 Oct 21:35 UTC - Replying to @countermode:

I tried:

trap '' HUP
service lightdm stop

also tried:

trap '' SIGHUP
service lightdm stop

But it failed. I know that it failed because that after the line service lightdm stop I write to files some data. Those files modification time is too old (a day ago), thus it doesn't work.

I tried to catch all the signals that I thought I was missing, using:

trap_with_arg() {
    func="$1" ; shift
    for sig ; do
        trap "trap_handler $sig" "$sig"
trap_handler() {
    echo -e " \n Caught signal $1 \n" >>/path_to_some_folder/_signals.txt
echo "" >/path_to_some_folder/_signals.txt
for i in {1..64} ; do
    signals_list+="$(kill -l $i) "
trap_with_arg trap_handler 0 $signals_list

service lightdm stop

As advised here.
All the signals which were caught are (left was first): HUP, CONT, 0.

So I tried to:

trap '' 0 HUP CONT
service lightdm stop

But it failed as well...

Update 9 Oct 08:46 UTC:

Actually the answer of @countermode works well.
I had a tiny mistake in the code so I thought it doesn't work.


Shell scripts can set their own signal handlers using the trap keyword. In your scenario you want to ignore SIGHUP, so you would call somewhere before terminating the ldm session

trap '' SIGHUP

where '' (the empty string) indicates that you want to ignore SIGHUP (you would otherwise specify the signal handling code here).

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.