如何在init脚本中以特定用户身份运行命令? [英] How to run a command as a specific user in an init script?

查看:990
本文介绍了如何在init脚本中以特定用户身份运行命令?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在编写一个初始化脚本,该脚本应该作为不同于root的用户执行单个命令。这是我目前的做法:

sudo -u username命令

I'm writing an init script which is supposed to execute a single command as a user different than root. This is how I'm doing it currently:
sudo -u username command

这通常在Ubuntu / Debian上正常工作,但是在RHEL上,命令挂起时执行的脚本。

有另一种方法运行命令作为另一个用户?

(请注意,我不能使用lsb init函数,因为它们在RHEL / Centos 5.x上不可用。)

This generally works as expected on Ubuntu/Debian, but on RHEL the script which is executed as the command hangs.
Is there another way to run the command as another user?
(Note that I can't use lsb init functions as they're not available on RHEL/Centos 5.x.)

推荐答案

在RHEL系统上, /etc/rc.d/init.d/functions 脚本旨在提供类似于你要什么。

On RHEL systems, the /etc/rc.d/init.d/functions script is intended to provide similar to what you want. If you source that at the top of your init script, all of it's functions become available.

提供帮助的特定函数是守护进程。如果你打算使用它来启动类似daemon的程序,一个简单的用法是:

The specific function provided to help with this is daemon. If you are intending to use it to start a daemon-like program, a simple usage would be:

daemon --user=username command

如果这对你所需要的太重了,有 runuser (详见 man runuser ):

If that is too heavy-handed for what you need, there is runuser (see man runuser for full info):

/sbin/runuser -u username -s /bin/bash -c "command(s) to run as user username"

这篇关于如何在init脚本中以特定用户身份运行命令?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
相关文章
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