mirror of
https://github.com/lollipopkit/flutter_server_box.git
synced 2025-12-17 07:14:28 +01:00
#148 fix: alpine process
This commit is contained in:
@@ -2,14 +2,16 @@ import '../../res/build_data.dart';
|
||||
import '../../res/server_cmd.dart';
|
||||
import '../server/system.dart';
|
||||
|
||||
const _cmdDivider = '\necho $seperator\n';
|
||||
const _cmdDivider = '\necho $seperator\n\t';
|
||||
|
||||
const _serverBoxDir = r'$HOME/.config/server_box';
|
||||
const _shellPath = '$_serverBoxDir/mobile_app.sh';
|
||||
|
||||
enum AppShellFuncType {
|
||||
status,
|
||||
docker;
|
||||
docker,
|
||||
process,
|
||||
;
|
||||
|
||||
String get flag {
|
||||
switch (this) {
|
||||
@@ -17,6 +19,8 @@ enum AppShellFuncType {
|
||||
return 's';
|
||||
case AppShellFuncType.docker:
|
||||
return 'd';
|
||||
case AppShellFuncType.process:
|
||||
return 'p';
|
||||
}
|
||||
}
|
||||
|
||||
@@ -30,6 +34,8 @@ enum AppShellFuncType {
|
||||
// `dockeR` -> avoid conflict with `docker` command
|
||||
// 以防止循环递归
|
||||
return 'dockeR';
|
||||
case AppShellFuncType.process:
|
||||
return 'process';
|
||||
}
|
||||
}
|
||||
|
||||
@@ -39,17 +45,36 @@ enum AppShellFuncType {
|
||||
return '''
|
||||
result=\$(uname 2>&1 | grep "Linux")
|
||||
if [ "\$result" != "" ]; then
|
||||
${_statusCmds.join(_cmdDivider)}
|
||||
\t${_statusCmds.join(_cmdDivider)}
|
||||
else
|
||||
${_bsdStatusCmd.join(_cmdDivider)}
|
||||
\t${_bsdStatusCmd.join(_cmdDivider)}
|
||||
fi''';
|
||||
case AppShellFuncType.docker:
|
||||
return '''
|
||||
result=\$(docker version 2>&1 | grep "permission denied")
|
||||
if [ "\$result" != "" ]; then
|
||||
${_dockerCmds.join(_cmdDivider)}
|
||||
\t${_dockerCmds.join(_cmdDivider)}
|
||||
else
|
||||
${_dockerCmds.map((e) => "sudo -S $e").join(_cmdDivider)}
|
||||
\t${_dockerCmds.map((e) => "sudo -S $e").join(_cmdDivider)}
|
||||
fi''';
|
||||
case AppShellFuncType.process:
|
||||
return '''
|
||||
# Try sequencially
|
||||
# main Linux: `ps -aux`
|
||||
# BSD: `ps -ax`
|
||||
# alpine: `ps -o pid,user,time,args`
|
||||
#
|
||||
# If there is any error, try another one
|
||||
result=\$(ps -aux 2>&1 | grep "ps: ")
|
||||
if [ "\$result" = "" ]; then
|
||||
ps -aux
|
||||
else
|
||||
result=\$(ps -ax 2>&1 | grep "ps: ")
|
||||
if [ "\$result" = "" ]; then
|
||||
ps -ax
|
||||
else
|
||||
ps -o pid,user,time,args
|
||||
fi
|
||||
fi''';
|
||||
}
|
||||
}
|
||||
|
||||
@@ -1,18 +1,16 @@
|
||||
// Models for `ps -aux`
|
||||
|
||||
// Each line
|
||||
import 'dart:convert';
|
||||
import '../../../data/res/misc.dart';
|
||||
|
||||
/// Some field can be null due to incompatible format on `BSD` and `Alpine`
|
||||
class Proc {
|
||||
final String user;
|
||||
final int pid;
|
||||
final double cpu;
|
||||
final double mem;
|
||||
final String vsz;
|
||||
final String rss;
|
||||
final String tty;
|
||||
final String stat;
|
||||
final String start;
|
||||
final double? cpu;
|
||||
final double? mem;
|
||||
final String? vsz;
|
||||
final String? rss;
|
||||
final String? tty;
|
||||
final String? stat;
|
||||
final String? start;
|
||||
final String time;
|
||||
final String command;
|
||||
|
||||
@@ -65,7 +63,7 @@ class Proc {
|
||||
|
||||
@override
|
||||
String toString() {
|
||||
return const JsonEncoder.withIndent(' ').convert(toJson());
|
||||
return jsonEncoder.convert(toJson());
|
||||
}
|
||||
|
||||
String get binary {
|
||||
@@ -99,10 +97,10 @@ class PsResult {
|
||||
}
|
||||
switch (sort) {
|
||||
case ProcSortMode.cpu:
|
||||
procs.sort((a, b) => b.cpu.compareTo(a.cpu));
|
||||
procs.sort((a, b) => b.cpu?.compareTo(a.cpu ?? 0) ?? 0);
|
||||
break;
|
||||
case ProcSortMode.mem:
|
||||
procs.sort((a, b) => b.mem.compareTo(a.mem));
|
||||
procs.sort((a, b) => b.mem?.compareTo(a.mem ?? 0) ?? 0);
|
||||
break;
|
||||
case ProcSortMode.pid:
|
||||
procs.sort((a, b) => a.pid.compareTo(b.pid));
|
||||
@@ -110,6 +108,9 @@ class PsResult {
|
||||
case ProcSortMode.user:
|
||||
procs.sort((a, b) => a.user.compareTo(b.user));
|
||||
break;
|
||||
case ProcSortMode.name:
|
||||
procs.sort((a, b) => a.binary.compareTo(b.binary));
|
||||
break;
|
||||
}
|
||||
return PsResult(procs: procs, error: err.isEmpty ? null : err);
|
||||
}
|
||||
@@ -119,5 +120,7 @@ enum ProcSortMode {
|
||||
cpu,
|
||||
mem,
|
||||
pid,
|
||||
user;
|
||||
user,
|
||||
name,
|
||||
;
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user