Skip to main content
 首页 » 编程设计

linux之编写运行其他用户拥有的另一个 shell 脚本的 shell 脚本

2023年11月22日121jackei

我已经在/opt/tomcat6 中安装了 tomcat,并在 ~/tomcatHost 目录中创建了一个实例。/opt/tomcat6 由用户 tomcat 和组 tomcat 所有。 Unix "ls -l"产生以下 -

drwxr-xr-x 9 tomcat tomcat 4096 Dec 15 03:37 tomcat6

在这种情况下,要在 ~/tomcatHost 中运行 tomcat 实例,我需要执行以下步骤 -

  • 将 conf、webapps、日志从/opt/tomcat6 复制到 ~/tomcatHost。在 ~tomcatHost/conf 中对 server.xml 进行一些更改(例如,更改“连接器”元素中的端口号)
  • 通过 chmod 为所有用户授予对 ~/tocatHost 的执行权限 -

$ chmod -R 777 tomcatHost

  • 并运行以下命令 -

$ export CATALINA_BASE=/home/guestUser/tomcatHost<br/> $ su tomcat<br/> $ cd /opt/tomcat6/bin<br/> $ sh startup.sh

现在 tomcat 在 server.xml 中配置的端口启动。我正在尝试编写一个 shell 脚本来完成上面执行的任务。我是 shell 脚本的新手,无法解决这个问题。我认为存在所有权问题,谁能告诉我如何处理 shell 脚本中的所有权问题?或者,我如何编写脚本来从其他用户运行“startup.sh”(由 tomcat 用户拥有)。你能为此推荐任何好的教程吗?

请您参考如下方法:

我已经找到了解决方案。可能这不是最好的解决方案,但效果很好。我在名为“start.sh”的文件中编写了以下代码(基于 this.)-

export CATALINA_BASE=/home/razib/tomcat_host   
echo "CATALINA_BASE is set..."   
echo "Password for 'tomcat' - "   
su -c "sh /opt/tomcat6/bin/startup.sh" -s /bin/sh tomcat 

然后从终端运行“start.sh”脚本