8月 02

linux远程通过ssh启停tomcat

  在linux上远程通过ssh启停tomcat,因为存在环境变量加载问题,所以写一个shell脚本,放在tomcat的bin目录下。CentOS测试通过。需要使用bash。因为脚本中包含脚本名字,在查找时要过滤掉自己,所以如果需要修改脚本名字,在脚本内也要相应的修改。

#!/bin/bash
# -------------------------------------------------------------------------------
# Filename:    tomcatcomm.sh
# Revision:    1.0
# Date:        2013-7-29
# Author:      simonzhang
# Email:       simon-zzm@163.com
# Description: 
# -------------------------------------------------------------------------------
# Source function library.
. /etc/init.d/functions
source /etc/profile

#### shell env
path=$(dirname $0)
if [ ${path:0:1} == . ]; then
   path=${path/./$(pwd)}
fi
my_name="tomcatcomm.sh"

###### delete tomcat`s work dir and startup server
del_start()
{
    /bin/rm -rf ${1}work/*
}

######main()
case "$1" in
  start)
       get_pid_count=`/bin/ps -ef|grep -v grep|grep -v ${my_name}|grep ${path}|wc -l`
       if [ ${get_pid_count} -gt 0 ] ; then
          /bin/ps -ef|grep -v grep|grep -v ${my_name}|grep ${path}|awk ' ''{print $2}'|xargs kill -9
          sleep 1
          get_pid_count=`/bin/ps -ef|grep -v grep|grep -v ${my_name}|grep ${path}|wc -l`
           if [ ${get_pid_count} -gt 0 ] ; then
              echo "Error"
           else
              del_start ${path}
              ${path}/startup.sh
           fi
        else
            del_start ${path}
            ${path}/startup.sh
        fi
        ;;
  stop)
        ${path}/shutdown.sh
        del_start ${path}
        sleep 1
        ;;
  restart)
	$0 stop
	$0 start
	;;
  *)
        echo $"Usage: $0 {start|stop|restart}"
        exit 1
esac

tomcat远程启停脚本

9月 27

我的 tornado 启停脚本

  之前在tornado学习中(一)中,写了一个启停的脚本,来启动多个端口,但是其中没有日志部分。我将脚本修改一下,使其能支持启动输出日志。具体可见早起笔记http://www.simonzhang.net/?p=1170

#!/bin/sh
#
# Filename:    main.sh
# Revision:    1.1
# Date:        2012-09-27
# Author:      simonzhang
# web:         www.simonzhang.net
# Email:       simon-zzm@163.com
#
### END INIT INFO

# Source function library.
. /etc/profile

# Set the base value
listen_line=1
listen_start=8000
## info|warning|error|none   
loglevel='info'
log_file_prefix='logs/pypixshow.log'
log_file_max_size=20480

# 
CWD=`pwd`
cd $CWD

# See how we were called.
case "$1" in
  start)
        /bin/rm -rf main.port
	for (( i=0 ; i<${listen_line} ; i++)); do
            listen_port=$[${listen_start}+${i}]
            echo ${listen_port} >> main.port
            python main.py ${listen_port} ${loglevel} ${log_file_prefix} ${log_file_max_size} &
	done
        echo "start ok !"
        ;;
  stop)
        get_port_line=`/bin/cat main.port`
        for i in ${get_port_line};do
             now_pid=`/bin/ps -ef|grep ${i}|grep -v grep|awk ' ''{print $2}'`
             /bin/kill -9 $now_pid
        done
        /bin/rm -rf *.pyc
        echo "stop"
        ;;
  status)
        get_port_line=`/bin/cat main.port`
        for i in ${get_port_line};do
             now_pid=`/bin/ps -ef|grep ${i}|grep -v grep`
             if [ -z "${now_pid}" ] ; then
                 echo ${i} "is stop"
             else
                 echo ${now_pid}
             fi
        done
	;;
  restart)
	$0 stop
	$0 start
	;;
  *)
        echo $"Usage: $0 {start|stop|restart|status}"
        exit 1
esac

exit $rc

main.py 也需要修改,下面简单列出日志相关的代码。

import  tornado.options
import logging

if __name__ == "__main__":
    listen_port =  sys.argv[1]
    tornado.options.options['logging'].set(sys.argv[2])
    tornado.options.options['log_file_prefix'].set(sys.argv[3])
    tornado.options.options['log_file_max_size'].set(int(sys.argv[4]))
    tornado.options.parse_command_line()
    application.listen(listen_port)
    tornado.ioloop.IOLoop.instance().start()
4月 12

C 语言 读取mysql

测试环境:CentOS 5.4 64位 mysql5.5.20
mysql使用bin安装,安装在/program/mysql目录

代码

#include 
#include 
#include 
#define GET_ARRAY_LEN(array,len) {len = (sizeof(array) / sizeof(array[0]));}

int main(int argc,char **argv )
{
    MYSQL *sql_p;
    MYSQL_RES *result_p;
    MYSQL_ROW row_p;
    int res;
    int len;
    sql_p=mysql_init(NULL);
    if(sql_p==NULL){
        printf("Init error!\n");
        exit(1);
    }

    sql_p=mysql_real_connect(sql_p,"192.168.1.41","root","test",
                    "mysql",0,NULL,0);
    if(!sql_p){
        fprintf(stderr,"%d:%s\n",mysql_errno(sql_p),mysql_error(sql_p));
        exit(1);
    }

    res=mysql_query(sql_p,"SELECT * FROM admin");
    if(res){
        printf("Select error!\n");
        exit(1);
    }
    
    result_p=mysql_use_result(sql_p);
    if(!result_p){
        printf("mysql_use error!\n");
        exit(1);
    }

    row_p=mysql_fetch_row(result_p);
    GET_ARRAY_LEN(row_p,len);
    printf("%d\n",len);
    int i;
    for (i=0 ;i<20 ;i++)
      {if (row_p[i][1])
         printf("%s\n",row_p[i]);
       }
    mysql_close(sql_p);
    return 0;
}

编译语句
gcc -Wall cmysql.c -o cmysql -L /program/mysql/lib -I /program/mysql/include/ -lmysqlclient -lz

问题:
运行报错
./cmysql: error while loading shared libraries: libmysqlclient.so.18: cannot open shared object file: No such file or directory

解决办法:
32位操作系统运行下面一句
ln -s /usr/local/mysql/lib/libmysqlclient.so.18 /usr/lib/
64位操作系统运行下面一句
ln -s /usr/local/mysql/lib/libmysqlclient.so.18 /usr/lib64/
再次编译通过。
在读取数据时还出现一个问题,如果没有数据,程序显示错误,这个我没有处理。

3月 15

python 在 crontab 中的调用

python 在 crontab 中的调用

  使用python写了一个脚本,手动执行没有问题。需要自动运行,所以要配置到crontab中。举例脚本位置“/Data/script/test.py” ,其中包含读取操作系统、读取配置文件、写日志操作,使用root用户每5分钟执行一次。最初配置为:

*/5 * * * * /usr/local/bin/python /Data/script/test.py >/dev/null

  需要注意的是“*/5 * * * *”中间要用空格分隔。“/usr/local/bin/python”要根据使用的python脚本安装位置填写,通过观察定时任务失败。
  分析原因,crontab运行时的环境变量与ssh登录的环境变量不同,导致读取配置目录和文件失败。解决方法,写一个shell调用脚本,将脚本放在crontab中。脚本有一个参数“start”。
shell脚本为test.sh:

#! /bin/bash
#
# crontab shell python
#
# www.simonzhang.net
# email:simon-zzm@163.com
#
### END INIT INFO

. /etc/profile
cd /Data/script/

case "$1" in
  start)
      /usr/local/bin/python /Data/script/test.py start &
     ;;
  test)
      /usr/local/bin/python /Data/script/test.py test &
      ;;
  *)
        echo $"Usage: $0 {start|test}"
        exit 1
esac

exit 1

将脚本配置到crontab中运行成功,配置为:

*/5 * * * * /bin/sh /Data/script/test.sh start >/dev/null