我正在编写一个脚本来检查安装包之前的一些事情。我需要检查包含$ PWD的文件系统有多少可用空间。我在RHEL 7.0上编写并测试了脚本。我使用df -m $PWD|awk {'print $4'}
来获取可用空间。这适用于RHEL 7.0。但是在RHEL 6.4和7.1中,这不会返回MB中的可用空间,而是返回文件系统上的可用%可用空间。在视觉上他们看起来一样。我看到df
命令的版本与df --version
不同。该脚本可用于各种RHEL系统。对此可以起到什么样的作用呢?
答案 0 :(得分:3)
将#include <sys/types.h>
#include <sys/stat.h>
#include <fcntl.h>
#include <stdio.h>
#include <stdlib.h>
#include <unistd.h>
#include <sys/procfs.h>
#include <sys/fcntl.h>
#include <pwd.h>
char *getWaitChannel(int pid);
char *getPath(int pid);
char *getUserName(int uid);
int getBytes(int pid);
int main(int argc, char *argv[])
{
long x;
if (argc < 2){
//error message
}
x = strtol(argv[1], NULL, 10);
printf("Good 1\n");
get_info(x, argc, argv);
}
int get_info(pid_t pid)
{
char path[40], line[100], *p, stateChar[100], Name[100];
FILE* statusf;
char buf[100];
printf("This is pid %d\n", pid);
int uid, vM;
snprintf(path, 40, "/proc/%d/status", pid);
statusf = fopen(path, "r");
if(statusf == NULL)
return -1;
while(fgets(buf,sizeof buf, statusf) != NULL){
sscanf(buf, "State: %s", stateChar);
sscanf(buf, "Name: %s", Name);
sscanf(buf, "Uid: %d", &uid);
sscanf(buf, "VmPeak: %d", &vM);
}
char *channel = getWaitChannel(pid);
char *full_path = getPath(pid);
char *user = getUserName(uid);
int b = getBytes(pid);
printf("State: %s\n", stateChar);
printf("Name: %s\n", Name);
printf("Uid: %d\n", uid);
printf("Username: %s\n", user);
printf("Max Virtual Memory: %d\n", vM);
printf("Full Path: %s\n", full_path);
printf("Bytes written to storage layer: %d\n", b);
printf("Waiting channel: %s\n", channel);
}
char *getUserName(int uid)
{
struct passwd *pw = getpwuid(uid);
if (pw)
{
return pw->pw_name;
}
return "";
}
int getBytes(int pid)
{
FILE* statusf2;
char path[40];
char buf2[100];
int storage_bytes;
snprintf(path, 40, "/proc/%d/io", pid);
statusf2 = fopen(path, "r");
if(statusf2 == NULL)
return -1;
while(fgets(buf2,sizeof buf2, statusf2) != NULL){
sscanf(buf2, "write_bytes: %d", &storage_bytes);
return storage_bytes;
}
}
char *getPath(int pid)
{
FILE* statusf3;
char path[40];
char buf3[100];
char *fullpath;
snprintf(path, 40, "/proc/%d/cmdline", pid);
statusf3 = fopen(path, "r");
if(statusf3 == NULL)
return "";
while(fgets(buf3,sizeof buf3, statusf3) != NULL){
sscanf(buf3,"/ %s", fullpath);
return fullpath;
}
}
char *getWaitChannel(int pid)
{
FILE* statusf4;
char path[40];
char buf4[100];
char *channel;
snprintf(path, 40, "/proc/%d/stack", pid);
statusf4 = fopen(path, "r");
if(statusf4 == NULL)
return "";
while(fgets(buf4,sizeof buf4, statusf4) != NULL){
sscanf(buf4,"[<c0227f4e>] %s", channel);
return channel;
}
}
与-P
命令
-P, - 可行性
使用POSIX输出格式
df