6

When I run

nohup sudo myscript.sh &

I only get something like

[1] 1220

in return and do not get prompted for a password. The process does not start either.

I want to run this script in the background and it needs to be run under sudo.

Anybody guess what I am doing wrong? Using OSX Mountain Lion (Darwin).

2 Answers 2

9

The problem is that you are putting sudo in background. I see two ways to solve this problem:

  1. remove the ampersand, enter your password and then pause the task with Ctrl+Z and then run bg so the job resumes running in the background ;
  2. dirty workaround: do a dummy sudo before (like sudo ls), then launch your nohup command, and it won't ask for your password again and will run anyway.
2
  • i like the "dirty workaround"...
    – amphibient
    Commented Sep 17, 2013 at 14:51
  • 1
    He could also launch it in screen instead of backgrounding it.
    – jordanm
    Commented Sep 17, 2013 at 15:04
9

sudo cannot prompt you for a password and read it because it is executing in the foreground. Run sudo in the foreground and arrange for it to run your program in the background.

sudo sh -c 'nohup myscript.sh &'
1
  • 1
    Beautiful, no workarounds required, this should be the accepted answer if anyone Googling comes across this. Does exactly what I needed it to do.
    – Chris
    Commented Mar 12, 2020 at 22:00

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .