通用linux后台运行shell脚本

左手 posted @ 2013年8月19日 18:56 in Linux with tags nohup 后台运行 linux后台运行 goagent后台启动 linux下goagent自动启动 , 13981 阅读

经常遇到开启后需要占一个终端窗口的应用。写了一个通用的后台运行脚本用nohup在后台运行,将运行pid输出到文件,可以查看nohup.out中的输出内容。适用于GoAgent、grant watch、rails开发模式等。可以把脚本加在~/.bash_profile中,实现登陆后自动启动。修改PROGNAME中的内容为要运行的程序名,修改run中的运行命令为你要执行的命令即可。

#!/bin/sh
PID=0
DIR=`dirname $0`
PIDFILE=$DIR/nohup.pid
OUTFILE=$DIR/nohup.out
PROGNAME='GoAgent'

run() {
    nohup $DIR/proxy.py>>$OUTFILE 2>&1 & echo $!>$PIDFILE
}

init() {
    if [ -f $PIDFILE ]; then
        PID=`cat $PIDFILE`
    fi
}

check() {
    if [ $PID -eq 0 ]; then
        return 1
    else
        kill -0 $PID 2>/dev/null
    fi
}

start() {
    init
    check
    if [ $? -eq 0 ]; then
        echo "$PROGNAME is run!" && exit 1
    else
        run
        echo "Start $PROGNAME!" && exit 0
    fi
}

silent() {
    init
    check
    if [ $? -eq 0 ]; then
        exit 1
    else
        run
        exit 0
    fi
}

restart() {
    init
    check
    if [ $? -eq 0 ]; then
        kill -9 $PID>/dev/null 2>&1
    fi
    run
    echo "Restart $PROGNAME!" && exit 0
}

stop() {
    init
    check
    if [ $? ]; then
        kill -9 $PID>/dev/null 2>&1
        echo "$PROGNAME is stop!" && exit 0
    else
        echo "$PROGNAME is not run!" && exit 1
    fi
}

status() {
    init
    check
    if [ $? -eq 0 ]; then
        echo "$PROGNAME is run!" && exit 0
    else
        echo "$PROGNAME is not run!" && exit 1
    fi
}

output() {
    if [ -f $OUTFILE ]; then
        watch -n 1 tail $OUTFILE
    else
        echo "$OUTFILE is not find!" && exit 1
    fi
}

clean() {
    if [ -f $OUTFILE ]; then
        rm -f $OUTFILE
    fi
    if [ -f $PIDFILE ]; then
        rm -f $PIDFILE
    fi
    echo 'Clean success!' && exit 0
}

case "$1" in
    start)
        $1
        exit 0
        ;;
    silent)
        $1
        exit 0
        ;;
    restart)
        $1
        exit 0
        ;;
    stop)
        $1
        exit 0
        ;;
    status)
        $1
        exit 0
        ;;
    output)
        $1
        exit 0
        ;;
    clean)
        $1
        exit 0
        ;;
    *)
        echo $DIR
        echo 'Usage: {start|stop|restart|silent|status|output|clean}'
        ;;
esac

命令说明:
start   启动
silent  安静模式启动(无输出)
stop    停止
restart 重启
status  查看运行状态
output  查看输出
clean   清除out和pid文件

Avatar_small
xell 说:
2013年8月28日 17:30

第9行
nohup $DIR/proxy.py>>$OUTFILE 2>&1 & echo $!>$PIDFILE

proxy.py 应当是 $PROGNAME 吧?

Avatar_small
左手 说:
2013年9月02日 00:01

@xell: 因为某些输出显示的原因,执行的命令只能在run里面写死。$PROGNAME是用来打印提示信息的时候用的。

Avatar_small
getwayxue 说:
2014年5月25日 11:08

这个脚本非常适用,而且非常全面啊,感谢分享~

Avatar_small
KSEB quick pay 说:
2023年1月19日 16:54

Kerala State Electricity Board formed by Kerala State Government under the Electricity act 1948. KSEB determined to generate, transmit and distribute the electricity in the regions of Kerala State. KSEB quick pay As a customer, you can use the KSEB online payment of the electricity department to update your pending dues for the electricity used. The bill may generat every month based on the reading grabbed from the electric meter fixed in your place that can be commercial or domestic.

Avatar_small
seo service london 说:
2024年2月21日 22:40

Great and an informative article! this wonderful post


登录 *


loading captcha image...
(输入验证码)
or Ctrl+Enter