好长一段时间本人的博客都未更新了,新年向大家献上第一份博文。网上关于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?=~?/(bstartb)+/)?{?
- ????????????print?BOLD?GREEN?"Starting?nginx?now?...";?
- ????????????system?"$base_dir/sbin/nginx?-c?$base_dir/conf/nginx.conf";?
- ????????????system?"touch?/var/lock/subsys/$prog";?
- ????????????printf?"%-sn","[OK]";?
- ????????}?
- ????????elsif?($args?=~?/(bstopb)+/)?{?
- ????????????print?BOLD?GREEN?"Stopping?nginx?now?...";?
- ????????????system?"killall?-s?QUIT?$prog";?
- ????????????unlink?"$base_dir/logs/nginx.pid";?
- ????????????unlink?"/var/lock/subsys/$prog";?
- ????????????printf?"%-sn","[OK]";?
- ??????}?
- ???????elsif?($args?=~?/(breloadb)+/)?{?
- ????????????print?BOLD?GREEN?"Reload?nginx?now";?
- ????????????system?"killall?-s?HUP?$prog";?
- ????????????printf?"%-sn","[OK]";?
- ????????}?
- ????????elsif?($args?=~?/(bstatusb)+/)?{?
- ?
- ????????????if?(open?FILE,"<","$base_dir/logs/nginx.pid")?{?
- ????????????????chomp(my?$pid?=?<FILE>);?
- ????????????????close?FILE;?
- ????????????????printf?"%sn","$prog?(pid?$pid?is?running)";?
- ????????????}?
- ?
- ????????}?
- ????????else?{?
- ????????????usage();?
- ????????????exit?1;?
- ????????}?
- ?
- ????}?
- ?
- }?else?{?
- ????????usage();?
- }?
- ?
- sub?errorexit?{?
- ????printf?"ERROR:?%s",@_;?
- ????printf?"n";?
- ????exit?0;?
- }?
- ?
- sub?usage?{?
- ????printf?"USAGE:??%-10sn","/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]?
- ......................................................?