Getting sudo and nohup to work together

早过忘川 提交于 2019-12-02 20:08:49

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

The solution is to use the -b flag for sudo to run the command in the background:

$ sudo -b ./ascii_loader_script.pl 20070502 ctm_20070502.csv

You should only use nohup if you want the program to continue even after you close your current terminal session

You must use sudo first, nohup second.

sudo nohup ./ascii_loader_script.pl 20070502 ctm_20070502.csv &

First of all, you should switch sudo and nohup. And then:

if sudo echo Starting ...
then
    sudo nohup <yourProcess> &
fi

The echo Starting ... can be replaced by any command that does not do much. I only use it as dummy command for the sudo.

By this the sudo in the if-condition triggers the password-check. If it is ok then the sudo session is logged in and the second call will succeed, otherwise the if will fail and not execute the actual command.

I open an editor and typed these lines:

#!/bin/bash
sudo echo Starting ...
sudo -b MyProcess

(Where MyProcess is anything I want to run as superuser.)

Then I save the file where I want it as MyShellScript.sh .

Then change the file permissions to allow execution. Then run it in a terminal. the "-b" option tells sudo to run the process separately in the background, so the process keeps running after the terminal session dies.

Worked for me in linux-mint.

Uts

This should work

sudo -b -u userName ./myScript > logFile

I am just curious to understand that can I send this logFile as a email after the ./myScript is successful running in background.

xiaoping378

You can set it as your alias:

sudo sh -c 'nohup openvpn /etc/openvpn/client.ovpn 2>&1 > /dev/null &'
diyism

Try:

xterm -e "sudo -b nohup php -S localhost:80 -t /media/malcolm/Workspace/sites &>/dev/null"

When you close xterm, the PHP web server still alive.
Don't put nohup before sudo or else the PHP web server will be killed after closing xterm.

ThomasDr

You can Try

sudo su

and then

nohup ./ascii_loader_script.pl 20070502 ctm_20070502.csv &

instead of

nohup sudo ./ascii_loader_script.pl 20070502 ctm_20070502.csv &
标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!