好长一段时间本人的博客都未更新了,新年向大家献上第一份博文。网上关于nginx的启动脚本大都是shell版的,我尝试着用perl重写了一下,不足之处,恳请指正。
code:
- #!/bin/env perl
- #
- # chkconfig: - 85 15
- # description: nginx is a web server
- # progname: nginx
- # config_file: /opt/nginx/conf/nginx.conf
- # pid_file: /opt/nginx/logs/nginx.pid
- # author: Henry He on 2012/01/09
- use strict;
- use File::Basename qw/basename/;
- use Term::ANSIColor qw(:constants);
- # Global options for my color
- $Term::ANSIColor::AUTORESET = 1;
- my $base_dir = "/opt/nginx";
- my $prog = `basename $base_dir`;
- chomp $prog;
- if (! -f "$base_dir/conf/nginx.conf") {
- &errorexit("no nginx config file");
- }
- if (! -x "$base_dir/sbin/nginx") {
- &errorexit("can't excute nginx binary file");
- }
- if (@ARGV != 0) {
- for my $args (@ARGV) {
- if ($args =~ /(\bstart\b)+/) {
- print BOLD GREEN "Starting nginx now ...";
- system "$base_dir/sbin/nginx -c $base_dir/conf/nginx.conf";
- system "touch /var/lock/subsys/$prog";
- printf "%-s\n","[OK]";
- }
- elsif ($args =~ /(\bstop\b)+/) {
- print BOLD GREEN "Stopping nginx now ...";
- system "killall -s QUIT $prog";
- unlink "$base_dir/logs/nginx.pid";
- unlink "/var/lock/subsys/$prog";
- printf "%-s\n","[OK]";
- }
- elsif ($args =~ /(\breload\b)+/) {
- print BOLD GREEN "Reload nginx now";
- system "killall -s HUP $prog";
- printf "%-s\n","[OK]";
- }
- elsif ($args =~ /(\bstatus\b)+/) {
- if (open FILE,"<","$base_dir/logs/nginx.pid") {
- chomp(my $pid = <FILE>);
- close FILE;
- printf "%s\n","$prog (pid $pid is running)";
- }
- }
- else {
- usage();
- exit 1;
- }
- }
- } else {
- usage();
- }
- sub errorexit {
- printf "ERROR: %s",@_;
- printf "\n";
- exit 0;
- }
- sub usage {
- printf "USAGE: %-10s\n","/etc/init.d/nginx {start|stop|status|reload}";
- }
将此脚本命名为nginx并拷贝到/etc/init.d/目录下
- # cp nginx /etc/init.d/nginx
- # chkconfig --add nginx
- # chkconfig nginx on
- # /etc/init.d/nginx
- USAGE: /etc/init.d/nginx {start|stop|status|reload}
- # # /etc/init.d/nginx start
- Starting nginx now ...[OK]
- ......................................................