How can I run a sudo command using nohup? I want to execute a sudo command and then terminate the ssh session but without interrupting or stopping the process.

I tried this but it doesn't work

sudo nohup . test.sh


nohup sudo <command> &

The second problem is the way of execution: . script will not work with nohup, as . is a shell internal command and nohup only takes executables. This should also be visible in nohup.out as error message.

I.e. use

nohup sudo /bin/bash script.sh &

(or whichever shell you run) or make the script an executable.

  • You changed sudo nohup into nohup sudo. May I ask why? – Kamil Maciorowski May 10 at 10:11
  • 1
    @KamilMaciorowski The nohup is then owned by the user and not root, this would make e.g. aborting simpler (no sudo needed). Also depending on how sudo handles the environment, the standard location of $HOME/nohup.out might differ. Or ownership of it. sudo nohup sleep 10 will have noup.out owned by root. – FelixJN May 10 at 10:41
  • 2
    Thank you. I asked not because I didn't know the difference. I asked because the OP chose sudo nohup either deliberately or randomly. In any case an answer that chooses to change to nohup sudo should explain why one may want to change the order. – Kamil Maciorowski May 10 at 11:15

Its not sudo command failure, but . failure because it is internal shell command. Those can be valid:

sudo nohup /bin/bash test.sh


nohup sudo /bin/bash test.sh

The difference is the owner of nohup.out

Without nohup:

sudo /bin/bash test.sh & disown %1


(sudo /bin/bash test.sh &)

Use > logfile 2>&1 if you need log in two latter cases. And depending on script nature you may not have to specify . or /bin/bash at all.

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.