Search code examples
linuxbashsudonohup

Cannot get nohup, open_init_pty and sudo to work together


I'm struggling to combine nohup, open_init_pty and sudo in a one-liner. Something like this:

nohup open_init_pty sudo bash -c "command1;command2"

My requirements are a little strange:

  • Use nohup so parent shell can exit cleanly
  • open_init_pty as actually inside a remote SSH via PHP which requires this
  • sudo to run privileged commands
  • Run multiple commands - hence the bash -c line

I've tried to narrow down the problem using different combinations:

# works OK, output in nohup.out
nohup sudo bash -c "echo hello"

# works OK, hello output to console
open_init_pty sudo bash -c "echo hello"

# doesn't work
nohup open_init_pty sudo bash -c "echo hello"

What am I doing wrong??


Solution

  • How about doing it without nohup? You can start a subshell like this:

    (open_init_pty sudo bash -c "echo hello")
    

    I sometimes do this with a & on the end, but your examples didn't run in the background. Anyway, this may accomplish your goal of not having the job terminate if the parent shell exits.