使用更多用户执行脚本
我正在编写一个 bash 脚本,它调用许多其他脚本。
必须以 user_1 身份执行多个脚本,但必须以 user_2 身份执行多个脚本。
应该严格按照顺序调用脚本。我以 user_1 身份启动脚本,并多次使用 su 成为 user_2。这些时候 su 需要密码,所以我必须多次重新输入密码。 我想避免这种情况,但 su 没有密码参数。
sudo 和 Expect 未安装。
I'm writing a bash script which calls a lot of other scripts.
Several scripts have to be executed as user_1 but several ones as user_2.
The scripts should be called in strict sequence. I start my script as user_1 and use su many times to become user_2. These times su requires a password so I have to retype it many times.
I'd like to avoid that, but su has no password parameter.
sudo and expect are not installed.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(4)
为 user_1 和 user_2 创建一个分支(使用“&”)。他们应该使用“锁定文件”(信号量)进行通信(类似于客户端-服务器)。例如,user_1 正在做某事,而 user_2 检查 while ...sleep 循环中是否存在“锁定文件”
也许这很愚蠢,但您可以使用
ssh user@localhost
结束键Make a fork (using '&') for user_1 and for user_2. They should communicate (similar as client-serwer) using 'lock file' (semaphore). For example user_1 doing something and user_2 checking is 'lock file' exist in while ...sleep loop
Maybe that is stupid, but you can use
ssh user@localhost
end keys如果你碰巧有 zsh,我认为你可以轻松地做到这一点 - 类似:
user1_script:
如果你遇到困难,并且有 zsh,无论如何它都值得一试。
If you happen to have zsh, I think you can do this easily - something like:
user1_script:
If you're stuck, and have zsh, it's worth a try anyway.
你可以以 root 身份运行。听起来有风险吗?如果您小心并在每个命令前面加上 su 并添加用户名,则不会,如下所示:
su -c 'script1' user_1 && su -c 'script2' user_2
这将(假设您以 root 身份启动)在运行 script1 之前将用户更改为 user_1,然后返回 root,然后更改为 user_2 来运行 script2.. 所有这些都不需要输入多个密码。
You could run as root. Sounds risky? Not if you are careful and precede each command with su and add the username like so:
su -c 'script1' user_1 && su -c 'script2' user_2
This will (assuming you start as root) change the user to user_1 before running script1 then back to root then change to user_2 to run script2.. all without asking for multiple passwords.
我已经使用ssh解决了这个问题。我已经为 user_1 生成了身份验证密钥并发布给 user2。不再需要密码。
I have solved the problem using ssh. I have generated the authentication key for user_1 and published to user2. Password is not needed any more.