Linux制作脚本来启动一组程序

作者:金良([email protected]) csdn博客:http://blog.csdn.net/u012176591

Linux制作脚本来启动一组程序_第1张图片

有时候需要同时启动一组后台服务,如TFTP下载服务、SAMBA通讯服务和NFS文件共享服务,每次到这个时候都要重复输入启动程序的命令,比较繁琐,这个问题终于解决了。

我写了一个脚本文件,命名myStart.sh,后缀.sh表示内容为shell脚本,同类的还有.bash。内容如下:

#!/bin/sh
/etc/init.d/openbsd-inetd restart
/etc/init.d/smbd restart
/etc/init.d/nfs-kernel-server restart

netstat -an|more
~                        

第一行#!/bin/sh是一个解释器,表示用sh来解释执行你的命令.

下面的三行分别表示启动TFTP、SAMBA和NFS服务。

我加上最后一行是为了查看TFTP服务启动成功与否。

写好后保存,然后执行以下命令

# chmod a+x myStart.sh 

把脚本文件设置为可执行。

然后就可以运行脚本了,命令如下

# myStart.sh 

执行状态如下:

root@ubuntu:/home/now# ./myStart.sh 
 * Restarting internet superserver inetd                                 [ OK ] 
Rather than invoking init scripts through /etc/init.d, use the service(8)
utility, e.g. service smbd restart

Since the script you are attempting to invoke has been converted to an
Upstart job, you may also use the stop(8) and then start(8) utilities,
e.g. stop smbd ; start smbd. The restart(8) utility is also available.
smbd stop/waiting
smbd start/running, process 2909
 * Stopping NFS kernel daemon                                            [ OK ] 
 * Unexporting directories for NFS kernel daemon...                      [ OK ] 
 * Exporting directories for NFS kernel daemon...                               exportfs: /etc/exports [1]: Neither 'subtree_check' or 'no_subtree_check' specified for export "*:/nfsroot".
  Assuming default behaviour ('no_subtree_check').
  NOTE: this default has changed since nfs-utils version 1.0.x

                                                                         [ OK ]
 * Starting NFS kernel daemon                                            [ OK ] 
Active Internet connections (servers and established)
Proto Recv-Q Send-Q Local Address           Foreign Address         State      
tcp        0      0 0.0.0.0:139             0.0.0.0:*               LISTEN     
tcp        0      0 0.0.0.0:111             0.0.0.0:*               LISTEN     
tcp        0      0 0.0.0.0:41395           0.0.0.0:*               LISTEN     
tcp        0      0 0.0.0.0:41172           0.0.0.0:*               LISTEN     
tcp        0      0 127.0.0.1:53            0.0.0.0:*               LISTEN     
tcp        0      0 0.0.0.0:22              0.0.0.0:*               LISTEN     
tcp        0      0 127.0.0.1:631           0.0.0.0:*               LISTEN     
tcp        0      0 0.0.0.0:38168           0.0.0.0:*               LISTEN     
tcp        0      0 0.0.0.0:60923           0.0.0.0:*               LISTEN     
tcp        0      0 0.0.0.0:445             0.0.0.0:*               LISTEN     
tcp        0      0 0.0.0.0:2049            0.0.0.0:*               LISTEN     
tcp        0      0 0.0.0.0:42122           0.0.0.0:*               LISTEN     
tcp        1      0 192.168.37.129:41993    91.189.89.144:80        CLOSE_WAIT 
tcp6       0      0 :::111                  :::*                    LISTEN     
tcp6       0      0 :::22                   :::*                    LISTEN     
tcp6       0      0 :::47351                :::*                    LISTEN     
tcp6       0      0 ::1:631                 :::*                    LISTEN     
tcp6       0      0 :::49404                :::*                    LISTEN     
tcp6       0      0 :::2049                 :::*                    LISTEN     
tcp6       0      0 :::53797                :::*                    LISTEN     
tcp6       0      0 :::41894                :::*                    LISTEN     
tcp6       0      0 :::39466                :::*                    LISTEN     
udp        0      0 0.0.0.0:40285           0.0.0.0:*                          
udp        0      0 0.0.0.0:914             0.0.0.0:*                          
udp        0      0 0.0.0.0:49595           0.0.0.0:*                          
udp        0      0 127.0.0.1:988           0.0.0.0:*                          
udp        0      0 0.0.0.0:50675           0.0.0.0:*                          
udp        0      0 0.0.0.0:33268           0.0.0.0:*                          
udp        0      0 0.0.0.0:2049            0.0.0.0:*                          
udp        0      0 0.0.0.0:56333           0.0.0.0:*                          
udp        0      0 127.0.0.1:53            0.0.0.0:*                          
udp        0      0 0.0.0.0:68              0.0.0.0:*                          
udp        0      0 0.0.0.0:69              0.0.0.0:*                          
udp        0      0 0.0.0.0:111             0.0.0.0:*               

注意倒数第二行:

udp        0      0 0.0.0.0:69              0.0.0.0:*  

表示69端口被打开,即开启了TFTP服务。


查看系统环境变量:

root@ubuntu:/home/now# echo $PATH
.:/usr/lib/jvm/jdk1.6.0_35/bin:/usr/lib/jvm/jdk1.6.0_35/jre/bin:/usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin:/sbin:/bin:/usr/games:/usr/local/arm/arm-2009q3/bin


把我们的脚本文件放到文件夹/usr/bin/,因为/usr/bin/是系统路径,当我们以后再任何地方执行脚本myStart.sh时,系统都能找到这个脚本文件。

root@ubuntu:/home/now# mv myStart.sh /usr/bin/

现在再次键入myStart.sh,看能像前面一样正确执行不?



你可能感兴趣的:(linux,脚本,启动程序)