简体   繁体   English

当bash脚本通过Java代码运行时,如何在Linux中将用户工作设置为root用户

[英]How do you set user work as root in Linux, when the bash script runs via Java code

Suppose my bash file contains 假设我的bash文件包含

chmod -R 777 $backup_files/app/webroot/uploadedFiles/

It works fine when I run it through root permission using /permission.sh but when I try to run this bash script via java code like 当我使用/permission.sh通过root权限运行它时,它运行良好,但是当我尝试通过Java代码(例如,

        Runtime runtime = Runtime.getRuntime();
        Process process = runtime.exec("/permission.sh");
        printBufferedReaderOutputFromProcess(process);
        process.waitFor();

It gives me the following error: 它给了我以下错误:

chmod: changing permissions of `/home/app/webroot/uploadedFiles/A.jpg': Operation not permitted 

What can I fix it? 我该如何解决?

I suspect you haven't use sudo before, however an alternative to using the setuid on the script is to give the user the right to sudo the script as root. 我怀疑您以前没有使用过sudo,但是在脚本上使用setuid的另一种方法是赋予用户以root身份对sudo脚本进行操作的权利。 ie

sudo permisions.sh

This runs as root can can be run without asking a password. 以root用户身份运行,无需输入密码即可运行。 You have to set this up in sudoers.conf 您必须在sudoers.conf中进行设置

How are you running your Java code? 您如何运行Java代码? Is it stand-alone, or running in a web container like Tomcat? 它是独立的,还是在像Tomcat这样的Web容器中运行? If the later, it's probably running as a user that doesn't have permissions to the directory you're trying to change. 如果是更高版本,则它可能以没有您要更改目录权限的用户身份运行。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 Java程序在bash脚本中运行但不运行 - Java program runs but not in bash script 您如何制作没有UI的应用程序,所以当您打开它时,它将运行一些代码 - How do you make an app with no UI so when you open it, it runs some code Java执行运行守护程序脚本的Bash脚本 - Java Execute Bash Script that runs a daemonized script 通过here文档将输入参数传递给运行需要该输入的java类的bash脚本 - Passing input arguments via here document to bash script which runs java class where that input is needed 如何在Linux上使用plesk为基于服务的tomcat版本设置JAVA_OPTS? - How do you set JAVA_OPTS for a service based version of tomcat using plesk on linux? 如何像在我的代码中一样将方法相互连接,当用户按下 2 时,它将不得不去吃零食,但它会运行代码来吃饭 java - how to connect the methods to each other like in my code when the user press 2 it will have to go to snack but instead it runs the code to meals java 从Java执行时,Linux脚本无法正常工作 - Linux Script does not work properly when execute from Java 你如何让用户设置变量? - How do you have the user set the variable? 你如何找到Java中睡眠中断的根本原因? - How do you get to the root cause of a sleep interruption in Java? 如何通过子Java进程可访问的Bash设置系统属性 - How to set a system property via Bash accessible by child Java processes
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM