Linux笔记

# 如何在shell脚本中获取自己的绝对路径?

basepath=$(cd `dirname $0`; pwd)

如果脚本文件是软连接,则

basepath=`dirname $(readlink $0)`

 

# 如何通过pid获取某个进程的stdout?

cat /proc/$pid/fd/1

 

# 如何用程序启动一个进程并获取它的标准输出?

用popen。

#include <stdio.h>
#include <string.h>

char buf[1024];
memset(buf, 0, sizeof(buf));
FILE *fp = popen("alsaplayer --status", "r");
fread(buf, sizeof(char), sizeof(buf) - 1, fp);
pclose(fp);
View Code

 

# 在cgi程序中使用system()出现的权限问题的解决

apache web server给cgi程序分配的用户权限较低,在cgi程序中使用system()函数时会返回256错误。

解决方法:

假设要在system中启动alsaplayer,则需首先在shell中执行

sudo chmod +s /usr/bin/alsaplayer

然后在cgi程序中执行

system("alsaplayer");

就能成功了。

 

# 当宿主机网络发生变化时,如果linux虚拟机的网络没有自动随着变化,则需要在linux虚拟机里执行:

sudo

ifconfig eth0 down
ifconfig eth0 up
dhclient eth0

 

 

posted @ 2014-04-25 11:28  One Leaf  阅读(266)  评论(0编辑  收藏  举报