一、chroot方式

[root@localhost ~]# chroot --userspec "nginx:nginx" "/" sh -c "whoami"
nginx
[root@localhost ~]# chroot --userspec "kibana:kibana" "/" sh -c "whoami"
kibana

以kibana用户启动kibana示例:

chroot --userspec "kibana:kibana" "/" sh -c "/usr/share/kibana/bin/kibana -c /etc/kibana/kibana.yml" >> /var/log/kibana/kibana.stdout 2>> /var/log/kibana/kibana.stderr &
  # Run the program!

  chroot --userspec "$user":"$group" "$chroot" sh -c "

    cd \"$chdir\"
exec \"$program\" $args
" >> /var/log/kibana/kibana.stdout 2>> /var/log/kibana/kibana.stderr &

二、daemon函数形式

参考https://stackoverflow.com/questions/17956151/how-to-run-a-command-as-a-specific-user-in-an-init-script

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.

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

If that is too heavy-handed for what you need, there is runuser (see man runuser for full info; some versions may need -u prior to the username):

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

elasticsearch启动脚本示例:

#
# Source function library.
#
if [ -f /etc/rc.d/init.d/functions ]; then
. /etc/rc.d/init.d/functions
fi cd $ES_HOME
echo -n $"Starting $prog: "
# if not running, start it up here, usually something like "daemon $exec"
daemon --user elasticsearch --pidfile $pidfile $exec -p $pidfile -d
retval=$?
echo
[ $retval -eq 0 ] && touch $lockfile
return $retval

linux里面以指定用户运行命令的更多相关文章

  1. Linux下以特定用户运行命令

    方法汇总: 1.su 2.sudo 3.runuser 比较常用的方式:su 示例:su - root -s /bin/sh -c "/usr/local/nginx/sbin/nginx& ...

  2. Linux指定用户运行程序

    参考:http://blog.useasp.net/archive/2015/07/29/run-command-as-different-user-on-linux.aspx 在实际中,我们有时候想 ...

  3. linux下在root用户登陆状态下,以指定用户运行脚本程序实现方式

    aaarticlea/png;base64,iVBORw0KGgoAAAANSUhEUgAAAMcAAABKCAIAAACASdeXAAAEoUlEQVR4nO2dy7WlIBBFTYIoSIIkmD ...

  4. Linux下非root用户运行Tomcat

    PS:Linux下使用非root用户运行tomcat的原因 由于项目需求,也由于root用户启动tomcat有一个严重的问题,那就是tomcat具有root权限. 这意味着你的任何一个页面脚本(htm ...

  5. Linux 和 windows下查看运行命令的位置

    经常遇到要查看某个命令的运行文件在哪儿! 比如说vue cli,经常使用vue命令创建项目,如果你对nodejs的全局包安装目录了解可能一下就找到了, 蛋疼的是不一定每个命令都是nodejs下的,有可 ...

  6. Linux下在root权限下临时使用其它用户运行命令

    一.简述 当我们在使用Linux时,经常需要在root权限下执行某些命令,或者在/etc/rc.d/rc.local中写一些角本.而如果某些角本必须使用非root用户时,直接su是不行的,比如Elas ...

  7. [Linux] cronjob指定用户运行脚本,并按日期区分输出日志

    废话不多说,直接上代码,在root的cronjob,指定nginx用户跑cronjob */1 * * * * su nginx -c "/usr/local/scripts/goods.s ...

  8. Linux 脚本内容指定用户执行

    #!/bin/bash set -x ## 因为这些变量在下面要用,所以要写在最上面, ## 如果直接写在下面,则变量获取不到,并且下面的 $ 标识的都要用 引号引起来,否则这些参数接收不到 tarf ...

  9. C# Windows服务以指定用户运行

    参考一下 https://bbs.csdn.net/topics/330151879 服务程序以Local System安装运行没问题,但用这个账户运行的服务无法访问局域网共享资源,比较麻烦,所以想指 ...

随机推荐

  1. flex兼容问题

    display:flex作为C3的新属性,还是有的浏览器不支持的,那下面我们就来说一下他的兼容写法 .box{ display: -webkit-box; /* 老版本语法: Safari, iOS, ...

  2. Scrapy爬虫框架下执行爬虫的方法

    在使用Scrapy框架进行爬虫时,执行爬虫文件的方法是 scrapy crawl xxx ,其中 xxx 是爬虫文件名. 但是,当我们在建立了多个文件时,使用上面的命令时会比较繁琐麻烦,我们就可以使用 ...

  3. Linux函数的使用方法

    [root@a ~]#cat fun.txt #定义函数库文件,方便在别的地方使用 addnum1() { echo $[$1+$2] } addnum2(){ echo $[$1*$2] } del ...

  4. python-判断一个字符串是不是小数

    ''' 1.判断小数点个数是不是1 count 2.分割,判断小数点右边是不是整数 3.判断小数点左边 1.是整数is_digit 2.如果是负整数,按照'-'符号右边的是不会整数 ''' def i ...

  5. 《计算机程式设计》Week4 课堂笔记

    本笔记记录自 Coursera课程 <计算机程式设计> 台湾大学 刘邦锋老师 Week4 Functions 4-1 System Function 函数主要分为两大类系统定义函数与使用者 ...

  6. Postman Tests脚本的使用

    直接在Tests中写js代码断言结果,Test Result展示运行结果,简单方便. 示例脚本: var jsonData = JSON.parse(responseBody); var num = ...

  7. Java 之 回调方法

    打个比方,我们点外卖都有到达提醒的服务,顾客自己选择提醒的方式.可以是电话提醒.短信提醒.也可以敲门提醒,这里,“提醒”这个行为是美团或饿了么这样的平台提供的,相当于库函数,但是提醒的方式是由顾客决定 ...

  8. 应用安全-软件安全-漏洞CVE整理

    jira ssrf CVE-2019-8451 url = url + '/plugins/servlet/gadgets/makeRequest?url=' + host + '@www.baidu ...

  9. git --> 工作使用流程

    [git]------git开发过程中的使用流程------[WangQi]   001.创建仓库 002.新建项目 003.初始化仓库  这一步不需要做 git init : 文件夹中会多出一个隐藏 ...

  10. Java实验报告(一)&&第三周学习总结

    实验报告(一) 1. 打印输出所有的“水仙花数”,所谓“水仙花数”是指一个3位数,其中各位数字立方和等于该数本身.例如,153是一个“水仙花数”. 源代码: public class Main { p ...