Linux对话框脚本中如何隐藏用户侧的执行进程?
解决TUI密码修改脚本弹窗问题
我负责的密码处理项目里,有个运行TUI的对话框脚本,供管理员修改密码。现在遇到个问题:管理员点击“changepass”选项后,执行密码修改命令时,终端窗口会短暂弹出。试过加&和> /dev/null 2>&1都没用,下面是用户输入旧密码、新密码和确认密码后需要无弹窗运行的脚本:
expect <<EOF spawn wdpass -c expect "Insert the OLD password" send "$curr_password\r" expect "Insert the NEW password" send "$new_password\r" expect "Confirm the NEW password" send "$confirm_password\r" expect eof EOF
解决方案
方法1:用setsid让进程脱离终端会话
setsid会创建新的会话,让wdpass不关联当前终端,避免弹窗。修改后的脚本如下:
expect <<EOF spawn setsid wdpass -c expect "Insert the OLD password" send "$curr_password\r" expect "Insert the NEW password" send "$new_password\r" expect "Confirm the NEW password" send "$confirm_password\r" expect eof EOF
方法2:用nohup后台运行并完全隔离输出
把整个expect脚本用nohup包裹,同时重定向所有输出到/dev/null,彻底脱离前台终端:
nohup expect <<EOF > /dev/null 2>&1 & spawn wdpass -c expect "Insert the OLD password" send "$curr_password\r" expect "Insert the NEW password" send "$new_password\r" expect "Confirm the NEW password" send "$confirm_password\r" expect eof EOF
方法3:用screen在后台虚拟终端运行
screen会创建一个后台虚拟终端,让TUI程序在无前台窗口的环境下执行:
screen -d -m expect <<EOF spawn wdpass -c expect "Insert the OLD password" send "$curr_password\r" expect "Insert the NEW password" send "$new_password\r" expect "Confirm the NEW password" send "$confirm_password\r" expect eof EOF
为什么之前的方法无效?
wdpass是TUI程序,会主动请求关联终端,单纯的后台运行(&)或输出重定向无法阻止它创建终端窗口。上述方法通过会话隔离、后台虚拟终端等方式,让程序在无前台终端的环境下运行,从而避免弹窗。
内容的提问来源于stack exchange,提问作者Axel
相关产品推荐
相关产品推荐

