简体   繁体   English

如何在Ubuntu 12.04上使用特定用户在启动时运行bash脚本(稳定)

[英]how to run a bash script at startup with a specific user on Ubuntu 12.04 (stable)

Being fairly new to the Linux environment, and not having local resources to inquire on, I would like to ask what is the preferred method of starting a process at startup as a specific user on a Ubuntu 12.04 system. 作为Linux环境的新手,并且没有可查询的本地资源,我想问一问在Ubuntu 12.04系统上以特定用户身份启动时启动进程的首选方法是什么。 The reasoning for such a setup is that this machine(s) will be hosting an Input/Output Controller (IOC) in an industrial setting. 进行这种设置的原因是,该机器将在工业环境中托管输入/输出控制器(IOC)。 If the machine fails or restarts, this process must boot automatically..... everytime. 如果机器出现故障或重新启动,则此过程必须每次都自动启动。

My internet searches have provided two such area's to perform this task: 我的互联网搜索提供了两个此类区域来执行此任务:

/etc/rc.local
/etc/init.d/

I ask for the specific advantages and disadvantages of each approach. 我要求每种方法的特定优点和缺点。 I'll add that some of these machines are clients and some are servers, but all need to run an IOC, and preferably in the same manner. 我要补充一点,这些机器中的一些是客户端,有些是服务器,但是所有这些都需要运行IOC,最好以相同的方式运行。

Within what ever method above is deemed to be the most appropriate, a bash shell script must be run as my specified user. 在以上认为最合适的方法中,必须以我的指定用户身份运行bash shell脚本。 It is my understanding all start up process are owned by root. 据我了解,所有启动过程均由root拥有。 So I question if this is the best practice: 所以我怀疑这是否是最佳实践:

sudo -u <user> start_ioc.sh

If this is the case, then I believe it is required to create a file under: 如果是这种情况,那么我认为需要在以下位置创建文件:

/etc/sudoers.d/

Using: 使用:

sudo visudo -f <filename>

Where within this file you assign the appropriate rights and paths to the user. 您可以在此文件中的适当位置为用户分配权限和路径。 Most of my searches has shown this as the proper format: 我的大部分搜索都将其显示为正确的格式:

<user or group> <host or IP>=(<user or group to run as>)NOPASSWD:<list of comma separated applications>
root ALL=(user)NOPASSWD:/usr/bin/start_ioc.sh

So for final additional information, the ultimate reason for this approach, which may also be flawed logic, is that the IOC process needs to have access to a network attached server (NAS). 因此,对于最终的附加信息,此方法的最终原因(可能还存在逻辑缺陷)是IOC进程需要访问网络连接服务器(NAS)。 Allowing root access to the NAS is I believe a no-no, where the user can have the appropriate permissions assigned. 我相信允许root访问NAS是不可以的,用户可以在其中分配适当的权限。

This may not be the best answer, but it is how I decided to complete this task: 这可能不是最佳答案,但这是我决定完成此任务的方式:

Exactly as this post here: 与此帖子完全一样:

how to run script as another user without password 如何以没有密码的其他用户身份运行脚本

I did use rc.local to initiate the process at startup. 我确实使用rc.local在启动时启动了进程。 It seems to be working quite well. 它似乎运行得很好。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM