Getting Sudo and Nohup to Work Together

Getting sudo and nohup to work together

The problem here, imho, is not nohup, but background processing sudo.

You are putting the process in background (& at end of command) but probably sudo needs password authentication, and that is why the process stops.

Try one of these:

1) remove the ampersand from end of command, reply to passord prompt and afterwords put it in background (by typing CTRL-Z - which stops the process and issuing the bg command to send it to background)

2) Change the /etc/sudoers to not ask for users password by including the line:
myusername ALL=(ALL) NOPASSWD: ALL

If besides the password reply your application waits for other input, then you can pipe the input to the command like this:
$ cat responses.txt|sudo mycommand.php

hth

nohup'ing a sudo command - doesn't seem to work

Use the -b option to sudo to instruct it to run the given command in the background. Job control doesn't work because the nohup process is not a child of the current shell, but of the sudo process.

sudo -b nohup dd if=/dev/zero of=/dev/sda bs=1M

Cannot get nohup, open_init_pty and sudo to work together

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.

Get the pid and return status from nohup + sudo

My final solutions:

#!/usr/bin/env bash

ROOT=$(cd `dirname $0`; pwd)

sudo kill -9 `cat ${ROOT}/pids` || true

nohup sudo node server.js >> node.log 2>&1 &

sleep 1

pid=$(ps --ppid $! | tail -1 | awk '{ print $1 }')

if echo $pid | egrep -q '^[0-9]+$'; then
echo $pid > ${ROOT}/pids
else
echo 'server not started!'
fi

Using sudoers to restrict script files for www-data? (nohup)

this line in the sudoers means www-data can sudo nohup commands with only files in this folder as parameters, passwordless:

www-data ALL = (root) NOPASSWD: /usr/bin/nohup /opt/myapp/bin/*

Having to hit enter with nohup

As far as I understand, you don't have to. The message is output to the console, but not added to your input buffer. Therefore you can just continue typing your commands in as if there were no message from nohup, the message will not interfere with your input.

Well, having to type not from the exact prompt position may be aesthetically not so pleasing.



Related Topics



Leave a reply



Submit