2013年3月11日星期一

shell 脚本运行 提示bad interpreter:No such file or directory


客户的服务器有cron运行。在旧的系统中正常。网站转移到我们新的服务器上,cron也完全copy。查看cron记录显示命令也运行了。但通过PHP文件生成的html文件没有内容。
将shell 脚本的错误输出重定向到一个文件。
/root/test.sh   2> /home/admin/test.txt 将test.sh脚本的错误输出重定向到/home/admi/test.txt文件。
打开test.txt文件:-bash: /root/test.sh : /bin/ksh^M: bad interpreter :No such file or directory
网上查了下资料。是文件格式导致的,查看下test.sh的格式:
vi test.sh
输入:set ff? 查看文件格式,出现 ileformat=dos,文件是DOS格式。使用:set ff=unix 强制将文件转换为unix格式。:wq 保存退出。再次域名test.sh脚本。文件正常输出。

linux 脚本不执行,出现错误bad interpreter: No such file or directory


linux 脚本不执行,出现错误bad interpreter: No such file or directory
出现bad interpreter:No such file or directory的原因
是文件格式的问题。这个文件是在Windows下编写的。换行的方式与Unix不一样,但是在VI下面如果不Set一下又完全看不出来。
解决方法:
1、上传到linux主机运行
     chmod +x back
     ./back
    错误提示如下:
    bash: ./back : bad interpreter:No such file or directory
2、错误分析:
因为操作系统是windows,在windows下编辑的脚本,所以有可能有不可见字符。
从你的脚本及报告的错误看来, 很有可能是你的脚本文件是DOS格式的, 即每一行的行尾以\r\n来标识, 其ASCII码分别是0x0D, 0x0A.
可以有很多种办法看这个文件是DOS格式的还是UNIX格式的, 还是MAC格式的
(1)vi filename
然后用命令
:set ff?
可以看到dos或unix的字样. 如果的确是dos格式的, 那么你可以用set ff=unix把它强制为unix格式的, 然后存盘退出. 再运行一遍看.

linux下监控系统进程并重启


一、用monit监控系统进程
monit是一款功能强大的系统状态、进程、文件、目录和设备的监控软件,用于*nix平台, 它可以自动重启那些已经挂掉的程序,非常适合监控系统关键的进程和资源(默认带web界面),如:nginx、apache、mysql和cpu占有率等。而监控管理Python进程,常用的是supervisorzdaemon
下面分别介绍monit的安装、配置和启动。
安装
在debian或ubuntu上安装monit非常方便,通过下面的命令
1
sudo apt-get install monit
即可,其它*nix上也很简单,下载源码走一遍安装三步就OK了。
1
2
3
./configure
make
make install
安装后,默认的配置文件为/etc/monit/monitrc。
配置
添加需要监控的进程等信息至monit的配置文件,monit的配置详见下面的示例文件。
001
002
003
004
005
006
007
008
009
010
011
012
013
014
015
016
017
018
019
020
021
022
023
024
025
026
027
028
029
030
031
032
033
034
035
036
037
038
039
040
041
042
043
044
045
046
047
048
049
050
051
052
053
054
055
056
057
058
059
060
061
062
063
064
065
066
067
068
069
070
071
072
073
074
075
076
077
078
079
080
081
082
083
084
085
086
087
088
089
090
091
092
093
094
095
096
097
098
099
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
##
## 示例monit配置文件,说明:
## 1. 域名以example.com为例。
## 2. 后面带xxx的均是举例用的名字,需要根据自己的需要修改。
##
 
###############################################################################
## Monit control file
###############################################################################
#
# 检查周期,默认为2分钟,对于网站来说有点长,可以根据需要自行调节,这改成30秒。
set daemon  30
 
# 日志文件
set logfile /var/log/monit.log
 
#
# 邮件通知服务器
#
#set mailserver mail.example.com
set mailserver localhost
 
#
# 通知邮件的格式设置,下面是默认格式供参考
#
## Monit by default uses the following alert mail format:
##
## --8<--
## From: monit@$HOST                         # sender
## Subject: monit alert --  $EVENT $SERVICE  # subject
##
## $EVENT Service $SERVICE                   #
##                                           #
##  Date:        $DATE                   #
##  Action:      $ACTION                 #
##  Host:        $HOST                   # body
##  Description: $DESCRIPTION            #
##                                           #
## Your faithful employee,                   #
## monit                                     #
## --8<--
##
## You can override the alert message format or its parts such as subject
## or sender using the MAIL-FORMAT statement. Macros such as $DATE, etc.
## are expanded on runtime. For example to override the sender:
#
# 简单的,这只改了一下发送人,有需要可以自己修改其它内容。
set mail-format { from: webmaster@example.com }
 
# 设置邮件通知接收者。建议发到gmail,方便邮件过滤。
set alert userxxx@gmail.com
 
set httpd port 2812 and            # 设置http监控页面的端口
     use address www.example.com   # http监控页面的IP或域名
     allow localhost               # 允许本地访问
     allow 58.68.78.0/24           # 允许此IP段访问
     ##allow 0.0.0.0/0.0.0.0       # 允许任何IP段,不建议这样干
     allow userxxx:passwordxxx     # 访问用户名密码
 
###############################################################################
## Services
###############################################################################
#
# 系统整体运行状况监控,默认的就可以,可以自己去微调
#
# 系统名称,可以是IP或域名
check system www.example.com
    if loadavg (1min) > 4 then alert
    if loadavg (5min) > 2 then alert
    if memory usage > 75% then alert
    if cpu usage (user) > 70% then alert
    if cpu usage (system) > 30% then alert
    if cpu usage (wait) > 20% then alert
 
