彻底删除daemontools启动的服务

| | Comments (0) | TrackBacks (0)

一直感觉删除一个daemontools启动的服务好麻烦。昨天strace了一下svscan和svc等程序,然后搞了个脚本专门来删这种服务。

具体细节请看: http://www.jianingy.com/killdaemontools.html

作为罪恶的Admin,一定要有罪恶的脚本。当那个讨厌的用户又登录了你的server,不要pkill -9 -f "sshd: the_user". 如果你这么做了,那是粗鲁而不是罪恶。罪恶的admin要调戏(molest)他的用户。让我们发挥SIGSTOP和SIGCONT的威力吧。找到那家伙的sshd,kill -STOP,sleep 0.1,kill -CONT.让他的终端像9C的魔兽世界一样卡。从精神上打垮他。当然我们需要sleep的真实一些,让这看起来真的像网络故障。于是moluser脚本出现了:

#!/bin/bash
# molest user by SIGSTOP and SIGCONT his sshd

GUY="$1"
MAXSLEEP="$2"
MAXSLEEP=${MAXSLEEP:0:10}
ZERO="0000000000"
if [ -z "$GUY" ]
then
    echo "Usage: moluser <username> [max sleep time(millionseconds)]"
    echo
    echo "Molest a user py kill -STOP his sshd"
    echo '$Id$'
    exit 0
fi

[ -z "$MAXSLEEP" ] && SLEEP=1000

while true
do
    SLEEP=$(expr $RANDOM % $MAXSLEEP)
    SLEEP="${ZERO:0:$(expr ${#MAXSLEEP} - ${#SLEEP})}$SLEEP"
    SLEEP="${SLEEP:0:1}.${SLEEP:1}"
    _UID="$(id -u $GUY 2>/dev/null)"
    pkill -SIGSTOP -U $_UID sshd
    sleep $SLEEP
    pkill -SIGCONT -U $_UID sshd
done

scp回拷文件

| | Comments (0) | TrackBacks (0)

如果登录了一台机器,想把上面某个文件copy回自己的机器,有懒得ifconfig找自己机器的IP。可以用:

scp file  ${SSH_CONNECTION%% *}:~/

依据 http://lists.schmorp.de/pipermail/rxvt-unicode/2007q4/000514.html 的说法。rxvt-unicode的字间距比其他terminal要大出很多,是因为计算公式有错误。使用如下patch即可消除这个问题。

diff -NBupr rxvt-unicode-9.02/src/rxvtfont.C rxvt-unicode-9.02-fixed/src/rxvtfont.C
--- rxvt-unicode-9.02/src/rxvtfont.C    2008-01-26 23:07:30.000000000 +0800
+++ rxvt-unicode-9.02-fixed/src/rxvtfont.C      2008-05-11 21:47:56.000000000 +0800
@@ -1198,9 +1198,14 @@ rxvt_font_xft::load (const rxvt_fontprop
           g.width -= g.x;

           int wcw = WCWIDTH (ch);
+/*
+ * see: http://lists.schmorp.de/pipermail/rxvt-unicode/2007q4/000514.html
           if (wcw > 0) g.width = (g.width + wcw - 1) / wcw;
-
           if (width    < g.width       ) width    = g.width;
+*/
+          if (wcw > 1) g.xOff = g.xOff / wcw;
+          if (width < g.xOff) width = g.xOff;
+
           if (height   < g.height      ) height   = g.height;
           if (glheight < g.height - g.y) glheight = g.height - g.y;
         }

一台机器上mount了一很多nfs的分区,但是其中一个nfs server挂了(硬件问题一时启动不起来)。结果几个df进程就跟着挂起了,并且用kill -9也杀不掉。当时的进程状态是:

[jianingy(0)@xxxxxx ~]$ ps ax -o pid,wchan,s,command | grep df$
3505 rpc_ex D df
3844 rpc_ex D df
4162 rpc_ex D df

[jianingy(0)@xxxxxx ~]$ pstree
init─┬─acpid
    ├─agetty
    ├─atd
    ├─crond
    ├─dbus-daemon-1
    ├─3*[df]
...

所有df都wait在了rpc_execute。进程都在uninterruptible sleep(即ps的D状态),因此不会处理任何信号。经过广泛的搜索,发现rpc_execute所需数据由rpciod提供。因此只要killall -KILL rpciod就可以终止rpc_execute调用, 而rpciod在被杀掉后会自己重启过来。

