Linux 让 sudo 和 nohup 一起工作
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/17475098/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Getting sudo and nohup to work together
提问by Alex Chen
Linux newbie here.
Linux 新手在这里。
I have a perl script which takes two command line inputs. I tried to run it in the background but this is what I got:
我有一个需要两个命令行输入的 perl 脚本。我试图在后台运行它,但这是我得到的:
[~user]$ nohup sudo ./ascii_loader_script.pl 20070502 ctm_20070502.csv &
[2] 19603
[~user]$ nohup: appending output to `nohup.out'
after the system returns "nohup: appending output to `nohup.out'", no new prompt will appear. Then as long as I type in some other command, the shell will tell me that the process is stopped:
系统返回“nohup:appending output to `nohup.out'”后,不会出现新的提示。然后只要我输入一些其他命令,shell 就会告诉我该进程已停止:
[~user]$ nohup sudo ./ascii_loader_script.pl 20070502 ctm_20070502.csv &
[2] 19603
[~user]$ nohup: appending output to `nohup.out'
ls
ascii_loader_script.pl format_wrds_trd.txt nohup.out norm_wrds_trd.cfg
[2]+ Stopped nohup sudo ./ascii_loader_script.pl 20070502 ctm_20070502.csv
I've looked at this post and tried to do "sudo date" before executing the command. Still got the same thing. http://www.sudo.ws/pipermail/sudo-users/2003-July/001648.html
我看过这篇文章并尝试在执行命令之前执行“sudo date”。还是得到了同样的东西。 http://www.sudo.ws/pipermail/sudo-users/2003-July/001648.html
采纳答案by Bandiera
The problem here, imho, is not nohup, but background processing sudo.
恕我直言,这里的问题不是 nohup,而是后台处理 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.
您将进程置于后台(& 在命令末尾),但 sudo 可能需要密码身份验证,这就是进程停止的原因。
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)
1)从命令末尾删除&符号,回复密码提示和后记将其置于后台(通过键入CTRL-Z - 停止进程并发出bg命令将其发送到后台)
2) Change the /etc/sudoers to not ask for users password by including the line: myusername ALL=(ALL) NOPASSWD: ALL
2)通过包含以下行将 /etc/sudoers 更改为不要求用户密码: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
如果除了密码回复之外,您的应用程序还等待其他输入,那么您可以将输入通过管道传递给命令,如下所示: $ cat Responds.txt|sudo mycommand.php
hth
第
回答by dusktreader
The solution is to use the -b flag for sudo to run the command in the background:
解决方案是使用 -b 标志让 sudo 在后台运行命令:
$ 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
如果您希望程序在关闭当前终端会话后继续运行,您应该只使用 nohup
回答by 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.
当您关闭 xterm 时,PHP
Web 服务器仍处于活动状态。
不要放在nohup
之前sudo
,否则PHP
关闭 xterm 后 Web 服务器将被杀死。
回答by 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.
我只是想知道,在 ./myScript 在后台成功运行后,我可以将此日志文件作为电子邮件发送。
回答by Frederic Leitenberger
First of all, you should switch sudo
and nohup
.
And then:
首先,您应该切换sudo
和nohup
。进而:
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
.
该echo Starting ...
可以通过没有做太多的任何命令来代替。我只将它用作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.
通过这个sudo
if 条件触发密码检查。如果没问题,则sudo
会话登录,第二次调用将成功,否则if
将失败并且不执行实际命令。
回答by QkiZ
You must use sudo first, nohup second.
您必须先使用 sudo,然后使用 nohup。
sudo nohup ./ascii_loader_script.pl 20070502 ctm_20070502.csv &
回答by DVOPS
I open an editor and typed these lines:
我打开一个编辑器并输入以下几行:
#!/bin/bash
sudo echo Starting ...
sudo -b MyProcess
(Where MyProcessis anything I want to run as superuser.)
(其中MyProcess是我想以超级用户身份运行的任何内容。)
Then I save the file where I want it as MyShellScript.sh .
然后我将文件保存为 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.
然后更改文件权限以允许执行。然后在终端中运行它。“-b”选项告诉 sudo 在后台单独运行该进程,因此该进程在终端会话终止后继续运行。
Worked for me in linux-mint.
在 linux-mint 为我工作。
回答by xiaoping378
You can set it as your alias:
您可以将其设置为别名:
sudo sh -c 'nohup openvpn /etc/openvpn/client.ovpn 2>&1 > /dev/null &'
回答by 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 &