通过以下方式从提示中轻松获取后台进程ID:
$ my_daemon &
$ echo $!
Run Code Online (Sandbox Code Playgroud)
但是,如果我想以不同的用户身份运行它,例如:
su - joe -c "/path/to/my_daemon &;"
Run Code Online (Sandbox Code Playgroud)
现在我如何捕获my_daemon的PID?
我需要我的应用程序以su编程方式执行一些命令(手机已植根).
使用adb完成后,命令可以正常工作.
例如:
su -c "mkdir /sdcard/testdir"在/ sdcard中创建一个名为"testdir"的目录.
我打电话的时候:
p = Runtime.getRuntime().exec("su -c \"mkdir /sdcard/testdir\"");
p.waitFor();
Run Code Online (Sandbox Code Playgroud)
它只是继续前进,没有任何变化.
我试着阅读输入:
DataInputStream dis = new DataInputStream(p.getInputStream());
while((temp = dis.readLine())!=null)
Log.d(ctx.TAG,"shell:"+temp);
Run Code Online (Sandbox Code Playgroud)
但它没有任何报告(循环执行0次迭代).
有没有人曾经遇到过这个问题?怎么解决?毫无疑问,非su命令可以用这种方法以编程方式工作.
注意:我举了mkdir一个例子(我知道它不一定需要su).我需要执行很多不同的命令su
谢谢!
编辑:当我以su -c "id"编程方式调用时,输出uid = 0.
我有这款"老式"联想A2107平板电脑,这是我多年前的根源(使用http://androidforums.com/threads/a2107-root-mods-and-rom-discussion.661261/下载,运行其root.bat从我的GNU/Linux机器手动编写脚本)并且工作正常.
然后我将这款平板电脑传递给了我的女儿几年,现在我把它拿回来了.
/ system/bin/su仍然存在,仍然是相同的日期和大小,仍然模式"rwsr-sr-x",仍然是相同的固件(Android 4.0.3)所以看起来都很好,但是当我尝试从它运行它一个shell,它只是告诉我"Permission denied".
知道可能会发生什么吗?我可以尝试做些什么来追踪它?
我希望能够编写一个su_mt_user(当前)看起来像这样的函数:
su_mt_user() {
su someuser -c "$*"
}
Run Code Online (Sandbox Code Playgroud)
目标是能够像这样使用它:
su_mt_user mt-auth --stuff etc
Run Code Online (Sandbox Code Playgroud)
哪个会mt-auth --stuff etc以用户身份运行该命令someuser.当前版本适用于此特定命令,但对于以下命令失败:
some_filename_with_spaces="/home/user/hello there i like spaces in filenames.txt"
su_mt_user stat "$some_filename_with_spaces"
Run Code Online (Sandbox Code Playgroud)
此操作失败,并出现以下错误:
stat: cannot stat '/home/user/hello': No such file or directory
stat: cannot stat 'there': No such file or directory
stat: cannot stat 'i': No such file or directory
stat: cannot stat 'like': No such file or directory
stat: cannot stat 'spaces': No such file or directory
stat: cannot …Run Code Online (Sandbox Code Playgroud) 我在做什么:
从手机终端登录时,我当前是用户 10157,一切正常:
$ id
uid=10157(10157) gid=10157(10157) 组=10157(10157),1015(1015),1023(1023),1028(1028),3003(3003)
当我通过 ssh 从 PC 连接到手机时(我在手机上使用 DigiSSHd),它以普通用户 10282 的身份登录,一切正常:
$ id
uid=10282 gid=10282 组=1015(1015),1023(1023),1028(1028),3003(3003)
Emacs 运行良好等。但是,这样我无法通过 emacsclient 连接到在用户 10157 下运行的 emacs 进程。这是可取的,因为我不想启动两个 emacs 进程,因为我想继续处理那些我已在用户 10157 下在 emacs 中打开。
所以:
$ su - 10157
很好,我可以运行 emacs 等。但是,我无法访问网络。
$ ping -c1 google.com
You must have internet permissions to use ping. Aborting.
$ id
uid=10157(10157) gid=10157(10157) groups=10157(10157)
Run Code Online (Sandbox Code Playgroud)
因此,除了其他组之外,我不再属于 Internet 访问所必需的 3003 组。
为什么这个组信息会被删除,我该如何解决这个问题,以便在 ssh 下以该用户身份 su 时我可以继续访问网络?
我想执行一些shell commands并获取输出TextView.该命令可能具有连续输出,如ping或logcat.此外,TextView应该在实时添加命令输出时自动滚动.为了做到这一点,我做到了这一点:
package com.example.rootapp;
import java.io.DataOutputStream;
import java.io.InputStream;
import android.app.Activity;
import android.os.Bundle;
import android.text.method.ScrollingMovementMethod;
import android.widget.TextView;
public class MainActivity extends Activity {
TextView tv;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
String[] cmdArray={"logcat -b radio"};
try {
runAsRoot(cmdArray);
} catch (Exception e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
public void runAsRoot(String[] cmds) throws Exception {
tv=(TextView)findViewById(R.id.cmdOp);
tv.setMovementMethod(new ScrollingMovementMethod());
Process p = Runtime.getRuntime().exec("su");
DataOutputStream os = new DataOutputStream(p.getOutputStream());
InputStream …Run Code Online (Sandbox Code Playgroud) 我有一个 arch linux 设置并通过 arch 用户存储库 ( yaourt -S neo4j) 安装了 neo4j,并且我能够正常运行 Web 控制台(sudo neo4j console具有看似正常的输出和完整功能),但是当尝试启动服务器 ( sudo neo4j start) 时,我遇到如下错误信息:
/usr/share/neo4j/bin/utils: line 345: [: -lt: unary operator expected\nUsing additional JVM arguments: -server -XX:+DisableExplicitGC -Dorg.neo4j.server.properties=/etc/neo4j/neo4j-server.properties -Djava.util.logging.config.file=/etc/neo4j/logging.properties -Dlog4j.configuration=file:/etc/neo4j/log4j.properties -XX:+UseConcMarkSweepGC -XX:+CMSClassUnloadingEnabled\nStarting Neo4j Server...cat: /run/neo4j/neo4j-service.pid: No such file or directory\nprocess []... waiting for server to be ready. Failed to start within 120 seconds.\nNeo4j Server may have failed to start, please check the logs.\nrm: cannot remove \xe2\x80\x98/run/neo4j/neo4j-service.pid\xe2\x80\x99: No such …Run Code Online (Sandbox Code Playgroud) 我希望能够在脚本中间切换用户。这是一种尝试:
su - User << EOF
/usr/bin/ruby -e "$(curl -fsSL https://raw.githubusercontent.com/Homebrew/install/master/install)" </dev/null
EOF
Run Code Online (Sandbox Code Playgroud)
我的目标是执行 EOF 分隔符之间的代码,就像我实际以用户身份登录一样。
中间一行应该安装 Homebrew。如果我以用户身份登录并单独运行中间行,它就可以正常安装。但运行上面的完整脚本给我带来了问题:
-e:5: unknown regexp options - lcal
-e:6: unknown regexp options - lcal
-e:8: unknown regexp options - Cach
-e:9: syntax error, unexpected tLABEL
BREW_REPO = https://github.com/Homebrew/brew.freeze
^
-e:9: unknown regexp options - gthb
-e:10: syntax error, unexpected tLABEL
CORE_TAP_REPO = https://github.com/Homebrew/homebrew-core.freeze
^
-e:10: unknown regexp options - gthb
-e:32: syntax error, unexpected end-of-input, expecting keyword_end
-bash: line 34: end: command not found …Run Code Online (Sandbox Code Playgroud) 我正在尝试在 android 7.1 上访问/proc/{PID}(或运行 ps)我的应用程序中的另一个进程。由于此补丁,我无法访问其他 PID 上的任何数据:
https://android-review.googlesource.com/c/platform/system/core/+/181345
我以为通过运行 su,我将能够访问 /proc,但我不能。即使我将用户切换到所述应用程序的用户,我也可以使用 ADB shell 访问 /proc。
groups从 adb shell运行:
> generic:/ # groups
uid=0(root) gid=0(root) groups=0(root)input log adb sdcard_rw sdcard_r net_bt_admin net_bt inet net_bw_stats readproc
Run Code Online (Sandbox Code Playgroud)
groups切换到应用程序用户后从 adb运行:
generic:/ # su u0_a56
generic:/ $ groups
uid=10056(u0_a56) gid=10056(u0_a56) groups=10056(u0_a56)input log adb sdcard_rw sdcard_r net_bt_admin net_bt inet net_bw_stats readproc
Run Code Online (Sandbox Code Playgroud)
groups从应用程序内部运行:
uid=10056(u0_a56) gid=10056(u0_a56) groups=10056(u0_a56)everybody all_a56
Run Code Online (Sandbox Code Playgroud)
groups运行 su 后从应用程序内部运行:
uid=0(root) gid=0(root) groups=0(root)everybody all_a56
Run Code Online (Sandbox Code Playgroud)
为什么 su 更改 gid 但不更改我所在的实际组?