如何以与根 cronjob 不同的用户身份运行命令?

发布于 2024-08-03 17:49:21 字数 531 浏览 2 评论 0原文

我似乎陷入了 NFS 限制和 Cron 限制之间。

因此,我让 root cron(在 RHEL5 上)运行一个 shell 脚本,除其他外,该脚本还需要通过 NFS 安装 rsync 一些文件。 NFS 挂载上的文件由模式 700 的 apache 用户拥有,因此只有 apache 用户可以运行 rsync 命令——以 root 身份运行会产生权限错误(显然,NFS 是一种罕见的情况,其中 root 用户是不是全能的?)

当我只想手动运行 rsync 时,我可以使用“sudo -u apache rsync ...”但是 sudo no workie in cron - 它说“sudo:抱歉,你必须有一个 tty”运行 sudo”。

我不想将整个脚本作为 apache 运行(即从 apache 的 crontab 运行),因为脚本的其他部分确实需要 root——这只是需要作为 apache 运行的一个命令。我真的不想更改文件的模式,因为这将涉及对其他应用程序的重大更改。

必须有一种方法可以从 cron 完成“sudo -u apache”?

谢谢! 抢

I seem to be stuck between an NFS limitation and a Cron limitation.

So I've got root cron (on RHEL5) running a shell script that, among other things, needs to rsync some files over an NFS mount. And the files on the NFS mount are owned by the apache user with mode 700, so only the apache user can run the rsync command -- running as root yields a permission error (NFS being a rare case, apparently, where the root user is not all-powerful?)

When I just want to run the rsync by hand, I can use "sudo -u apache rsync ..." But sudo no workie in cron -- it says "sudo: sorry, you must have a tty to run sudo".

I don't want to run the whole script as apache (i.e. from apache's crontab) because other parts of the script do require root -- it's just that one command that needs to run as apache. And I would really prefer not to change the mode on the files, as that will involve significant changes to other applications.

There's gotta be a way to accomplish "sudo -u apache" from cron??

thanks!
rob

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(5

明媚殇 2024-08-10 17:49:21

su --shell=/bin/bash --session-command="/path/to/command -argument=something" 用户名 &

对我有用(CentOS)

su --shell=/bin/bash --session-command="/path/to/command -argument=something" username &

Works for me (CentOS)

琉璃梦幻 2024-08-10 17:49:21

使用 su 而不是 sudo

su -c "rsync ..." apache

Use su instead of sudo:

su -c "rsync ..." apache
绿萝 2024-08-10 17:49:21

默认情况下,在 RHEL 上,没有终端 (tty) 的进程不允许使用 sudo。这是在 /etc/sudoers 中设置的。

您可以使用以下说明为特定用户允许无 tty sudo

https://serverfault.com/questions/111064/sudoers-how-to-disable-requiretty-per-user

By default on RHEL, sudo isn't allowed for processes without a terminal (tty). That's set in /etc/sudoers.

You can allow tty-less sudo for particular users with these instructions:

https://serverfault.com/questions/111064/sudoers-how-to-disable-requiretty-per-user

薯片软お妹 2024-08-10 17:49:21

如果你想永久地让你以 apache 的身份摆弄:

chsh apache

这允许你更改用户的 shell

If you want to permanently enable you to fiddle around as apache:

chsh apache

this allows you to change the shell for the user

⒈起吃苦の倖褔 2024-08-10 17:49:21

将其放在 /etc/crontab 中并在用户字段中指定 apache 而不是 root

place it in /etc/crontab and specify apache instead of root in the user field

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文