#
# 监控nginx
#
# 需要提供进程pid文件信息
check process nginx with pidfile /var/run/nginx.pid
    # 进程启动命令行,注:必须是命令全路径
    start program = "/etc/init.d/nginx start"
    # 进程关闭命令行
    stop program  = "/etc/init.d/nginx stop"
    # nginx进程状态测试,监测到nginx连不上了,则自动重启
    if failed host www.example.com port 80 protocol http then restart
    # 多次重启失败将不再尝试重启,这种就是系统出现严重错误的情况
    if 3 restarts within 5 cycles then timeout
    # 可选,设置分组信息
    group server
 
#   可选的ssl端口的监控,如果有的话
#    if failed port 443 type tcpssl protocol http
#       with timeout 15 seconds
#       then restart
 
#
# 监控apache
#
check process apache with pidfile /var/run/apache2.pid
    start program = "/etc/init.d/apache2 start"
    stop program  = "/etc/init.d/apache2 stop"
    # apache吃cpu和内存比较厉害,额外添加一些关于这方面的监控设置
    if cpu > 50% for 2 cycles then alert
    if cpu > 70% for 5 cycles then restart
    if totalmem > 1500 MB for 10 cycles then restart
    if children > 250 then restart
    if loadavg(5min) greater than 10 for 20 cycles then stop
    if failed host www.example.com port 8080 protocol http then restart
    if 3 restarts within 5 cycles then timeout
    group server
    # 可选,依赖于nginx
    depends on nginx
 
#
# 监控spawn-fcgi进程(其实就是fast-cgi进程)
#
check process spawn-fcgi with pidfile /var/run/spawn-fcgi.pid
    # spawn-fcgi一定要带-P参数才会生成pid文件,默认是没有的
    start program = "/usr/bin/spawn-fcgi -a 127.0.0.1 -p 8081 -C 10 -u userxxx -g groupxxx -P /var/run/spawn-fcgi.pid -f /usr/bin/php-cgi"
    stop program = "/usr/bin/killall /usr/bin/php-cgi"
    # fast-cgi走的不是http协议,monit的protocol参数也没有cgi对应的设置,这里去掉protocol http即可。
    if failed host 127.0.0.1 port 8081 then restart
    if 3 restarts within 5 cycles then timeout
    group server
    depends on nginx
虽然在注释里有详细说明,但是我还是要再强调说明几点:
  1. start和stop的program参数里的命令必须是全路径,否则monit不能正常启动,比如killall应该是/usr/bin/killall。
  2. 对于spawn-fcgi,很多人会用它来管理PHP的fast-cgi进程,但spawn-fcgi本身也是有可能挂掉的,所以还是需要用monit来监控spawn-fcgi。spawn-fcgi必须带-P参数才会有pid文件,而且fast-cgi走的不是http协议,monit的protocol参数也没有cgi对应的设置,一定要去掉protocol http这项设置才管用。
  3. 进程多次重启失败monit将不再尝试重启,收到这样的通知邮件表明系统出现了严重的问题,要引起足够的重视,需要赶紧人工处理。
当然monit除了管理进程之外,还可以监控文件、目录、设备等,本文不做讨论,具体配置方式可以去参考monit的官方文档
启动、停止、重启
标准的start、stop、restart
1
2
3
sudo /etc/init.d/monit start
sudo /etc/init.d/monit stop
sudo /etc/init.d/monit restart
看到正确的提示信息即可,若遇到问题可以去查看配置里指定的日志文件,如/var/log/monit.log。
从我的服务器这几年的运行情况(monit发了的通知邮件)来看,nginx挂掉的事几乎没有,但apache或fast-cgi出问题的情况还是比较多见,赶快用上monit来管理你的服务器以提高服务器稳定性,跟502 Bad Gateway之类错误说拜拜吧。
介绍图:

二、用process-monitor监控系统进程

安装
1
2
3
cd russells-process-monitor
make
make install
运行
1
process-monitor -d /etc/init.d/network start
可通过tail -f /var/log/message 查看状态
帮助
01
02
03
04
05
06
07
08
09
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
# process-monitor -h
Usage: process-monitor [args] [--] childpath [child_args...]
       process-monitor -P <pipe> --command=stop|start|exit|hup|int
  -C|--clear-env              Clear the environment before setting the vars
                              specified with -E
  -c|--command <command>      Make a running process-monitor react to
                              <command>
  -D|--dir <dirname>          Change to <dirname> before starting child
  -d|--daemon                 Go into the background
                                (changes some signal handling behaviour)
  -E|--env <var=value>        Environment var for child process
                                (can use multiple times)
  -e|--email <addr>           Email when child restarts
                                (not implemented)
  -h|--help                   This message
  -L|--child-log-name <name>  Name to use in messages that come from the
                               child process
  -l|--log-name <name>        Name to use in our own messages
  -M|--max-wait-time <time>   Maximum time between child starts
  -m|--min-wait-time <time>   Minimum time between child starts
                                (seconds, cannot be less than 1)
  -P|--command-pipe <pipe>    Open named pipe <pipe> to receive commands
  -p|--pid-file <file>        Write PID to <file>, if in the background
  -u|--user <user>            User to run child as (name or uid)
                                (can be user:group)
  -- is required if childpath or any of child_args begin with -
参考:http://feilong.me/2011/02/monitor-core-processes-with-monit