[英]export shell function to su as a user with ksh default shell
I have a situation where only root can mailx, and only ops can restart the process. 我有一种情况,只有root可以mailx,只有ops可以重新启动该过程。 I want to make an automated script that both restarts the process and sends an email about doing so.
我想制作一个自动化脚本,该脚本既可以重新启动该过程,又可以发送一封有关此过程的电子邮件。
When I try this using a function the function is "not found". 当我使用功能尝试此功能时,“找不到”功能。
I had something like: 我有类似的东西:
#!/usr/bin/bash
function restartprocess {
/usr/bin/processcontrol.sh start
}
export -f restartprocess
su - ops -c "restartprocess"
mailx -s "process restarted" myemail.mydomain.com < emailmessage.txt
exit 0
It told me that the function was not found. 它告诉我找不到该功能。 After some troubleshooting, it turned out that the ops user's default shell is ksh.
经过一些故障排除后,事实证明ops用户的默认shell是ksh。
I tried changing the script to run in ksh, and changing "export -f" to "typeset -xf", and still the function was not found. 我尝试更改脚本以在ksh中运行,并将“ export -f”更改为“ typeset -xf”,但仍然找不到该功能。 Like: ksh: exportfunction not found
像:ksh:找不到exportfunction
I finally gave up and just called the script (that was in the function directly) and that worked. 我最终放弃了,只是调用了脚本(直接在函数中),并且该脚本可以正常工作。 It was like:
这就像是:
su - ops -c "/usr/bin/processcontrol.sh start"
(This is all of course a simplification of the real script). (这当然是对真实脚本的简化)。
Given that user ops has default shell is ksh and I can't change that or modify sudoers, is there a way to export a function such that I can su as ops (and I need to run ops's profile) and execute that function? 鉴于用户ops的默认外壳程序是ksh,并且我不能更改它或修改sudoers,是否有一种方法可以导出一个函数,使我可以将su作为ops(并且我需要运行ops的配置文件)并执行该函数?
I made sure ops user had permission to the directory of the script I wanted it to execute, and permission to run that script. 我确定ops用户具有要执行的脚本目录的权限,并且具有运行该脚本的权限。
Any education about this would be appreciated! 任何对此的教育将不胜感激!
There are many restrictions for exporting functions, especially combined with su - ...
with different accounts and different shells. 导出功能有很多限制,特别是与具有不同帐户和不同外壳的
su - ...
结合使用时。
Instead, turn your script inside out and put all of the command that is to be run inside a function in the calling shell. 而是将您的脚本内翻,然后将要运行的所有命令放入调用外壳程序的函数中。
Something like: (Both bash and ksh) 类似于:(bash和ksh)
#!/usr/bin/bash
function restartprocess {
/bin/su - ops -c "/usr/bin/processcontrol.sh start"
}
if restartprocess; then
mailx -s "process restarted" \
myemail@mydomain.com < emailmessage.txt
fi
exit 0
This will hide all of the /bin/su
processing inside the restartprocess
function, and can be expanded at will. 这会将所有
/bin/su
处理隐藏在restartprocess
函数中,并且可以随意扩展。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.