我正在尝试编写一个脚本来返回特定进程的 CPU 使用率(以 % 为单位)我需要使用 /proc/PID/stat 因为嵌入式系统上不存在 ps aux。
我试过这个:
#!/usr/bin/env bash
PID=$1
PREV_TIME=0
PREV_TOTAL=0
while true;do
TOTAL=$(grep '^cpu ' /proc/stat |awk '{sum=$2+$3+$4+$5+$6+$7+$8+$9+$10; print sum}')
sfile=`cat /proc/$PID/stat`
PROC_U_TIME=$(echo $sfile|awk '{print $14}')
PROC_S_TIME=$(echo $sfile|awk '{print $15}')
PROC_CU_TIME=$(echo $sfile|awk '{print $16}')
PROC_CS_TIME=$(echo $sfile|awk '{print $17}')
let "PROC_TIME=$PROC_U_TIME+$PROC_CU_TIME+$PROC_S_TIME+$PROC_CS_TIME"
CALC="scale=2 ;(($PROC_TIME-$PREV_TIME)/($TOTAL-$PREV_TOTAL)) *100"
USER=`bc <<< $CALC`
PREV_TIME="$PROC_TIME"
PREV_TOTAL="$TOTAL"
echo $USER
sleep 1
done
Run Code Online (Sandbox Code Playgroud)
但是,如果我将其与顶部进行比较,则不会给出正确的值。你们中的一些人知道我哪里出错了吗?
谢谢
我想使用 de java Google 电子表格 API 访问 Google 云端硬盘上的电子表格。以前我使用客户端登录方法来登录,但现在这种方法已被弃用。现在,我尝试使用服务帐户进行身份验证,而无需与用户交互,因为该工具在后台运行,我需要自动化。但我无法让它工作。这些是我现在的代码。
List<String> SCOPES_ARRAY = Arrays.asList(
"https://www.googleapis.com/auth/drive.file",
"https://www.googleapis.com/auth/userinfo.email",
"https://www.googleapis.com/auth/userinfo.profile",
"https://docs.google.com/feeds",
"https://spreadsheets.google.com/feeds");
credential = new GoogleCredential.Builder()
.setTransport(transport)
.setJsonFactory(jsonFactory)
.setServiceAccountId(
"xxxxxx@developer.gserviceaccount.com")
.setServiceAccountScopes(SCOPES_ARRAY)
.setServiceAccountPrivateKeyFromP12File(p12)
.setServiceAccountUser("me@gmail.com")
.build();
service = new SpreadsheetService("MonitorV3");
service.setOAuth2Credentials(credential);
SPREADSHEET_FEED_URL = new URL("https://spreadsheets.google.com/feeds/spreadsheets/private/full");
worksheetFeed = service.getFeed(spreadsheet.getWorksheetFeedUrl(),
WorksheetFeed.class);
Run Code Online (Sandbox Code Playgroud)
但在执行最后一行时我遇到了空指针异常。刷新令牌出了问题。有人看到我做错了什么吗?
我制作了一个独立的简单应用程序,现在我收到以下详细错误:
Exception in thread "main" com.google.gdata.util.AuthenticationException: Failed to refresh access token: 401 Unauthorized
at com.google.gdata.client.GoogleAuthTokenFactory$OAuth2Token.refreshToken(GoogleAuthTokenFactory.java:260)
at com.google.gdata.client.GoogleAuthTokenFactory.handleSessionExpiredException(GoogleAuthTokenFactory.java:702)
at com.google.gdata.client.GoogleService.handleSessionExpiredException(GoogleService.java:738)
at com.google.gdata.client.GoogleService.getFeed(GoogleService.java:649)
at com.google.gdata.client.Service.getFeed(Service.java:1017)
at JavaApplication20.printDocuments(JavaApplication20.java:50)
at main.main(main.java:35)
Run Code Online (Sandbox Code Playgroud)