另外出现这种情况大多因为使用默认方式mount了nfs, 这种情况下连接失败时nfs客户端会不停尝试连接服务器。在mount时使用intr选项可以避免这类问题的出现。下面贴一段nfs mount option的说明

       soft           If  an  NFS  file operation has a major timeout then report an I/O error to the calling
                      program.  The default is to continue retrying NFS file operations indefinitely.

       hard           If an NFS file operation has a major timeout then report "server not responding" on the
                      console and continue retrying indefinitely.  This is the default.

       intr           If an NFS file operation has a major timeout and it is hard mounted, then allow signals
                      to interupt the file operation and cause it to return EINTR  to  the  calling  program.
                      The default is to not allow file operations to be interrupted.

Linux & BSD上获取本机IP地址

| | Comments (0) | TrackBacks (0)
ifconfig | awk '/inet (addr:)?/{sub("addr:","");print $2;exit}'

需要拿多个IP的话,把exit去掉即可

Keep Tunnelling - 不灭的隧道

| | Comments (0) | TrackBacks (0)

我们用daemontools来控制ssh tunnel确保tunnel能在意外断开后主动连接上。在daemon中做tunnel最大的问题就是如何输入密码。这个问题可以使用以前的 ssh-attach 脚本来解决。

首先创建一个连接脚本(tunnel.sh):

#!/bin/sh
HOME=/home/jianingy
eval `$HOME/bin/ssh-attach`
ssh -p 23456 -b 0.0.0.0 -R 12345:localhost:143 xxx.xxx.xxx.xxx "vmstat 60"

再建立daemontools的run脚本:

#!/bin/sh

exec setuidgid jianingy ./tunnel.sh

用加载ssh-agent的用户(这里是jianingy)来执行tunnel.sh(即用setuidgid jianingy在指定),酱紫tunnel.sh中调用的ssh-attach便能复用已有的ssh-agent了,保证tunnel连接无须输入密码.

daemontools 常用操作

| | Comments (0) | TrackBacks (0)

这里可说的是DJB写的 daemontools , 不是DAEMON Tools那个虚拟光驱哦.

启停daemon

svc -t /service/lighttpd # 重启lighttpd
svc -u /service/lighttpd # 启动lighttpd
svc -d /service/lighttpd # 停止lighttpd

删除daemon

cd /service/lighttpd
rm -rf /service/lighttpd
svc -dx .

查看daemon的状态

svstat /service

查看daemon log

tailn64log /service/lighttpd/log/main/current

PS. 用daemontools管理lighttpd再好不过了,能够准确的定位多个lighttpd服务中你想要的那个~

Perl Module 的编写

| | Comments (0) | TrackBacks (0)
package YourModule; 
# gives you Exporter's import() method directly
use Exporter 'import'; 
@EXPORT_OK = qw(munge frobnicate);
@EXPORT = qw(sth);

@EXPORT_OK 中声明的函数需要在use YourModule后加入qw//来申请进入全局。而@EXPORT 中声明的函数是默认就进入全局的。

linux的TCP状态里各种WAIT的含义

| | Comments (0) | TrackBacks (0)

netstat -ant 显示的连接状态有几种WAIT: FIN_WAIT_1,FIN_WAIT_2,CLOSE_WAIT和TIME_WAIT. 他们的含义要从TCP的连接中断过程说起

Server              Client
  -------- FIN -------->
  <------- ACK ---------
  <------- FIN ---------
  -------- ACK -------->

对于服务器主动关闭连接(Active Close):

  1. 服务器首先向客户机发送FIN包,然后服务器进入FIN_WAIT_1状态。
  2. 客户机向服务器确认FIN包收到,向服务器发送FIN/ACK,客户机进入CLOSE_WAIT状态。
  3. 服务器收到来自客户机的FIN/ACK后,进入FIN_WAIT_2状态
  4. 现在客户机进入被动关闭("passive close")状态,客户机OS等待他上面的应用程序关闭连接。一旦连接被关
    闭,客户端会发送FIN包到服务器
  5. 当服务器收到FIN包后,服务器会向客户机发送FIN/ACK确认,然后进入著名的TIME_WAIT状态
  6. 由于在连接关闭后,还不能确定所有连接关闭前的包都被服务器接受到了(包的接受是没有先后顺序的),因此有了TIME_WAIT状态。在这个状态中,服务器仍然在等待客户机发送的但是还未到达服务器的包。这个状态dl.
  7. 将保持2*MSL的时间,这里的MSL指的是一个TCP包在网络中存在的最长时间。一般情况下2*MSL=240秒。
声援SOFF|声援珊瑚虫:如果你是珊瑚虫用户,请坚决力挺声援珊瑚虫!

Archives