LinuxSir.cn,穿越时空的Linuxsir!

 找回密码
 注册
搜索
热搜: shell linux mysql
查看: 1813|回复: 21

请教,如何判断是否在X-windows下

[复制链接]
发表于 2006-2-27 09:14:41 | 显示全部楼层 |阅读模式
请问如何判断是否在X-windows下?如果是判断进程的话,哪个进程最保险?
发表于 2006-2-27 10:30:08 | 显示全部楼层
I would just do
  1. ps -aef|grep X
复制代码
, but the problem is if you ctrl-alt-F1 or go to other vts, you will still be able to grab X but you are not actually under X.
回复 支持 反对

使用道具 举报

 楼主| 发表于 2006-2-27 10:49:36 | 显示全部楼层
多谢版主~~~英文~~汗
回复 支持 反对

使用道具 举报

发表于 2006-2-27 11:18:40 | 显示全部楼层
pgrep X 也可以,如果有这个命令。
回复 支持 反对

使用道具 举报

发表于 2006-3-2 01:09:07 | 显示全部楼层
可以试试这个
不断查找父进程,存在 xinit 这个父进程便说明在 X 下

  1. #!/bin/bash

  2. ppid=$(awk '/^PPid/{print $2}' /proc/$$/status)
  3. ppname=$(awk '/^Name/{print $2}' /proc/$ppid/status)

  4. while [[ $ppname != "init" && $ppname != "xinit" ]]
  5. do
  6.     ppid=$(awk '/^PPid/{print $2}' /proc/$ppid/status)
  7.     ppname=$(awk '/^Name/{print $2}' /proc/$ppid/status)
  8. done

  9. [[ $ppname = "xinit" ]] && echo -e "You are under X\n" && exit
  10. echo -e "You are not under X\n"
复制代码
回复 支持 反对

使用道具 举报

发表于 2006-3-2 14:14:14 | 显示全部楼层
test x"$DISPLAY" = x""
回复 支持 反对

使用道具 举报

发表于 2006-3-2 14:23:17 | 显示全部楼层
但是 ssh 好像也会自动设置 DISPLAY 的。
回复 支持 反对

使用道具 举报

发表于 2006-3-3 00:07:34 | 显示全部楼层
Post by 终极幻想
但是 ssh 好像也会自动设置 DISPLAY 的。

不会,除非你配置了forward x11选项。不过检测DISPLAY的值确实不一定正确。
回复 支持 反对

使用道具 举报

发表于 2006-3-3 08:15:37 | 显示全部楼层
我家里的2台机器,是用 X11 forward。。呵呵。。
回复 支持 反对

使用道具 举报

发表于 2006-3-4 00:27:45 | 显示全部楼层
Post by yongjian
I would just do
  1. ps -aef|grep X
复制代码
, but the problem is if you ctrl-alt-F1 or go to other vts, you will still be able to grab X but you are not actually under X.



vts 是 指终端virtual terminal s..... ?

谢谢
回复 支持 反对

使用道具 举报

您需要登录后才可以回帖 登录 | 注册

本版积分规则

快速回复 返回顶部 返回列表