好长一段时间本人的博客都未更新了,新年向大家献上第一份博文。网上关于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/目录下