我们在登录主机上使用 freenx-server,用户从终端通过 NoMachine 客户端访问它们。由于与 freenx 相关的许多错误以及我们的预算不允许使用完整的商业版本,当连接在终止后没有自行清理时(通常是无意的),我们最终会导致某些用户拥有多个会话残留。以下是 /etc/nxserver.log 的示例摘录:
Display Type Session ID Options Depth Screen Status Session Name
------- ---------------- -------------------------------- -------- ----- -------------- ----------- ------------------------------
33091 unix-gnome 189E20515FC5624507CAF25C836FB426 --D--PSA 24 885x843 Suspended Connection to NXHost11 (user5) (Shadowed)
33053 unix-gnome 1DBD1E1131156842D72376675CE33E89 --D--PSA 24 1920x1123 Suspended Connection to NXHost11 (user5) (Shadowed)
33111 unix-gnome 385B87A23EA908C0B4C6D00806D063B6 --D--PSA 24 871x538 Suspended Connection to NXHost11 (user5) (Shadowed)
33082 unix-gnome 4D30B996528B752BFB1DE30240659230 --D--PSA 24 871x734 Running Connection to NXHost11 (user5) (Shadowed)
33057 unix-gnome 57904D3FF28785366D962CDA35CBC6D4 --D--PSA 24 871x555 Running Connection to NXHost11 (user5) (Shadowed)
33044 unix-gnome 6E338B9EE71D55DE4873ED83FCCB0AD9 --D--PSA 24 871x538 Suspended Connection to NXHost11 (user5) (Shadowed)
33112 unix-gnome 8B2C9442496D09EFA0B78A13239D4EA0 --D--PSA 24 871x696 Suspended Connection to NXHost11 (user5) (Shadowed)
33110 unix-gnome 8FD5158542B616E6B9B05B4E2425A6AF --D--PSA 24 871x831 Suspended Connection to NXHost11 (user5) (Shadowed)
33014 unix-gnome 933CD67071697FBDC63D435E954BC8D6 --D--PSA 24 871x538 Running Connection to NXHost11 (user5) (Shadowed)
33072 unix-gnome B07D38F59367CE02ECA445B6EBBF84A1 --D--PSA 24 1138x667 Running Connection to NXHost11 (user5) (Shadowed)
33000 unix-gnome B58A633707EEC0C59C8A51EC9D688D3C --D--PSA 24 1920x1123 Running Connection to NXHost11 (user5) (Shadowed)
33069 unix-gnome BB78D40014DE5CF2A706536FF76FCFD8 --D--PSA 24 871x802 Suspended Connection to NXHost11 (user5) (Shadowed)
33108 unix-gnome C57D24C4AAC14F839E0A8C6B6C83D24E --D--PSA 24 1920x1123 Suspended Connection to NXHost11 (user5) (Shadowed)
33070 unix-gnome F90462D7CCEC82D5DDDA1E8C2CCE78E4 --D--PSA 24 1168x909 Suspended Connection to NXHost11 (user5) (Shadowed)
要删除会话以使用户的客户端不会持续冻结,我们必须手动删除它们/var/lib/nxserver/db/running/
- 但格式不一样:
[root@NXHost11 running]# pwd
/var/lib/nxserver/db/running
[root@NXHost11 running]# ls
sessionId{095675CAD6805301A215278C29E186F5} sessionId{84A166F4F3D0B743357FA37751BDA786}
sessionId{0FA3E7C0B2914C66B8E468BE646D88F7} sessionId{8B2C9442496D09EFA0B78A13239D4EA0}
sessionId{104750C53E17F5B87A9BFBC1E966B8D7} sessionId{8CC40155C71AE6E7F73365DAFDCD2349}
sessionId{14B7F108B0A13635B7D697B26427D856} sessionId{8ECBE26B9D5C10C44192C150FDA80158}
sessionId{15782E405134B2CF3BD09821CC3DC55C} sessionId{8FADEDAE322A31585EB0DD8A30AB0285}
sessionId{180CA97293F95E625F12894B78FE100B} sessionId{8FD5158542B616E6B9B05B4E2425A6AF}
sessionId{189E20515FC5624507CAF25C836FB426} sessionId{92BCB8BA457267750FFE862EEB3C8CE6}
sessionId{18F86BA13166B94D0A80F10E3D3F220F} sessionId{933CD67071697FBDC63D435E954BC8D6}
sessionId{195E394D40C21178D0ED4E5198B90792} sessionId{9400C9D9D16CC0D4254DFAC45C9A52C9}
sessionId{1CD93CE30AD76937710823D0FAE15828} sessionId{95DE940721837DF10BE3FEB1535DC78A}
sessionId{1DBD1E1131156842D72376675CE33E89} sessionId{9A2A5A849295FC2EC4A9B7E27B53CBF7}
sessionId{1E1B5C326321775CCA05F2752CE17A8B} sessionId{9D565E61FA3B1D81F9F1A0B185A0EC83}
sessionId{1FE7FD8EC70DC99652F214A3B5E905D1} sessionId{A5877C91B948DFC24AF86EFB2495E527}
sessionId{22FAD34C0CC63CD959204669D8FDD9F0} sessionId{A64443BB165B532521741D5D8F19EFAB}
sessionId{272CF217D5E8F56973743F86CAAF94BC} sessionId{ABDC93271DF58FCE7DA7DBE7500C21B3}
sessionId{27FF7291F20965E10272A39B1FB8E4DC} sessionId{AF6B1A42DD47C25FD176E60231A90DAD}
sessionId{2E7F640462E924284B079E10CC0E2E23} sessionId{B07D38F59367CE02ECA445B6EBBF84A1}
sessionId{330CB4473B1AF6C948F2BFB9BDD2BADB} sessionId{B2D860333DDD3B7FF35EA48BFF9ABD00}
sessionId{385B87A23EA908C0B4C6D00806D063B6} sessionId{B42E37E4785B42342C4EB009D74B4EA0}
sessionId{389242121CE6E206584C2A45E236ABFF} sessionId{B58A633707EEC0C59C8A51EC9D688D3C}
sessionId{3C4884BB99ADFF1FA18B039E88A864D1} sessionId{B6C3CDE47C2371972A7536B854A931F4}
sessionId{3FED31A80C56BA9B038A6A765AED0C38} sessionId{B779C01B1F1915E571B1639A481E9848}
sessionId{400753DEC565BB0DB52ED904137C463F} sessionId{BB78D40014DE5CF2A706536FF76FCFD8}
sessionId{421C53FC39AE9D79E60E6055D2FC7DE8} sessionId{C0740993AD5429567243033DD5451B0F}
sessionId{42F76BBDCE57874C8E3A0109E61BE37B} sessionId{C0B5C4E91BFFC94A2ED8C399ADD264E4}
sessionId{43DBB2733414428C80DD68FA87D7C5B7} sessionId{C57D24C4AAC14F839E0A8C6B6C83D24E}
sessionId{46A37999D33B0D66026C48D6D9C7B90A} sessionId{C605F9961D8C4C6511CE11F375E2DAD2}
sessionId{4D30B996528B752BFB1DE30240659230} sessionId{CD76B8B7119293B2CA6164D621BCA062}
sessionId{57904D3FF28785366D962CDA35CBC6D4} sessionId{DAAA94C7D01E099A1D566AF0E44B23E4}
sessionId{58BFD388FBED5D39EED5D03A6BEB83B7} sessionId{DBAE1C4AD9B0CEB956711384B9A4C503}
sessionId{5DC416CBE36DD68259246F21C4D12C63} sessionId{DF866B06E27911C90130A4AC7F24E03C}
sessionId{6284D3EE9771A085520C12C5C089CB63} sessionId{E112F0415D4B2B47D892FA16D83819AE}
sessionId{63F6B34AF4DD407E46CAB8D52EE1B00D} sessionId{E767F74579682E357BCF9E8635658847}
sessionId{64CD7EC1A6156DFD992DCF6AA0D3D959} sessionId{EB78959FD59424DAF2C965388A056AE3}
sessionId{6D570D2998E544DD2E2B4F68A9185377} sessionId{ED560BFC6D38D0946AF3E0A453E8EBC2}
sessionId{6E338B9EE71D55DE4873ED83FCCB0AD9} sessionId{EE11025659E3D66007357A870FB973AA}
sessionId{70556E711755D613445D72F505F5ACAA} sessionId{EF819D2926EFF1DA0E0208578D282D28}
sessionId{7706DEA73E98C5997D61CA6BE49D2313} sessionId{F1141CC7C189F45D4C6E1B5A805BA849}
sessionId{7723BB4A0C5CCE18A67D5D64235357AC} sessionId{F5E6EC0AB1E015AE187CC30412291A70}
sessionId{787D6CE3EBA641706F3E07DD61159B24} sessionId{F90462D7CCEC82D5DDDA1E8C2CCE78E4}
sessionId{7BD83901652D8F26B9D4B128D0021344} sessionId{F904BF11478F62FF368ACAA9978F365B}
sessionId{8100B0CADB1369EB9573E9B986758545} sessionId{F91EECDB9A7B9298763F3DB439205CFC}
另一个问题是,同一主机上有其他用户正在运行 NoMachine 会话,我无法关闭,或者只需 rm 整个目录的会话就很容易了。
因此,问题是,除了手动删除每个单独的会话 ID: 之外
rm sessionId{pasteletternumbercombinationhere}
,是否有一种更简单的方法可以在不影响其他用户会话的情况下完成此操作?看起来 ps 不能显示我可以编写脚本的信息,因为 freenx 的“处理程序”机制取得所有权,并且所有 sessionID 文件都是 nx:nx 而不是用户名。
答案1
我希望我的理解是正确的:您想要删除 sessionID 列为已挂起的所有文件吗?这将是
for SessionID in `sed -n '/Suspended/s/.*unix-gnome *\(\S*\).*/\1/p' /etc/nxserver.log`; do
rm /var/lib/nxserver/db/running/sessionId\{$SessionID\}
done
sed 脚本从挂起的会话中提取 sessionId,每个相应的文件都会被删除。
如果这不是您想要的,请澄清您的问题。
答案2
由于您的数据已很好地制成表格,因此快速awk
即可完成您的大部分工作:
awk 'NR>2 && $7=="Suspended" {print "sessionID{"$3"}"}' /etc/nxserver.log | xargs rm -f
要查看运行前要删除的文件列表,只需使用:
awk 'NR>2 && $7=="Suspended" {print "sessionID{"$3"}"}' /etc/nxserver.log
答案3
cd "/var/lib/nxserver/db/running" && \
perl -lne '
@F = unpack "x25A32x31A12";
unlink "sessionId{$F[0]}" if $F[1] eq "Suspended";
' /etc/nxserver.log
unpack
跳过前 25 个字符,然后获取下一个 32 = sessionId,并类似地获取 Status 字段。最后unlink
删除该文件。