bash 使用 sudo 运行延迟命令

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/6370208/
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

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-18 00:11:56  来源:igfitidea点击:

Running delayed command with sudo

bashsudognu-coreutils

提问by kobame

Want run a bash script as root but delayed. How can achieve this?

想要以 root 身份运行 bash 脚本但延迟了。怎样才能做到这一点?

sudo "sleep 3600; command" , or
sudo (sleep 3600; command)

does not works.

不起作用。

回答by Rajish

You can use at:

您可以使用at

sudo at next hour

And then you have to enter the command and close the file with Ctrl+D. Alternatively you can specify commands to be run in a file:

然后你必须输入命令并用Ctrl+关闭文件D。或者,您可以指定要在文件中运行的命令:

sudo at -f commands next hour

回答by chrisdowney

If you really must avoid using cron: sudo sh -c "(sleep 3600; command)&"

如果你真的必须避免使用 cron: sudo sh -c "(sleep 3600; command)&"

回答by thebodzio

How about:

怎么样:

sleep 3600; sudo <command>

anyway I would consider using cron in your case…

无论如何,我会考虑在你的情况下使用 cron ......