shell (check return of each line)and sudoer

shell result from cmdline

echo $?

if 0 then success ;else failure

(shell 执行每部返回值,rm -rf 错误,打包不能覆盖)

解决sudo: sorry, you must
have a tty to run sudo

前几天遇到一个问题,在一个终端中调用另一个shell,始终是无法执行的,后来捕捉到报错信息为sudo:
sorry, you must have a tty to run sudo,后来,在网上了解到可以如下解决:

1. 编辑 /etc/sudoers

1)Defaults  
 requiretty,修改为 #Defaults    requiretty,表示不需要控制终端。

2)Defaults  
 requiretty,修改为 Defaults:nobody !requiretty,表示仅 nobody 用户不需要控制终端。

如果修改为
Defaults:%nobody !requiretty,表示仅 nobody 组不需要控制终端。

其实只要注释掉)Defaults  
 requiretty
那个就可以了。表示在执行的时候不打开终端。但是,有的shell必须要有终端才可以执行。这样显然是不行的。后来,又找到一片文章才搞定。下面为抄录的,仅为记录以后使用。

有些程序/脚本可能在没有控制终端的环境下上执行(如系统启动服务时, Daemon,

或者是setsid启动的程序等)
但可能这个程序需要控制终端, 这这么办呢?

如我们的例子: 我们在linux启动时启动一个服务,
可是中间有个程序(旧的systemtap)使用了sudo

但sudo需要控制终端(当然可以通过修改sudo的配置文件,
但这样很对用户不友好啊)

例子: (setsid 启动的程序会失去控制终端)

# setsid sudo ls

sudo: sorry, you must have
a tty to run sudo

(如果你的系统没有输出这句话,
那就是你的系统的sudo配置文件允许sudo可以在这个,

请确保已经设置了Defaults  
 requiretty)

没有控制中断的时候, 打开控制终端是这样的效果:

# setsid head -c 0 /dev/tty

head: cannot open
`/dev/tty‘ for reading: No such device or address

为了解决这个问题,
所以应该使用能创建(伪)控制终端的程序来启动你的程序如: script, expect

如:

# setsid script -c "sudo
ls" /dev/null

或:

# setsid expect -c ‘spawn
sudo ls; expect‘

#打开控制终端成功:

# setsid script -c ‘head -c
0 /dev/tty‘ /dev/null

# setsid expect -c ‘spawn
head -c 0 /dev/tty; expect‘

不过 setsid 跟 script
组合使用有着奇怪的bug, 我这段时间非常的depression,

这样对script, expect来说,
都是大材小用了

时间: 2024-08-25 00:30:36

shell (check return of each line)and sudoer的相关文章

Shell脚本return、echo、printf

ehco和return  return  返回的是状态码,函数结束 return不返回函数返回值 可以在前面用 echo 返回函数返回值,return 返回指定函数退出状态码 echo  返回的是函数返回值,函数退出状态码是函数最后一条命令的退出状态码 test1.sh #!/bin/bash       function test(){     echo 200                                                                   

[POI 2018] Plan Metra

[题目链接] https://www.lydsy.com/JudgeOnline/problem.php?id=5100 [算法] 首先分两类考虑 : 1. 1 -> N的路径不经过其它节点 , 我们只需判断(d1i - d2i)的绝对值是否全部相等 2. 1 -> N的路径经过了其它节点 , 那么显然 , 1 -> N这条链的长度为min{ d1i + d2i } , 所有d1i + d2i等于链长的节点都在链上 , 将其余节点的d1i和d2i作差 , 即可O(1)判断出这个节点是挂在

shell脚本:lnmp等源码安装脚本

##脚本功能: # 源码安装dns.nginx.php.memcached.gonet.mysql,并做相关的配置 # ##脚本说明: # ##更新记录: # 1.增加dns.nginx的配置文件 # 2.优化mysql.nginx.dns等安装的部分函数 # 3.增加安装gonet服务的功能函数 -------------------------- #!/bin/bash # by colin on 2016-01-06 # revision on 2016-04-29 ###########

Shell Style Guide

Shell Style Guide Revision 1.26 Paul ArmstrongToo many more to mention Each style point has a summary for which additional information is available by toggling the accompanying arrow button that looks this way: ▽. You may toggle all summaries with th

shell脚本:日志切割与上传

脚本说明: 日志切割与上传类脚本的功能:脚本自身的运行日志记录与清理.日志切割与打包.ftp上传.传送失败自动重传.断电自动补传.清理超期旧打包等 -------------------- #!/bin/bash # Cut and upload aclog log # by colin # revision on 2016-06-15 ######################################## # 功能说明:该脚本运用于上传aclog日志 # # 使用说明: #+ ./a

shell脚本:mysql全备与binlog增量备份

脚本功能:mysql库备份脚本,全备与binlog日志备份 脚本可以根据指定的参数进行全备,其余时间备份binlog日志 --------------- #!/bin/bash # description:Trian Server backup databases # revision on 2016-02-18 # by colin # #################################### # 功能说明:该脚本运用于mysql每天备份与上传 #  # 使用说明: # ./m

shell学习总结之自定义函数

shell学习总结之自定义函数 Myfun (){ echo patams1 is $1 echo -n "now i is $i " ! [ "$i" ] && exit ; echo jj return '1' } myf=$(Myfun); echo myf Myfun 12 unset Myfun Myfun echo 'the end !'$myf 别人的 #! bin/bash # ----------------------------

java调用shell脚本,并且返回结果集

1 /** 2 * 运行shell脚本 3 * @param shell 需要运行的shell脚本 4 */ 5 public static void execShell(String shell){ 6 try { 7 Runtime rt = Runtime.getRuntime(); 8 rt.exec(shell); 9 } catch (Exception e) { 10 e.printStackTrace(); 11 } 12 } 13 14 15 /** 16 * 运行shell

MongoDB - Introduction of the mongo Shell

Introduction The mongo shell is an interactive JavaScript interface to MongoDB. You can use the mongo shell to query and update data as well as perform administrative operations. The mongo shell is a component of the MongoDB distributions. Once you h